dac6d42295b1fff0828b5e8f42d3ec1ab359db01
[platform/core/messaging/email-service.git] / email-core / email-core-account.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@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 /******************************************************************************
24  * File :  email-core-account.c
25  * Desc :  Account Management
26  *
27  * Auth :  Kyuho Jo
28  *
29  * History :
30  *    2010.08.25  :  created
31  *****************************************************************************/
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <vconf.h>
39 #include <curl/curl.h>
40 #include <sys/shm.h>
41 #include <tpkp_curl.h>
42 #include <ckmc/ckmc-manager.h>
43
44
45 #include "email-convert.h"
46 #include "email-types.h"
47 #include "email-daemon.h"
48 #include "email-debug-log.h"
49 #include "email-storage.h"
50 #include "email-network.h"
51 #include "email-utilities.h"
52 #include "email-core-utils.h"
53 #include "email-core-event.h"
54 #include "email-core-global.h"
55 #include "email-core-account.h"
56 #include "email-core-mailbox.h"
57 #include "email-core-signal.h"
58 #include "email-core-imap-mailbox.h"
59 #include "email-core-container.h"
60 #include "email-core-key-manager.h"
61
62 #include "imap4r1.h"
63
64 #include "account.h"
65 #include "account-types.h"
66
67 #define EMAIL_SERVICE_BNR_ACCOUNT_KEY "email_service_backup_restore_account_key"
68
69 char *g_default_mbox_alias[MAILBOX_COUNT] = {
70         EMAIL_INBOX_DISPLAY_NAME,
71         EMAIL_DRAFTBOX_DISPLAY_NAME,
72         EMAIL_OUTBOX_DISPLAY_NAME,
73         EMAIL_SENTBOX_DISPLAY_NAME,
74         EMAIL_TRASH_DISPLAY_NAME,
75         EMAIL_SPAMBOX_DISPLAY_NAME,
76 };
77
78 char *g_default_mbox_name[MAILBOX_COUNT] = {
79         EMAIL_INBOX_NAME,
80         EMAIL_DRAFTBOX_NAME,
81         EMAIL_OUTBOX_NAME,
82         EMAIL_SENTBOX_NAME,
83         EMAIL_TRASH_DISPLAY_NAME,
84         EMAIL_SPAMBOX_NAME,
85 };
86
87 email_mailbox_type_e g_default_mbox_type[MAILBOX_COUNT] = {
88         EMAIL_MAILBOX_TYPE_INBOX,
89         EMAIL_MAILBOX_TYPE_DRAFT,
90         EMAIL_MAILBOX_TYPE_OUTBOX,
91         EMAIL_MAILBOX_TYPE_SENTBOX,
92         EMAIL_MAILBOX_TYPE_TRASH,
93         EMAIL_MAILBOX_TYPE_SPAMBOX,
94 };
95
96 INTERNAL_FUNC email_account_t* emcore_get_account_reference(char *multi_user_name, int account_id, int with_password)
97 {
98         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
99         int err = EMAIL_ERROR_NONE;
100         email_account_t *result_account = NULL;
101         emstorage_account_tbl_t *p_result_account = NULL;
102
103         if (account_id < 0) {
104                    emcore_get_account_from_unvalidated_account_list(account_id, &result_account);
105                    return result_account;
106         } else if (account_id > 0) {
107                         if (with_password) {
108                                 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &p_result_account, false, &err)) {
109                                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
110                                         return NULL;
111                                 }
112                         } else {
113                                 if (!emstorage_get_account_by_id(multi_user_name, account_id, GET_FULL_DATA_WITHOUT_PASSWORD, &p_result_account, false, &err)) {
114                                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed : [%d]", err);
115                                         return NULL;
116                                 }
117                         }
118
119                         result_account = (email_account_t *)em_malloc(sizeof(email_account_t));
120                         if (result_account == NULL) {
121                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
122                                 return NULL;
123                         }
124
125                         em_convert_account_tbl_to_account(p_result_account, result_account);
126
127                         emstorage_free_account(&p_result_account, 1, NULL);
128         }
129
130         EM_DEBUG_FUNC_END("[%p]", result_account);
131         return result_account;
132 }
133
134 INTERNAL_FUNC int emcore_get_account_reference_list(char *multi_user_name, email_account_t **output_account_list, int *output_count)
135 {
136         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p]", output_account_list, output_count);
137     int err = EMAIL_ERROR_NONE;
138     int i = 0;
139     int account_count = 0;
140     email_account_t *account_list = NULL;
141     emstorage_account_tbl_t *account_list_tbl = NULL;
142
143     if (!emstorage_get_account_list(multi_user_name, &account_count, &account_list_tbl, false, true, &err)) {
144         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed : [%d]", err);
145         goto FINISH_OFF;
146     }
147
148     if (account_count > 0) {
149         account_list = em_malloc(sizeof(email_account_t) * (account_count));
150         if (account_list == NULL) {
151             EM_DEBUG_EXCEPTION("em_mallocfailed");
152             err = EMAIL_ERROR_OUT_OF_MEMORY;
153             goto FINISH_OFF;
154         }
155     }
156
157     for (i = 0; i < account_count; i++) {
158         em_convert_account_tbl_to_account(&account_list_tbl[i], &account_list[i]);
159     }
160
161 FINISH_OFF:
162
163     if (account_list_tbl)
164         emstorage_free_account(&account_list_tbl, account_count, NULL);
165
166     if (output_account_list)
167         *output_account_list = account_list;
168
169     if (output_count)
170         *output_count = account_count;
171
172         EM_DEBUG_FUNC_END();
173         return err;
174 }
175
176 static char *emcore_get_imap_capability_string(MAILSTREAM *input_stream)
177 {
178         EM_DEBUG_FUNC_BEGIN_SEC("input_stream[%p]", input_stream);
179         char *result_string = NULL;
180         IMAPCAP *imap_capability = NULL;
181         char capability_string[512] = { 0, };
182
183         if ((imap_capability = imap_cap(input_stream))) {
184                 if (imap_capability->idle)
185                         EM_SAFE_STRNCAT(capability_string, "IDLE ", sizeof(capability_string) - EM_SAFE_STRLEN(capability_string) - 1);
186                 if (imap_capability->quota)
187                         EM_SAFE_STRNCAT(capability_string, "QUOTA ", sizeof(capability_string) - EM_SAFE_STRLEN(capability_string) - 1);
188                 if (imap_capability->starttls)
189                         EM_SAFE_STRNCAT(capability_string, "STARTTLS ", sizeof(capability_string) - EM_SAFE_STRLEN(capability_string) - 1);
190 #ifdef __FEATURE_XLIST_SUPPORT__
191                 if (imap_capability->xlist)
192                         EM_SAFE_STRNCAT(capability_string, "XLIST ", sizeof(capability_string) - EM_SAFE_STRLEN(capability_string) - 1);
193 #endif /* __FEATURE_XLIST_SUPPORT__ */
194                 result_string = EM_SAFE_STRDUP(capability_string);
195         }
196
197         EM_DEBUG_FUNC_END("[%s]", result_string);
198         return result_string;
199 }
200
201 INTERNAL_FUNC int emcore_validate_account_with_account_info(char *multi_user_name, email_account_t *account, email_event_type_t event_type, char **output_server_capability_string, int event_handle, int *err_code)
202 {
203         EM_DEBUG_FUNC_BEGIN_SEC("account[%p] output_server_capability_string[%p] err_code[%p] incoming_server_address [%s]", account, output_server_capability_string, err_code, account->incoming_server_address);
204
205         int ret = false;
206         int err = EMAIL_ERROR_NONE;
207         int server_capability_string_length = 0;
208         email_session_t *session = NULL;
209         char *imap_capability_string = NULL;
210         char smtp_capability_string[128] = { 0, };
211         SENDSTREAM *stream = NULL;
212         MAILSTREAM *tmp_stream = NULL;
213
214         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
215
216         if (!emnetwork_check_network_status(&err)) {
217                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
218                 goto FINISH_OFF;
219         }
220         EM_DEBUG_LOG("Network available");
221
222         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
223
224         if (!emcore_get_empty_session(&session)) {
225                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
226 /*              err = EMAIL_ERROR_SESSION_NOT_FOUND;
227                 goto FINISH_OFF; */
228         }
229
230         /* validate connection for pop3/imap */
231         EM_DEBUG_LOG("Validate connection for POP3/IMAP4");
232
233         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
234
235         if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name,
236                                                                                                                         account,
237                                                                                                                         0,
238                                                                                                                         true,
239                                                                                                                         (void **)&tmp_stream,
240                                                                                                                         &err) || !tmp_stream) {
241                 EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
242                 if (EMAIL_ERROR_AUTHENTICATE == err || EMAIL_ERROR_LOGIN_FAILURE == err) {      /*  wrong password or etc */
243                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed : Login or Authentication failed - %d", err);
244                 } else if (EMAIL_ERROR_CONNECTION_FAILURE != err) {
245                         /* err = EMAIL_ERROR_VALIDATE_ACCOUNT */
246                 }
247                 goto FINISH_OFF;
248         }
249
250         if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)
251                 imap_capability_string = emcore_get_imap_capability_string(tmp_stream);
252
253 #ifdef __FEATURE_SMTP_VALIDATION__
254         /* validate connection for SMTP */
255         EM_DEBUG_LOG("Validate connection for SMTP server");
256
257         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
258
259         if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name,
260                                                                                                                         account,
261                                                                                                                         EMAIL_CONNECT_FOR_SENDING,
262                                                                                                                         true,
263                                                                                                                         (void **)&stream,
264                                                                                                                         &err) || !stream) {
265                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
266                 err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
267                 goto FINISH_OFF;
268
269 #if 0
270                 if (account->outgoing_server_secure_connection == 0x01) /*  0x01 == SSL */ {
271                         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
272
273                         EM_DEBUG_LOG("Retry with TLS");
274                         account->outgoing_server_secure_connection = 0x02;      /*  0x02 == TLS */
275
276                         if (!emcore_connect_to_remote_mailbox_with_account_info(multi_user_name,
277                                                                                                                                         account,
278                                                                                                                                         EMAIL_CONNECT_FOR_SENDING,
279                                                                                                                                         true,
280                                                                                                                                         (void **)&stream,
281                                                                                                                                         &err) || !stream) {
282                                 EM_DEBUG_LOG("emcore_connect_to_remote_mailbox failed [%d]", err);
283                                 err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
284                                 account->outgoing_server_secure_connection = 0x01;      /*  restore to the previous value */
285                                 goto FINISH_OFF;
286                         }
287
288                         FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
289                 } else {
290                         err = EMAIL_ERROR_VALIDATE_ACCOUNT_OF_SMTP;
291                         goto FINISH_OFF;
292                 }
293 #endif
294         }
295         if (stream && stream->protocol.esmtp.ok) {
296                 if (stream->protocol.esmtp.size.ok && stream->protocol.esmtp.size.limit > 0) {
297                         account->outgoing_server_size_limit = stream->protocol.esmtp.size.limit;
298                         SNPRINTF(smtp_capability_string, 128, "SMTP_MAIL_SIZE_LIMIT=%d ", account->outgoing_server_size_limit);
299                         EM_DEBUG_LOG("%s", smtp_capability_string);
300                 }
301         }
302 #endif /* __FEATURE_SMTP_VALIDATION__ */
303
304         int dummy = 0;
305         if (!emcore_check_event_thread_status(&dummy, event_handle)) {
306                 EM_DEBUG_LOG("canceled event: [%d]", dummy);
307                 if (event_type == EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT || event_type == EMAIL_EVENT_VALIDATE_ACCOUNT_EX) {
308                         if (!emcore_delete_account(multi_user_name, account->account_id, false, NULL))
309                                 EM_DEBUG_EXCEPTION("emdaemon_delete_account failed [%d]", account->account_id);
310                 }
311                 err = EMAIL_ERROR_CANCELLED;
312                 goto FINISH_OFF;
313         }
314
315         if (output_server_capability_string) {
316                 server_capability_string_length = EM_SAFE_STRLEN(imap_capability_string) + EM_SAFE_STRLEN(smtp_capability_string);
317                 if (server_capability_string_length) {
318                         *output_server_capability_string = em_malloc(server_capability_string_length + 1);
319
320                         if      (*output_server_capability_string == NULL) {
321                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
322                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
323                                 goto FINISH_OFF;
324                         }
325                         EM_SAFE_STRNCAT(*output_server_capability_string,smtp_capability_string , (server_capability_string_length + 1) - EM_SAFE_STRLEN(*output_server_capability_string)- 1);
326                         EM_SAFE_STRCAT(*output_server_capability_string, imap_capability_string);
327                         EM_DEBUG_LOG("%s", *output_server_capability_string);
328                 }
329         }
330
331         ret = true;
332
333 FINISH_OFF:
334         if (stream)
335                 smtp_close(stream);
336
337         if (tmp_stream)
338                 tmp_stream = mail_close(tmp_stream);
339
340         if (err_code != NULL)
341                 *err_code = err;
342
343         EM_SAFE_FREE(imap_capability_string);
344
345         emcore_clear_session(session);
346
347         EM_DEBUG_FUNC_END();
348         return ret;
349 }
350
351
352 INTERNAL_FUNC int emcore_validate_account(char *multi_user_name, int account_id, int handle, int *err_code)
353 {
354         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
355
356         int err = EMAIL_ERROR_NONE, ret = false;
357         email_account_t *ref_account = NULL;
358
359
360         if (account_id <= 0) {
361                 EM_DEBUG_EXCEPTION("account_id[%p]", account_id);
362                 err = EMAIL_ERROR_INVALID_PARAM;
363                 goto FINISH_OFF;
364         }
365
366         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
367
368         if (ref_account && emcore_validate_account_with_account_info(multi_user_name, ref_account, EMAIL_EVENT_VALIDATE_ACCOUNT, NULL, handle, &err) == false) {
369                 EM_DEBUG_EXCEPTION("emcore_validate_account_with_account_info failed (%d)", err);
370                 goto FINISH_OFF;
371         }
372
373         ret = true;
374
375 FINISH_OFF:
376
377         if (ref_account) {
378                 emcore_free_account(ref_account);
379                 EM_SAFE_FREE(ref_account);
380         }
381
382         if (err_code)
383                 *err_code = err;
384
385         EM_DEBUG_FUNC_END();
386
387         return ret;
388 }
389
390 key_t del_account_key = 4511; /* name of the segment d/4 e/5 l/11 */
391
392 INTERNAL_FUNC int emcore_delete_account(char *multi_user_name, int account_id, int input_delete_from_account_svc, int *err_code)
393 {
394         EM_DEBUG_FUNC_BEGIN("account_id[%d], err_code[%p]", account_id, err_code);
395
396         int shmid = 0;
397         int *del_account_id = NULL;
398
399         EM_DEBUG_LOG("begin account_id [%d]", account_id);
400         /* worker thread is single instance, so multiple accounts cant be deleted concurrently */
401         if ((shmid = shmget(del_account_key, sizeof(int), IPC_CREAT | 0666)) != -1) {
402                 /* attaching the segment to the current process space */
403                 if ((del_account_id = (int*) shmat(shmid, NULL, 0)) != (int*) -1) {
404                         /* write it */
405                         *del_account_id = account_id;
406                         EM_DEBUG_LOG("recorded account_id [%d]", *del_account_id);
407                 } else
408                         EM_DEBUG_EXCEPTION("shmget error[%d]", errno);
409
410         } else
411                 EM_DEBUG_EXCEPTION("shmget error[%d]", errno);
412
413         /*  default variabl */
414         int ret = false;
415         int err = EMAIL_ERROR_NONE;
416         int before_tr_begin = 0;
417
418         if (account_id < FIRST_ACCOUNT_ID)  {
419                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
420                 err = EMAIL_ERROR_INVALID_PARAM;
421                 before_tr_begin = 1;
422                 goto FINISH_OFF;
423         }
424
425 #ifdef __FEATURE_LOCAL_ACTIVITY__
426         /* Delete all local activities of previous account */
427         emstorage_activity_tbl_t activity;
428         memset(&activity, 0x00, sizeof(emstorage_activity_tbl_t));
429         activity.account_id = account_id;
430     activity.multi_user_name = EM_SAFE_STRDUP(multi_user_name);
431
432         if (!emcore_delete_activity(&activity, &err)) {
433                 EM_DEBUG_LOG("\t emcore_delete_activity failed - %d", err);
434                 before_tr_begin = 1;
435                 goto FINISH_OFF;
436         }
437 #endif
438
439 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
440         if (false == emcore_clear_partial_body_thd_event_que(&err))
441                 EM_DEBUG_EXCEPTION(" emcore_clear_partial_body_thd_event_que [%d]", err);
442
443         if (false == emstorage_delete_full_pbd_activity_data(multi_user_name, account_id, true, &err))
444                 EM_DEBUG_EXCEPTION("emstorage_delete_full_pbd_activity_data failed [%d]", err);
445
446 #endif
447
448 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
449         if (!emcore_clear_auto_download_queue())
450                 EM_DEBUG_EXCEPTION("emcore_clear_auto_download_queue failed");
451
452         if (!emstorage_delete_all_auto_download_activity(multi_user_name, account_id, true, &err))
453                 EM_DEBUG_EXCEPTION("emstorage_delete_all_auto_download_activity failed [%d]", err);
454 #endif
455
456         if (input_delete_from_account_svc == true) {
457                 int error_code_from_account_svc = 0;
458                 email_account_t *account_to_be_deleted;
459                 void *join_zone = NULL;
460
461                 account_to_be_deleted = emcore_get_account_reference(multi_user_name, account_id, false);
462                 if (account_to_be_deleted && account_to_be_deleted->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
463                         EM_DEBUG_LOG("Calling account_svc_delete with account_svc_id[%d]", account_to_be_deleted->account_svc_id);
464                         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
465                                 if ((err = emcore_set_join_zone(multi_user_name, &join_zone)) != EMAIL_ERROR_NONE) {
466                                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
467                                         goto FINISH_OFF;
468                                 }
469                         }
470
471                         error_code_from_account_svc = account_delete_from_db_by_id(account_to_be_deleted->account_svc_id);
472                         EM_DEBUG_LOG("account_delete_from_db_by_id returns [%d]", error_code_from_account_svc);
473
474                         if (join_zone)
475                                 emcore_unset_join_zone(join_zone);
476                 }
477
478                 if (account_to_be_deleted) {
479                         emcore_free_account(account_to_be_deleted);
480                         EM_SAFE_FREE(account_to_be_deleted);
481                 }
482         }
483
484         if (emcore_cancel_all_threads_of_an_account(multi_user_name, account_id) < EMAIL_ERROR_NONE) {
485                 EM_DEBUG_EXCEPTION("There are some remaining jobs. I couldn't stop them.");
486                 err = EMAIL_ERROR_CANNOT_STOP_THREAD;
487                 before_tr_begin = 1;
488                 goto FINISH_OFF;
489         }
490
491         /* Delete contact log */
492         if (((err = emcore_delete_contacts_log(multi_user_name, account_id)) != EMAIL_ERROR_NONE) && (err != EMAIL_ERROR_DATA_NOT_FOUND)) {
493         EM_DEBUG_EXCEPTION("emcore_delete_contacts_log failed : [%d]", err);
494         }
495
496         /*  BEGIN TRANSACTION;           */
497         if (!emstorage_begin_transaction(multi_user_name, NULL, NULL, &err)) {
498                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction failed [%d]", err);
499                 before_tr_begin = 1;
500                 goto FINISH_OFF;
501         }
502
503         if (!emstorage_delete_account(multi_user_name, account_id, false, &err)) {
504                 if (err != EMAIL_ERROR_SYSTEM_FAILURE) {
505                         EM_DEBUG_EXCEPTION("emstorage_delete_account failed [%d]", err);
506                         goto FINISH_OFF;
507                 }
508         }
509
510 #ifdef __FEATURE_KEEP_CONNECTION__
511         /* emcore_reset_streams(); */
512         emcore_remove_connection_info(account_id);
513 #endif
514
515         if ((err = emcore_delete_all_mails_of_acount(multi_user_name, account_id)) != EMAIL_ERROR_NONE) {
516                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_acount failed [%d]", err);
517                 goto FINISH_OFF;
518         }
519
520         /*  delete all mailboxes */
521         if (!emstorage_delete_mailbox(multi_user_name, account_id, -1, 0, false, &err)) {
522                 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
523                         EM_DEBUG_EXCEPTION("emstorage_delete_mailbox failed - %d", err);
524                         goto FINISH_OFF;
525                 }
526         }
527
528         /*  delete local imap sync mailbox from imap mailbox table */
529         if (!emstorage_remove_downloaded_mail(multi_user_name, account_id, 0, NULL, NULL, false, &err)) {
530                 EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
531                 goto FINISH_OFF;
532         }
533
534         emcore_display_unread_in_badge(multi_user_name);
535         emcore_delete_notification_by_account(multi_user_name, account_id, true, true, true);
536
537         ret = true;
538
539 FINISH_OFF:
540         if (ret == true) {      /*  COMMIT TRANSACTION; */
541                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {
542                         err = EMAIL_ERROR_DB_FAILURE;
543                         ret = false;
544                 }
545                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE, account_id, 0, NULL, 0))
546                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
547
548         } else {        /*  ROLLBACK TRANSACTION; */
549                 if (!before_tr_begin && emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false)
550                         err = EMAIL_ERROR_DB_FAILURE;
551                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_DELETE_FAIL, account_id, err, NULL, 0))
552                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_DELETE] : Notification Failed >>> ");
553         }
554
555         if (err_code)
556                 *err_code = err;
557
558         /* del the segment */
559         if (shmid != -1) {
560                 shmctl(shmid, IPC_RMID, NULL);
561         }
562
563         EM_DEBUG_FUNC_END();
564
565         return ret;
566 }
567
568 INTERNAL_FUNC int emcore_create_account(char *multi_user_name, email_account_t *account, int add_account_to_account_svc, int *err_code)
569 {
570         EM_DEBUG_FUNC_BEGIN("account[%p] add_account_to_account_svc [%d] err_code[%p]", account, add_account_to_account_svc, err_code);
571
572         int ret = false;
573         int err = EMAIL_ERROR_NONE;
574         int i, count = 0;
575         int private_id = 0;
576         char vconf_private_id[MAX_PATH] = {0, };
577         email_mailbox_t local_mailbox = {0};
578         emstorage_account_tbl_t *temp_account_tbl = NULL;
579
580         if (account == NULL)  {
581                 EM_DEBUG_EXCEPTION("account[%p]", account);
582                 err = EMAIL_ERROR_INVALID_PARAM;
583                 goto FINISH_OFF;
584         }
585
586         if (!emstorage_get_account_count(multi_user_name, &count, true, &err))  {
587                 EM_DEBUG_EXCEPTION("emstorage_get_account_count failed - %d", err);
588                 goto FINISH_OFF;
589         }
590
591
592         if (count >= EMAIL_ACCOUNT_MAX)  {
593                 EM_DEBUG_EXCEPTION("too many accounts...");
594                 err = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
595                 goto FINISH_OFF;
596         }
597
598         account->account_id = 0;
599
600         /* Temporarily code - begin */
601         if (account->default_mail_slot_size == 0) {
602                 account->default_mail_slot_size = 50;
603                 EM_DEBUG_LOG("account->default_mail_slot_size [%d]", account->default_mail_slot_size);
604         }
605         /* Temporarily code - end */
606
607         /* check for email address validation */
608         EM_DEBUG_LOG_SEC("account->user_email_address[%s]", account->user_email_address);
609         if (account->user_email_address) {
610                 if ((err = em_verify_email_address(account->user_email_address)) != EMAIL_ERROR_NONE) {
611                         EM_DEBUG_EXCEPTION("em_verify_email_address error [%d]", err);
612                         goto FINISH_OFF;
613                 }
614         }
615
616         if (EM_SAFE_STRLEN(account->options.alert_ringtone_path) == 0) {
617                 account->options.alert_ringtone_path = EM_SAFE_STRDUP(vconf_get_str(VCONFKEY_SETAPPL_NOTI_MSG_RINGTONE_PATH_STR));
618         }
619
620         if (add_account_to_account_svc) {
621                 if (account->incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
622                         int account_svc_id = 0;
623                         int error_code = 0;
624                         account_h account_handle = NULL;
625                         void *join_zone = NULL;
626
627                         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
628                                 err = emcore_set_join_zone(multi_user_name, &join_zone);
629                                 if (err != EMAIL_ERROR_NONE) {
630                                         EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
631                                         goto FINISH_OFF;
632                                 }
633                         }
634
635
636                         error_code = account_create(&account_handle);
637                         if (error_code != ACCOUNT_ERROR_NONE) {
638                                 EM_DEBUG_EXCEPTION("account_create failed [%d]", error_code);
639                                 err = error_code;
640                                 if (join_zone) emcore_unset_join_zone(join_zone);
641                                 goto FINISH_OFF;
642                         }
643
644                         error_code = account_set_user_name(account_handle, account->incoming_server_user_name);
645                         if (error_code != ACCOUNT_ERROR_NONE) {
646                                 EM_DEBUG_LOG("account_set_user_name failed [%d]", error_code);
647                         }
648
649                         error_code = account_set_domain_name(account_handle, account->account_name);
650                         if (error_code != ACCOUNT_ERROR_NONE) {
651                                 EM_DEBUG_LOG("account_set_domain_name failed [%d]", error_code);
652                         }
653
654                         error_code = account_set_email_address(account_handle,  account->user_email_address);
655                         if (error_code != ACCOUNT_ERROR_NONE) {
656                                 EM_DEBUG_LOG("account_set_email_address failed [%d]", error_code);
657                         }
658
659                         error_code = account_set_source(account_handle, "SLP EMAIL");
660                         if (error_code != ACCOUNT_ERROR_NONE) {
661                                 EM_DEBUG_LOG("account_set_source failed [%d]", error_code);
662                         }
663
664                         error_code = account_set_package_name(account_handle, "email-setting-efl");
665                         if (error_code != ACCOUNT_ERROR_NONE) {
666                                 EM_DEBUG_LOG("account_set_package_name failed [%d]", error_code);
667                         }
668
669                         /* account_set_capability(account_handle , ACCOUNT_CAPABILITY_EMAIL, ACCOUNT_CAPABILITY_ENABLED); OLD API */
670                         error_code = account_set_capability(account_handle , ACCOUNT_SUPPORTS_CAPABILITY_EMAIL , ACCOUNT_CAPABILITY_ENABLED);
671                         if (error_code != ACCOUNT_ERROR_NONE) {
672                                 EM_DEBUG_LOG("account_set_capability failed [%d]", error_code);
673                         }
674
675                         error_code = account_set_sync_support(account_handle, ACCOUNT_SYNC_STATUS_IDLE); /* This means "The account is supporting 'sync' and initialized as idle status" */
676                         if (error_code != ACCOUNT_ERROR_NONE) {
677                                 EM_DEBUG_LOG("account_set_sync_support failed [%d]", error_code);
678                         }
679
680                         if (account->logo_icon_path) {
681                                 error_code = account_set_icon_path(account_handle, account->logo_icon_path);
682                                 if (error_code != ACCOUNT_ERROR_NONE) {
683                                         EM_DEBUG_LOG("account_set_icon_path failed [%d]", error_code);
684                                 }
685                         }
686
687                         error_code = account_insert_to_db(account_handle, &account_svc_id);
688                         if (error_code != ACCOUNT_ERROR_NONE) {
689                                 EM_DEBUG_EXCEPTION("account_insert_to_db failed [%d]", error_code);
690                         } else {
691                                 account->account_svc_id = account_svc_id;
692                                 EM_DEBUG_LOG("account_insert_to_db succeed");
693                         }
694
695                         if (account_handle)
696                                 account_destroy(account_handle);
697
698                         if (join_zone) emcore_unset_join_zone(join_zone);
699                 }
700         }
701
702         temp_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
703         if (!temp_account_tbl) {
704                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
705                 goto FINISH_OFF;
706         }
707
708         em_convert_account_to_account_tbl(account, temp_account_tbl);
709
710         if (!emstorage_add_account(multi_user_name, temp_account_tbl, true, &err))  {
711                 EM_DEBUG_EXCEPTION("emstorage_add_account failed - %d", err);
712                 goto FINISH_OFF;
713         }
714         account->account_id = temp_account_tbl->account_id;
715
716         if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
717                 /* 1. create default local mailbox
718                 *    (Inbox, Draft, Outbox, Sentbox) */
719                 for (i = 0; i < MAILBOX_COUNT; i++) {
720                         EM_DEBUG_LOG_SEC("g_default_mbox_name [%d/%d] is [%s]", i, MAILBOX_COUNT, g_default_mbox_name[i]);
721                         local_mailbox.account_id      = temp_account_tbl->account_id;
722                         local_mailbox.mailbox_name    = EM_SAFE_STRDUP(g_default_mbox_name[i]);
723                         local_mailbox.alias           = EM_SAFE_STRDUP(g_default_mbox_alias[i]);
724                         local_mailbox.mailbox_type    = g_default_mbox_type[i];
725                         local_mailbox.mail_slot_size  = temp_account_tbl->default_mail_slot_size;
726                         local_mailbox.eas_data        = NULL;
727                         local_mailbox.eas_data_length = 0;
728
729                         if (local_mailbox.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
730                                 local_mailbox.local = EMAIL_MAILBOX_FROM_SERVER;
731                         else
732                                 local_mailbox.local = EMAIL_MAILBOX_FROM_LOCAL;
733
734                         if (!emcore_create_mailbox(multi_user_name, &local_mailbox, 0, account->incoming_server_type, account->default_mail_slot_size, &err)) {
735                                 EM_DEBUG_EXCEPTION("emcore_create failed [%d]", err);
736                                 emcore_free_mailbox(&local_mailbox);
737                                 goto FINISH_OFF;
738                         }
739
740                         emcore_free_mailbox(&local_mailbox);
741                 }
742         }
743
744         /* Initialize the noti private id */
745         SNPRINTF(vconf_private_id, sizeof(vconf_private_id), "%s/%d", VCONF_KEY_NOTI_PRIVATE_ID, account->account_id);
746         if (vconf_get_int(vconf_private_id, &private_id) != 0) {
747                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
748         } else {
749                 if (vconf_set_int(vconf_private_id, 0) != 0) {
750                         EM_DEBUG_EXCEPTION("vconf_set_int failed : [NOTI key initialize]");
751                 }
752         }
753
754         ret = true;
755
756 FINISH_OFF:
757         if (temp_account_tbl)
758                 emstorage_free_account(&temp_account_tbl, 1, NULL);
759
760         if (ret == false && account != NULL)  {
761                 if (!emcore_delete_account(multi_user_name, account->account_id, false, NULL))
762                         EM_DEBUG_EXCEPTION("emdaemon_delete_account Failed [%d]", account->account_id);
763         }
764
765         if (err_code)
766                 *err_code = err;
767
768         EM_DEBUG_FUNC_END("Return value [%d]", ret);
769         return ret;
770 }
771
772 INTERNAL_FUNC int emcore_free_account_list(email_account_t **account_list, int count, int *err_code)
773 {
774         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
775
776         int ret = false;
777         int err = EMAIL_ERROR_NONE;
778
779         if (count <= 0 || !account_list || !*account_list) {
780                 err = EMAIL_ERROR_INVALID_PARAM;
781                 goto FINISH_OFF;
782         }
783
784         email_account_t *p = *account_list;
785         int i;
786         for (i = 0; i < count; i++)
787                 emcore_free_account(p+i);
788
789         EM_SAFE_FREE(p);
790         *account_list = NULL;
791
792         ret = true;
793
794 FINISH_OFF:
795         if (err_code)
796                 *err_code = err;
797         EM_DEBUG_FUNC_END();
798         return ret;
799 }
800
801 INTERNAL_FUNC void emcore_free_option(email_option_t *option)
802 {
803         EM_SAFE_FREE(option->display_name_from);
804         EM_SAFE_FREE(option->signature);
805 }
806
807
808 INTERNAL_FUNC void emcore_free_account(email_account_t *account)
809 {
810         if (!account) return;
811
812         EM_SAFE_FREE(account->account_name);
813         EM_SAFE_FREE(account->logo_icon_path);
814         EM_SAFE_FREE(account->user_data);
815         account->user_data_length = 0;
816         EM_SAFE_FREE(account->user_display_name);
817         EM_SAFE_FREE(account->user_email_address);
818         EM_SAFE_FREE(account->reply_to_address);
819         EM_SAFE_FREE(account->return_address);
820         EM_SAFE_FREE(account->incoming_server_address);
821         EM_SAFE_FREE(account->incoming_server_user_name);
822         EM_SAFE_FREE(account->incoming_server_password);
823         EM_SAFE_FREE(account->outgoing_server_address);
824         EM_SAFE_FREE(account->outgoing_server_user_name);
825         EM_SAFE_FREE(account->outgoing_server_password);
826         emcore_free_option(&account->options);
827         EM_SAFE_FREE(account->certificate_path);
828
829         EM_DEBUG_FUNC_END();
830 }
831
832 INTERNAL_FUNC int emcore_duplicate_account_members(const email_account_t *input_account, email_account_t *output_account_dup)
833 {
834         memcpy(output_account_dup, input_account , sizeof(email_account_t));
835
836         output_account_dup->account_name                             = EM_SAFE_STRDUP(input_account->account_name);
837         output_account_dup->incoming_server_address                  = EM_SAFE_STRDUP(input_account->incoming_server_address);
838         output_account_dup->user_email_address                       = EM_SAFE_STRDUP(input_account->user_email_address);
839         output_account_dup->incoming_server_user_name                = EM_SAFE_STRDUP(input_account->incoming_server_user_name);
840         output_account_dup->incoming_server_password                 = EM_SAFE_STRDUP(input_account->incoming_server_password);
841         output_account_dup->outgoing_server_address                  = EM_SAFE_STRDUP(input_account->outgoing_server_address);
842         output_account_dup->outgoing_server_user_name                = EM_SAFE_STRDUP(input_account->outgoing_server_user_name);
843         output_account_dup->outgoing_server_password                 = EM_SAFE_STRDUP(input_account->outgoing_server_password);
844         output_account_dup->user_display_name                        = EM_SAFE_STRDUP(input_account->user_display_name);
845         output_account_dup->reply_to_address                         = EM_SAFE_STRDUP(input_account->reply_to_address);
846         output_account_dup->return_address                           = EM_SAFE_STRDUP(input_account->return_address);
847         output_account_dup->logo_icon_path                           = EM_SAFE_STRDUP(input_account->logo_icon_path);
848         output_account_dup->user_data                                = em_memdup(input_account->user_data, input_account->user_data_length);
849         output_account_dup->options.display_name_from                = EM_SAFE_STRDUP(input_account->options.display_name_from);
850         output_account_dup->options.signature                        = EM_SAFE_STRDUP(input_account->options.signature);
851         output_account_dup->certificate_path                         = EM_SAFE_STRDUP(input_account->certificate_path);
852
853         return EMAIL_ERROR_NONE;
854 }
855
856
857 INTERNAL_FUNC void emcore_duplicate_account(const email_account_t *account, email_account_t **account_dup, int *err_code)
858 {
859         EM_DEBUG_FUNC_BEGIN("account[%p]", account);
860
861         if (!account || !account_dup) { /*prevent 40514*/
862                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
863                 if (err_code) *err_code = EMAIL_ERROR_INVALID_PARAM;
864                 return;
865         }
866
867         *account_dup = em_malloc(sizeof(email_account_t));
868         if (!*account_dup) { /*prevent 40514*/
869                 EM_DEBUG_EXCEPTION("malloc failed...");
870                 if (err_code) *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
871                 return;
872         }
873
874         emcore_duplicate_account_members(account, *account_dup);
875
876         if (err_code != NULL)
877                 *err_code = EMAIL_ERROR_NONE;
878
879         EM_DEBUG_FUNC_END();
880 }
881
882 #ifdef __FEATURE_BACKUP_ACCOUNT__
883
884 static int append_data_into_buffer(char **target_buffer, int *target_buffer_lenth, char *input_data, int input_data_length, int *error_code)
885 {
886         EM_DEBUG_FUNC_BEGIN("target_buffer [%p], target_buffer_lenth [%p], input_data [%p], input_data_length[%d]", target_buffer, target_buffer_lenth, input_data, input_data_length);
887         int local_error_code = EMAIL_ERROR_NONE, ret_code = false;
888
889         if (!target_buffer || !target_buffer_lenth || !input_data) {
890                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
891                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
892                 goto FINISH_OFF;
893         }
894
895         if (*target_buffer_lenth > 0 && input_data_length) {
896                 EM_DEBUG_LOG("*target_buffer_lenth [%d]", *target_buffer_lenth);
897                 *target_buffer = realloc(*target_buffer, (*target_buffer_lenth) + input_data_length);
898                 if (!*target_buffer) {
899                         EM_DEBUG_EXCEPTION("realloc failed");
900                         local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
901                         goto FINISH_OFF;
902                 }
903                 memcpy(*target_buffer + (*target_buffer_lenth), input_data, input_data_length);
904                 *target_buffer_lenth += input_data_length;
905                 EM_DEBUG_LOG("*target_buffer_lenth [%d] input_data_length [%d]", *target_buffer_lenth, input_data_length);
906         } else {
907                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
908                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
909                 goto FINISH_OFF;
910         }
911
912         ret_code = true;
913
914 FINISH_OFF:
915
916         if (error_code)
917                 *error_code = local_error_code;
918         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
919
920         return ret_code;
921 }
922
923
924 static int emcore_write_account_into_buffer(char **target_buffer, int *target_buffer_lenth, emstorage_account_tbl_t *account_tbl_ptr, int *error_code)
925 {
926         EM_DEBUG_FUNC_BEGIN("target_buffer [%p], target_buffer_lenth [%p], account_tbl_ptr [%p], error_code [%p]", target_buffer, target_buffer_lenth, account_tbl_ptr, error_code);
927         int local_error_code = EMAIL_ERROR_NONE, ret_code = false, stream_length = 0;
928         email_account_t temp_account = {0};
929         char *byte_stream = NULL;
930
931         if (em_convert_account_tbl_to_account(account_tbl_ptr, &temp_account)) {
932                 byte_stream = em_convert_account_to_byte_stream(&temp_account, &stream_length);
933                 EM_DEBUG_LOG("stream_length [%d]", stream_length);
934                 /*  EM_DEBUG_LOG_SEC("incoming_server_password [%s]", temp_account->password) */
935
936                 if (byte_stream) {
937                         if (!append_data_into_buffer(target_buffer, target_buffer_lenth, (char *)&stream_length, sizeof(int), &local_error_code)) {
938                                 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
939                                 goto FINISH_OFF;
940                         }
941                         EM_DEBUG_LOG("append_data_into_buffer succeed for stream_length");
942
943                         if (!append_data_into_buffer(target_buffer, target_buffer_lenth, byte_stream, stream_length, &local_error_code)) {
944                                 EM_DEBUG_EXCEPTION("append_data_into_buffer failed");
945                                 goto FINISH_OFF;
946                         }
947                         EM_DEBUG_LOG("append_data_into_buffer succeed for byte_stream");
948                 }
949         } else {
950                 EM_DEBUG_EXCEPTION("em_convert_account_tbl_to_account failed");
951                 local_error_code = EMAIL_ERROR_SYSTEM_FAILURE;
952                 goto FINISH_OFF;
953         }
954
955         ret_code = true;
956 FINISH_OFF:
957         emcore_free_account(&temp_account);
958         if (error_code)
959                 *error_code = local_error_code;
960
961         EM_SAFE_FREE(byte_stream);
962
963         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
964         return ret_code;
965 }
966
967 INTERNAL_FUNC int emcore_backup_accounts(char *multi_user_name, const char *file_path, int *error_code)
968 {
969         EM_DEBUG_LOG("file_path [%s], error_code [%p]", file_path, error_code);
970         int local_error_code = EMAIL_ERROR_NONE, local_error_code_2 = EMAIL_ERROR_NONE, ret_code = false;
971         int select_num, i, target_buff_length = 0;
972         int normal_email_account_count = 0;
973         char *target_buffer = NULL;
974         unsigned char *output_data = NULL;
975         emstorage_account_tbl_t *account_list = NULL;
976         int fd = -1;
977
978         if (!file_path) {
979                 local_error_code = EMAIL_ERROR_INVALID_PARAM;
980                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
981                 goto FINISH_OFF;
982         }
983
984         select_num = 1000;
985
986         if (!emstorage_get_account_list(multi_user_name, &select_num, &account_list, true, true, &local_error_code)) {
987                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", local_error_code);
988                 goto FINISH_OFF;
989         }
990
991         EM_DEBUG_LOG("select_num [%d]", select_num);
992
993         if (account_list) {
994                 for (i = 0; i < select_num; i++) {
995                         if (account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_POP3 || account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)
996                                 normal_email_account_count++;
997                 }
998         }
999
1000         EM_DEBUG_LOG("normal_email_account_count [%d]", normal_email_account_count);
1001
1002         if (normal_email_account_count == 0) {
1003                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_ACCOUNT_NOT_FOUND");
1004                 local_error_code = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
1005                 goto FINISH_OFF;
1006         } else {
1007                 target_buffer = em_malloc(sizeof(int));
1008                 if (!target_buffer)  {
1009                         EM_DEBUG_EXCEPTION("malloc failed");
1010                         local_error_code = EMAIL_ERROR_OUT_OF_MEMORY;
1011                         goto FINISH_OFF;
1012                 }
1013
1014                 memcpy(target_buffer, (char *)&normal_email_account_count, sizeof(int));
1015                 target_buff_length = sizeof(int);
1016
1017                 for (i = 0; i < select_num; i++) {
1018                         if (account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_POP3 || account_list[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1019                                 if (!emcore_write_account_into_buffer(&target_buffer, &target_buff_length, account_list + i, &local_error_code)) {
1020                                         EM_DEBUG_EXCEPTION("emcore_write_account_into_buffer failed [%d]", local_error_code);
1021                                         goto FINISH_OFF;
1022                                 }
1023                         }
1024                 }
1025
1026                 EM_DEBUG_LOG("target_buff_length [%d]", target_buff_length);
1027
1028                 local_error_code = em_open(file_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR, &fd);
1029                 if (local_error_code != EMAIL_ERROR_NONE) {
1030                                 EM_DEBUG_EXCEPTION("em_open failed - %d [%s] : ", local_error_code, file_path);
1031                                 goto FINISH_OFF;
1032                 }
1033
1034
1035                 int tmp = emcore_check_key_in_key_manager(EMAIL_SERVICE_BNR_ACCOUNT_KEY);
1036
1037                         if (tmp == EMAIL_ERROR_KEY_NOT_FOUND){
1038                                         EM_DEBUG_LOG("Key not exist");
1039
1040                                         int ret = 0;
1041
1042                                         ret = emcore_save_key_in_key_manager(EMAIL_SERVICE_BNR_ACCOUNT_KEY);
1043
1044                                         if(ret != EMAIL_ERROR_NONE){
1045                                                 EM_DEBUG_EXCEPTION("save key failed %d", ret);
1046                                                 goto FINISH_OFF;
1047                                         }
1048
1049                         } else if (tmp == EMAIL_ERROR_INVALID_PARAM) {
1050
1051                                 EM_DEBUG_EXCEPTION("Invalid parameter");
1052                                 goto FINISH_OFF;
1053
1054                         }
1055                 int output_length = 0;
1056                 emcore_encryption_data_in_key_manager(EMAIL_SERVICE_BNR_ACCOUNT_KEY, target_buffer, target_buff_length, &output_data, &output_length);
1057
1058                 EM_DEBUG_LOG("output length %d", output_length);
1059                 int write_length = write(fd, (char *)output_data, output_length);
1060                 EM_DEBUG_LOG("write length : %d", write_length);
1061
1062         }
1063
1064         ret_code = true;
1065 FINISH_OFF:
1066
1067         EM_SAFE_CLOSE(fd);
1068         EM_SAFE_FREE(target_buffer);
1069         EM_SAFE_FREE(output_data);
1070
1071         if (account_list)
1072                 emstorage_free_account(&account_list, select_num, &local_error_code_2);
1073
1074         if (error_code)
1075                 *error_code = local_error_code;
1076
1077         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1078         return ret_code;
1079 }
1080
1081 INTERNAL_FUNC int emcore_restore_accounts(char *multi_user_name, const char *file_path)
1082 {
1083         EM_DEBUG_FUNC_BEGIN_SEC("file_path [%s]", file_path);
1084         int err = EMAIL_ERROR_NONE;
1085         int account_count = 0, i = 0, account_stream_length = 0;
1086         char *temp_buffer = NULL, *account_stream = NULL, *buffer_ptr = NULL;
1087         email_account_t temp_account = {0};
1088         email_account_t *account_list = NULL;
1089         char *encrypt_buffer = NULL;
1090
1091
1092         if (!emcore_notify_storage_event(NOTI_ACCOUNT_RESTORE_START, 0, 0, NULL, 0))
1093                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_RESTORE_START] failed");
1094
1095         if (!file_path) {
1096                 err = EMAIL_ERROR_INVALID_PARAM;
1097                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1098                 goto FINISH_OFF;
1099         }
1100
1101         if ((err = emcore_get_account_reference_list(multi_user_name, &account_list, &account_count)) == EMAIL_ERROR_NONE) {
1102                 for (i = 0; i < account_count; i++) {
1103                         if (account_list[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
1104                                 if (!emcore_delete_account(multi_user_name, account_list[i].account_id, true, &err)) {
1105                                         EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
1106                                         goto FINISH_OFF;
1107                                 }
1108                         }
1109                 }
1110
1111                 if (account_list) {
1112                         emcore_free_account_list(&account_list, account_count, NULL);
1113                         account_list = NULL;
1114                 }
1115         }
1116
1117         int output_length = 0;
1118         char *output_data = NULL;
1119
1120         emcore_decryption_data_from_file_in_key_manager(EMAIL_SERVICE_BNR_ACCOUNT_KEY, file_path, &output_data, &output_length);
1121
1122         if (output_length > 0) {
1123                 memcpy((void *)&account_count, output_data, sizeof(int));
1124                 buffer_ptr = output_data + sizeof(int);
1125
1126                 EM_DEBUG_LOG("account_count[%d]", account_count);
1127
1128                 for (i = 0; i < account_count; i++) {
1129                         memcpy((void *)&account_stream_length, buffer_ptr, sizeof(int));
1130                         buffer_ptr += sizeof(int);
1131                         EM_DEBUG_LOG("account_stream_length [%d]", account_stream_length);
1132                         if (account_stream_length) {
1133                                 account_stream = em_malloc(account_stream_length);
1134                                 if (!account_stream) {
1135                                         EM_DEBUG_EXCEPTION("em_malloc() failed.");
1136                                         err = EMAIL_ERROR_OUT_OF_MEMORY ;
1137                                         goto FINISH_OFF;
1138                                 }
1139                                 memcpy(account_stream, buffer_ptr, account_stream_length);
1140
1141                                 em_convert_byte_stream_to_account(account_stream, account_stream_length, &temp_account);
1142                                 EM_SAFE_FREE(account_stream);
1143
1144
1145
1146                                 if ((emstorage_check_duplicated_account(multi_user_name,
1147                                                                                                                 &temp_account,
1148                                                                                                                 true,
1149                                                                                                                 &err) == false) && (err = EMAIL_ERROR_ALREADY_EXISTS)) {
1150                                         EM_DEBUG_EXCEPTION("An account is duplicated. ");
1151                                         buffer_ptr += account_stream_length;
1152                                         account_stream_length = 0;
1153                                         emcore_free_account(&temp_account);
1154                                         err = EMAIL_ERROR_NONE;
1155                                         continue;
1156                                 }
1157
1158                                 if (!emcore_create_account(multi_user_name, &temp_account, true, &err)) {
1159                                         EM_DEBUG_EXCEPTION("emcore_create_account() failed. [%d]", err);
1160                                         goto FINISH_OFF;
1161                                 }
1162
1163                                 emcore_free_account(&temp_account);
1164                         }
1165                         buffer_ptr += account_stream_length;
1166                         account_stream_length = 0;
1167                 }
1168         } else {
1169                 EM_DEBUG_EXCEPTION("ssm_read() failed.");
1170                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1171                 goto FINISH_OFF;
1172         }
1173
1174 FINISH_OFF:
1175 #ifdef FEATURE_SSS_ENABLE
1176         EM_SAFE_CLOSE(fd);
1177 #endif
1178
1179         if (err == EMAIL_ERROR_NONE) {
1180                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_RESTORE_FINISH, 0, 0, NULL, err))
1181                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_RESTORE_FINISH] failed");
1182         } else {
1183                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_RESTORE_FAIL, 0, 0, NULL, err))
1184                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_RESTORE_FAIL] failed");
1185         }
1186
1187         if (account_list) {
1188                 emcore_free_account_list(&account_list, account_count, NULL);
1189                 account_list = NULL;
1190         }
1191         emcore_free_account(&temp_account);
1192         EM_SAFE_FREE(account_stream);
1193         EM_SAFE_FREE(temp_buffer);
1194         EM_SAFE_FREE(encrypt_buffer);
1195         EM_SAFE_FREE(output_data);
1196
1197         EM_DEBUG_FUNC_END("err [%d]", err);
1198         return err;
1199 }
1200
1201 #endif /*  __FEATURE_BACKUP_ACCOUNT_ */
1202
1203 INTERNAL_FUNC int emcore_save_default_account_id(char *multi_user_name, int input_account_id)
1204 {
1205         EM_DEBUG_FUNC_BEGIN("account_id [%d]", input_account_id);
1206         int ret_code = EMAIL_ERROR_NONE, result_value = 0;
1207
1208         result_value = vconf_set_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, input_account_id);
1209         if (result_value < 0) {
1210                 EM_DEBUG_EXCEPTION("vconf_set_int failed [%d]", result_value);
1211                 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1212         }
1213
1214         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1215         return ret_code;
1216 }
1217
1218 static int _recover_from_invalid_default_account_id(char *multi_user_name, int *output_account_id)
1219 {
1220         EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1221         int ret_code = EMAIL_ERROR_NONE;
1222         int account_count = 100;
1223         emstorage_account_tbl_t *result_account_list = NULL;
1224
1225         if (output_account_id == NULL) {
1226                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1227                 goto FINISH_OFF;
1228         }
1229
1230         if (!emstorage_get_account_list(multi_user_name, &account_count, &result_account_list, false, false, &ret_code) || !result_account_list) {
1231                 if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
1232                         EM_DEBUG_LOG("no account found");
1233                 else
1234                         EM_DEBUG_EXCEPTION("emstorage_get_account_list() failed [%d]", ret_code);
1235                 *output_account_id = 0;
1236                 goto FINISH_OFF;
1237         }
1238
1239         if (account_count > 0) {
1240                 *output_account_id = result_account_list[0].account_id;
1241         }
1242
1243         EM_DEBUG_LOG_DEV("output_account_id [%d]", *output_account_id);
1244
1245 FINISH_OFF:
1246
1247         if (result_account_list)
1248                 emstorage_free_account(&result_account_list, account_count, NULL);
1249
1250         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1251         return ret_code;
1252 }
1253
1254 INTERNAL_FUNC int emcore_load_default_account_id(char *multi_user_name, int *output_account_id)
1255 {
1256         EM_DEBUG_FUNC_BEGIN("account_id [%p]", output_account_id);
1257         int ret_code = EMAIL_ERROR_NONE;
1258         int result_value = 0;
1259         emstorage_account_tbl_t *result_account = NULL;
1260
1261         if (output_account_id == NULL) {
1262                 ret_code = EMAIL_ERROR_INVALID_PARAM;
1263                 goto FINISH_OFF;
1264         }
1265
1266         result_value = vconf_get_int(VCONF_KEY_DEFAULT_ACCOUNT_ID, output_account_id);
1267
1268         if (result_value < 0) {
1269                 EM_DEBUG_EXCEPTION("vconf_get_int() failed [%d]", result_value);
1270                 ret_code = EMAIL_ERROR_SYSTEM_FAILURE;
1271                 *output_account_id = 0;
1272         }
1273
1274         if (*output_account_id != 0) {
1275                 if (!emstorage_get_account_by_id(multi_user_name, *output_account_id, EMAIL_ACC_GET_OPT_DEFAULT, &result_account, false, &ret_code)) {
1276                         if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND) {
1277                                 EM_DEBUG_LOG("no account found account_id[%d]", *output_account_id);
1278                                 *output_account_id = 0;
1279                         } else {
1280                                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id() failed account_id[%d] err[%d]", *output_account_id, ret_code);
1281                                 goto FINISH_OFF;
1282                         }
1283                 }
1284         }
1285
1286         if (*output_account_id == 0) {
1287                 if ((ret_code = _recover_from_invalid_default_account_id(multi_user_name, output_account_id)) != EMAIL_ERROR_NONE) {
1288                         if (ret_code == EMAIL_ERROR_ACCOUNT_NOT_FOUND)
1289                                 EM_DEBUG_LOG("no account found");
1290                         else
1291                                 EM_DEBUG_EXCEPTION("_recover_from_invalid_default_account() failed [%d]", ret_code);
1292                         *output_account_id = 0;
1293                 }
1294         }
1295
1296 FINISH_OFF:
1297         if (result_account)
1298                 emstorage_free_account(&result_account, 1, NULL);
1299
1300         EM_DEBUG_FUNC_END("ret_code [%d]", ret_code);
1301         return ret_code;
1302 }
1303
1304 INTERNAL_FUNC int emcore_recover_from_secured_storage_failure(char *multi_user_name)
1305 {
1306         EM_DEBUG_FUNC_BEGIN();
1307         int err = EMAIL_ERROR_NONE;
1308         int i = 0;
1309         int account_count = 50;
1310         emstorage_account_tbl_t *temp_account_tbl_list = NULL;
1311         emstorage_account_tbl_t *temp_account_tbl      = NULL;
1312
1313         if (!emstorage_get_account_list(multi_user_name, &account_count, &temp_account_tbl_list, true, false, &err)) {
1314                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
1315                 goto FINISH_OFF;
1316         }
1317
1318         for (i = 0; i < account_count; i++) {
1319                 if (!emstorage_get_account_by_id(multi_user_name, temp_account_tbl_list[i].account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_PASSWORD, &temp_account_tbl, true, &err)) {
1320                         if (err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
1321                                 if (!emcore_delete_account(multi_user_name, temp_account_tbl_list[i].account_id, true, &err)) {
1322                                         EM_DEBUG_EXCEPTION("emcore_delete_account failed [%d]", err);
1323                                         goto FINISH_OFF;
1324                                 }
1325                         }
1326                 }
1327         }
1328
1329 FINISH_OFF:
1330
1331         emstorage_free_account(&temp_account_tbl_list, account_count, NULL);
1332         emstorage_free_account(&temp_account_tbl, 1, NULL);
1333
1334         EM_DEBUG_FUNC_END("err [%d]", err);
1335         return err;
1336 }
1337
1338 INTERNAL_FUNC int emcore_update_sync_status_of_account(char *multi_user_name, int input_account_id, email_set_type_t input_set_operator, int input_sync_status)
1339 {
1340         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_set_operator [%d], input_sync_status [%d]", input_account_id, input_set_operator, input_sync_status);
1341         int err = EMAIL_ERROR_NONE;
1342
1343 #ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__
1344         int err_from_account_svc = 0;
1345         emstorage_account_tbl_t *account_tbl_data = NULL;
1346         void *join_zone = NULL;
1347
1348         if (input_account_id != ALL_ACCOUNT && (input_sync_status == SYNC_STATUS_SYNCING)) {
1349                 if (!emstorage_get_account_by_id(multi_user_name, input_account_id, EMAIL_ACC_GET_OPT_DEFAULT | EMAIL_ACC_GET_OPT_OPTIONS, &account_tbl_data, true, &err)) {
1350                         EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
1351                         goto FINISH_OFF;
1352                 }
1353
1354                 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1355                         err = emcore_set_join_zone(multi_user_name, &join_zone);
1356                         if (err != EMAIL_ERROR_NONE) {
1357                                 EM_DEBUG_EXCEPTION("emcore_set_join_zone failed : [%d]", err);
1358                                 goto FINISH_OFF;
1359                         }
1360                 }
1361
1362                 if (input_set_operator == SET_TYPE_SET)
1363                         err_from_account_svc = account_update_sync_status_by_id(account_tbl_data->account_svc_id, ACCOUNT_SYNC_STATUS_RUNNING);
1364                 else if (input_set_operator == SET_TYPE_MINUS)
1365                         err_from_account_svc = account_update_sync_status_by_id(account_tbl_data->account_svc_id, ACCOUNT_SYNC_STATUS_IDLE);
1366
1367                 EM_DEBUG_LOG("account_update_sync_status_by_id returns [%d] by id[%d]", err_from_account_svc, account_tbl_data->account_svc_id);
1368
1369                 emcore_unset_join_zone(join_zone);
1370         }
1371 #endif /* __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__ */
1372
1373         if (!emstorage_update_sync_status_of_account(multi_user_name, input_account_id, input_set_operator, input_sync_status, true, &err))
1374                 EM_DEBUG_EXCEPTION("emstorage_update_sync_status_of_account failed [%d]", err);
1375
1376 #ifdef __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__
1377 FINISH_OFF:
1378         if (account_tbl_data)
1379                 emstorage_free_account(&account_tbl_data, 1, NULL);
1380 #endif /* __FEATURE_USING_ACCOUNT_SVC_FOR_SYNC_STATUS__ */
1381
1382         EM_DEBUG_FUNC_END("err [%d]", err);
1383         return err;
1384 }
1385
1386 #define GOOGLE_PIM_SYNC_GET_TOKEN_URL "https://accounts.google.com/o/oauth2/token"
1387 #define GOOGLE_PIM_SYNC_CLIENT_ID     "233916860221.apps.googleusercontent.com"
1388 #define GOOGLE_PIM_SYNC_CLIENT_SECRET "dI8wRLGMJ16Ioxfl8KaujmuU"
1389 #define CALCURL_TIMEOUT               30
1390
1391 int emcore_curl_progress_callback(void *p, double dtot, double dnow, double utot, double unow)
1392 {
1393         EM_DEBUG_LOG("%0.0lf %0.0lf %0.0lf %0.0lf", dtot, dnow, utot, unow);
1394         return 0;
1395 }
1396
1397 size_t emcore_curl_write_callback(char* data, size_t size, size_t nmemb, void* target)
1398 {
1399         EM_DEBUG_FUNC_BEGIN("data[%p] size[%d] nmemb[%d] target[%p]", data, size, nmemb, target);
1400         int last_pos = 0;
1401         char **result_string = (char**)target;
1402         size_t received_length = size * nmemb;
1403
1404         if (*result_string == NULL) {
1405                 EM_DEBUG_LOG("received_length[%d]", received_length);
1406                 *result_string = em_malloc(received_length + 1);
1407         } else {
1408                 int new_buffer_length = 0;
1409                 last_pos = EM_SAFE_STRLEN(*result_string);
1410                 new_buffer_length = last_pos + received_length + 1;
1411                 EM_DEBUG_LOG("new_buffer_length[%d]", new_buffer_length);
1412                 *result_string = realloc(*result_string, new_buffer_length);
1413         }
1414
1415         if (*result_string == NULL) {
1416                 EM_DEBUG_EXCEPTION("memory allocation failed");
1417                 received_length = 0;
1418                 goto FINISH_OFF;
1419         }
1420
1421         memcpy(*result_string + last_pos, data, received_length);
1422         /*EM_DEBUG_LOG_SEC("result_string[%s]", *result_string);*/
1423
1424 FINISH_OFF:
1425         EM_DEBUG_FUNC_END("received_length [%d]", received_length);
1426         return received_length;
1427 }
1428
1429 static int emcore_get_xoauth2_access_token(char *input_refresh_token, char **output_access_token)
1430 {
1431         EM_DEBUG_FUNC_BEGIN("input_refresh_token [%p] output_access_token [%p]", input_refresh_token, output_access_token);
1432         int err = EMAIL_ERROR_NONE;
1433
1434         CURL *curl = NULL;
1435         CURLcode curl_ressult_code;
1436         char *result_string = NULL;
1437         char *pos = NULL;
1438         char *savepos = NULL;
1439         char *access_token = NULL;
1440         long response_code;
1441         double speed_upload, total_time, download_size;
1442
1443         if (input_refresh_token == NULL || output_access_token == NULL) {
1444                 EM_DEBUG_EXCEPTION("curl_easy_init() failed");
1445                 err = EMAIL_ERROR_INVALID_PARAM;
1446                 goto FINISH_OFF;
1447         }
1448
1449         curl = curl_easy_init();
1450         if (NULL == curl) {
1451                 EM_DEBUG_EXCEPTION("curl_easy_init() failed");
1452                 err = EMAIL_ERROR_SYSTEM_FAILURE;
1453                 goto FINISH_OFF;
1454         }
1455
1456         char buf[1024] = {0};
1457         snprintf(buf, sizeof(buf),
1458                                 "client_id=%s&"
1459                                 "client_secret=%s&"
1460                                 "refresh_token=%s&"
1461                                 "grant_type=refresh_token",
1462                                 GOOGLE_PIM_SYNC_CLIENT_ID,
1463                                 GOOGLE_PIM_SYNC_CLIENT_SECRET,
1464                                 input_refresh_token);
1465         curl_easy_setopt(curl, CURLOPT_URL, GOOGLE_PIM_SYNC_GET_TOKEN_URL);
1466         curl_easy_setopt(curl, CURLOPT_POST, 1);
1467         curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);
1468         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1469         curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CALCURL_TIMEOUT);
1470         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
1471         curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);
1472         curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, emcore_curl_progress_callback);
1473         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, emcore_curl_write_callback);
1474         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&result_string);
1475         curl_easy_setopt(curl, CURLOPT_HEADER, true);
1476
1477         /* pinning */
1478         curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, tpkp_curl_ssl_ctx_callback);
1479
1480         curl_ressult_code = curl_easy_perform(curl);
1481
1482         EM_DEBUG_LOG_SEC("CURLcode: %d (%s)", curl_ressult_code, curl_easy_strerror(curl_ressult_code));
1483
1484         if (0 == curl_ressult_code) {
1485                 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
1486                 curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD,  &speed_upload);
1487                 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME,    &total_time);
1488                 curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size);
1489
1490                 EM_DEBUG_LOG("Speed: %.3f bytes/sec during %.3f seconds", speed_upload, total_time);
1491                 EM_DEBUG_LOG("response code = %ld , length = %.3f", response_code, download_size);
1492         }
1493
1494         if (result_string == NULL) {
1495                 EM_DEBUG_EXCEPTION("Getting result string failed");
1496                 err = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
1497                 goto FINISH_OFF;
1498         }
1499
1500         if (strstr(result_string, "error")) {
1501                 if (strstr(result_string, "\"invalid_grant\""))
1502                         err = EMAIL_ERROR_XOAUTH_INVALID_GRANT;
1503                 else
1504                         err = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
1505                 goto FINISH_OFF;
1506         }
1507
1508         pos = strstr(result_string, "\"access_token\" : \"");
1509
1510         if (!pos) {
1511                 err = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
1512                 goto FINISH_OFF;
1513         }
1514
1515         pos = pos + strlen("\"access_token\" : \"");
1516
1517         access_token = strtok_r(pos, "\"", &savepos);
1518
1519         if (access_token) {
1520                 *output_access_token = EM_SAFE_STRDUP(access_token);
1521
1522                 if (*output_access_token == NULL) {
1523                         EM_DEBUG_EXCEPTION("strdup failed");
1524                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1525                         goto FINISH_OFF;
1526                 }
1527         }
1528
1529 FINISH_OFF:
1530
1531         EM_SAFE_FREE(result_string);
1532
1533         if (curl)
1534                 curl_easy_cleanup(curl);
1535
1536         tpkp_curl_cleanup();
1537
1538         EM_DEBUG_FUNC_END("err [%d]", err);
1539         return err;
1540 }
1541
1542 INTERNAL_FUNC int emcore_refresh_xoauth2_access_token(char *multi_user_name, int input_account_id)
1543 {
1544         EM_DEBUG_FUNC_BEGIN("input_account_id [%d]", input_account_id);
1545         int err = EMAIL_ERROR_NONE;
1546         int new_password_string_length = 0;
1547         char *access_token = NULL;
1548         char *refresh_token = NULL;
1549         char *new_access_token = NULL;
1550         char *new_password_string = NULL;
1551         char *saveptr = NULL;
1552         email_account_t *ref_account = NULL;
1553
1554
1555         ref_account = emcore_get_account_reference(multi_user_name, input_account_id, true);
1556         if (ref_account == NULL) {
1557                 EM_DEBUG_EXCEPTION("emcore_get_account_reference() failed");
1558                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1559                 goto FINISH_OFF;
1560         }
1561
1562         access_token = EM_SAFE_STRDUP(strtok_r(ref_account->incoming_server_password, "\001", &saveptr));
1563         if (access_token == NULL) {
1564                 EM_DEBUG_EXCEPTION("invalid token string.");
1565                 err = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
1566                 goto FINISH_OFF;
1567         }
1568
1569         EM_DEBUG_LOG_SEC("access_token [%s]", access_token);
1570
1571         refresh_token = EM_SAFE_STRDUP(strtok_r(NULL, "\001", &saveptr));
1572
1573         if (refresh_token == NULL) {
1574                 EM_DEBUG_EXCEPTION("invalid token string.");
1575                 err = EMAIL_ERROR_XOAUTH_BAD_REQUEST;
1576                 goto FINISH_OFF;
1577         }
1578
1579         EM_DEBUG_LOG_SEC("refresh_token [%s]", refresh_token);
1580
1581         if ((err = emcore_get_xoauth2_access_token(refresh_token, &new_access_token)) != EMAIL_ERROR_NONE) {
1582                 EM_DEBUG_EXCEPTION("emcore_get_xoauth2_access_token() failed [%d]", err);
1583                 goto FINISH_OFF;
1584         }
1585
1586         new_password_string_length = EM_SAFE_STRLEN(new_access_token) + EM_SAFE_STRLEN(refresh_token) + 2;
1587
1588         new_password_string = em_malloc(new_password_string_length);
1589         if (new_password_string == NULL) {
1590                 EM_DEBUG_EXCEPTION("em_malloc() failed");
1591                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1592                 goto FINISH_OFF;
1593         }
1594
1595         SNPRINTF(new_password_string, new_password_string_length, "%s\001%s", new_access_token, refresh_token);
1596
1597         if ((err = emstorage_update_account_password(multi_user_name, input_account_id, new_password_string, new_password_string)) != EMAIL_ERROR_NONE) {
1598                 EM_DEBUG_EXCEPTION("emstorage_update_account_password() failed [%d]", err);
1599                 goto FINISH_OFF;
1600         }
1601
1602 FINISH_OFF:
1603
1604         EM_SAFE_FREE(access_token);
1605         EM_SAFE_FREE(refresh_token);
1606         EM_SAFE_FREE(new_access_token);
1607         EM_SAFE_FREE(new_password_string);
1608
1609         if (ref_account) {
1610                 emcore_free_account(ref_account);
1611                 EM_SAFE_FREE(ref_account);
1612         }
1613
1614         EM_DEBUG_FUNC_END("err [%d]", err);
1615         return err;
1616 }