-INTERNAL_FUNC int emstorage_add_certificate(char *multi_user_name, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
-
- if (!certificate) {
- EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
- if (err_code)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- int rc = -1, ret = false;
- int error = EMAIL_ERROR_NONE;
- DB_STMT hStmt = NULL;
- char sql_query_string[QUERY_SIZE] = {0, };
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-#endif
-
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
-
- EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
-
- char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
- char **result = NULL;
-
- /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
- EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
- ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
-
- if (NULL==result[1]) rc = 1;
- else rc = atoi(result[1])+1;
- sqlite3_free_table(result);
-
- certificate->certificate_id = rc;
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
- goto FINISH_OFF;
- }
-#endif
- SNPRINTF(sql_query_string, sizeof(sql_query_string),
- "INSERT INTO mail_certificate_tbl VALUES "
- "( "
- " ? " /* Index of certificate */
- " , ? " /* Select the account */
- " , ? " /* Year of issue */
- " , ? " /* Month of issue */
- " , ? " /* Day of issue */
- " , ? " /* Year of expiration */
- " , ? " /* Month of expiration */
- " , ? " /* Day of expiration */
- " , ? " /* Organization of issue */
- " , ? " /* Email address */
- " , ? " /* Subject of certificate */
- " , ? " /* Name of saved certificate */
- ") ");
-
-
- /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
- EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
- EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
- _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
- _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
- _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
- _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
- _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
- _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
- _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
- _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
- /* rc = sqlite3_step(hStmt); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
-
- EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
-
- ret = true;
-
-FINISH_OFF:
- EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
- if (hStmt != NULL) {
- rc = sqlite3_finalize(hStmt);
- if (rc != SQLITE_OK) {
- EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
- error = EMAIL_ERROR_DB_FAILURE;
- }
- }
-
- if (error == EMAIL_ERROR_NONE) {
- if (!emcore_notify_storage_event (NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
- EM_DEBUG_EXCEPTION ("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
- }
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
-
- int ret = false;
- int error = EMAIL_ERROR_NONE;
-
- if (count > 0) {
- if (!certificate_list || !*certificate_list) {
- EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
- error = EMAIL_ERROR_INVALID_PARAM;
- goto FINISH_OFF;
- }
-
- emstorage_certificate_tbl_t *p = *certificate_list;
- int i = 0;
-
- for (; i < count; i++) {
- EM_SAFE_FREE(p[i].issue_organization_name);
- EM_SAFE_FREE(p[i].email_address);
- EM_SAFE_FREE(p[i].subject_str);
- EM_SAFE_FREE(p[i].filepath);
- EM_SAFE_FREE(p[i].password);
- }
-
- EM_SAFE_FREE(p);
- *certificate_list = NULL;
- }
-
- ret = true;
-
-FINISH_OFF:
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_get_certificate_list(char *multi_user_name, int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN();
-
- int i = 0, count = 0, rc = -1, ret = false;
- int error = EMAIL_ERROR_NONE;
- emstorage_certificate_tbl_t *p_data_tbl = NULL;
-
- DB_STMT hStmt = NULL;
-
- if (!select_num || !certificate_list) {
- EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
- if (err_code != NULL)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
- EMSTORAGE_START_READ_TRANSACTION(transaction);
-
- char sql_query_string[QUERY_SIZE] = {0, };
- char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
- char **result;
-
- /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
- EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
- ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
-
- count = atoi(result[1]);
- sqlite3_free_table(result);
-
- if (!count) {
- EM_DEBUG_EXCEPTION("no account found...");
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- ret = true;
- goto FINISH_OFF;
- }
- EM_DEBUG_LOG("count = %d", rc);
- SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
-
- /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
-
- EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
- EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
- /* rc = sqlite3_step(hStmt); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
- if (rc == SQLITE_DONE) {
- EM_DEBUG_EXCEPTION("no account found...");
-
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- count = 0;
- ret = true;
- goto FINISH_OFF;
- }
-
- if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
- EM_DEBUG_EXCEPTION("malloc failed...");
- error = EMAIL_ERROR_OUT_OF_MEMORY;
- goto FINISH_OFF;
- }
- memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
- for (i = 0; i < count; i++) {
- /* get recordset */
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
- if (with_password == true) {
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- /* get password from the secure storage */
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-
- EM_SAFE_FREE(p_data_tbl[i].password);
-
- /* get password file name */
- if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
- goto FINISH_OFF;
- }
-
- /* read password from secure storage */
- if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
- EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
- goto FINISH_OFF;
- }
- EM_DEBUG_LOG_SEC("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
-#endif
- }
-
- /* rc = sqlite3_step(hStmt); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
- EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
- }
-
- ret = true;
-
-FINISH_OFF:
- if (ret == true) {
- *certificate_list = p_data_tbl;
- *select_num = count;
- EM_DEBUG_LOG("COUNT : %d", count);
- }
- else if (p_data_tbl != NULL)
- emstorage_free_certificate(&p_data_tbl, count, NULL);
- if (hStmt != NULL) {
- rc = sqlite3_finalize(hStmt);
- hStmt = NULL;
- if (rc != SQLITE_OK) {
- EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
- error = EMAIL_ERROR_DB_FAILURE;
- }
- }
-
- EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *multi_user_name, char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN_SEC("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
-
- if (!certificate) {
- EM_DEBUG_EXCEPTION_SEC("email_address[%s], certificate[%p]", email_address, certificate);
- if (err_code != NULL)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- int ret = false;
- int error = EMAIL_ERROR_NONE;
- emstorage_certificate_tbl_t *p_data_tbl = NULL;
- DB_STMT hStmt = NULL;
- char sql_query_string[QUERY_SIZE] = {0, };
- int rc = -1;
- int sql_len = 0;
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-#endif
-
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
- EMSTORAGE_START_READ_TRANSACTION(transaction);
-
- /* Make query string */
- SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
- sql_len = EM_SAFE_STRLEN(sql_query_string);
-
- /* dummy value, FROM WHERE clause */
- SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
-
- /* FROM clause */
- EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
-
- /* execute a sql and count rows */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
- EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
-
- if (rc == SQLITE_DONE) {
- EM_DEBUG_EXCEPTION("no matched certificate found...");
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- goto FINISH_OFF;
- }
-
- /* Assign query result to structure */
- if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
- EM_DEBUG_EXCEPTION("malloc failed...");
- error = EMAIL_ERROR_OUT_OF_MEMORY;
- goto FINISH_OFF;
- }
-
- memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
-
- if (with_password) {
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- /* get password file name */
- if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
- goto FINISH_OFF;
- }
-
- /* read password from secure storage */
- if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
- goto FINISH_OFF;
- }
- EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
-#endif
- }
- ret = true;
-
-FINISH_OFF:
- if (ret == true)
- *certificate = p_data_tbl;
-
- if (hStmt != NULL) {
- rc = sqlite3_finalize(hStmt);
- if (rc != SQLITE_OK) {
- EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
- error = EMAIL_ERROR_DB_FAILURE;
- }
- }
-
- EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_get_certificate_by_index(char *multi_user_name, int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
-
- if (!certificate) {
- EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
- if (err_code != NULL)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- int ret = false;
- int error = EMAIL_ERROR_NONE;
- emstorage_certificate_tbl_t *p_data_tbl = NULL;
- DB_STMT hStmt = NULL;
- char sql_query_string[QUERY_SIZE] = {0, };
- int rc = -1;
- int sql_len = 0;
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-#endif
-
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
- EMSTORAGE_START_READ_TRANSACTION(transaction);
-
- /* Make query string */
- SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
- sql_len = EM_SAFE_STRLEN(sql_query_string);
-
- /* dummy value, FROM WHERE clause */
- SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
-
- /* FROM clause */
- EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
-
- /* execute a sql and count rows */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
- EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
-
- if (rc == SQLITE_DONE) {
- EM_DEBUG_EXCEPTION("no matched certificate found...");
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- goto FINISH_OFF;
- }
-
- /* Assign query result to structure */
- if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
- EM_DEBUG_EXCEPTION("malloc failed...");
- error = EMAIL_ERROR_OUT_OF_MEMORY;
- goto FINISH_OFF;
- }
-
- memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
-
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
- _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
-
- if (with_password) {
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- /* get password file name */
- if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
- goto FINISH_OFF;
- }
-
- /* read password from secure storage */
- if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
- goto FINISH_OFF;
- }
- EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
-#endif
- }
- ret = true;
-
-FINISH_OFF:
- if (ret == true)
- *certificate = p_data_tbl;
-
- if (hStmt != NULL) {
- rc = sqlite3_finalize(hStmt);
- if (rc != SQLITE_OK) {
- EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
- error = EMAIL_ERROR_DB_FAILURE;
- }
- }
-
- EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_delete_certificate(char *multi_user_name, int certificate_id, int transaction, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
-
- if (certificate_id < 1) {
- EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
- if (err_code)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- int rc = -1, ret = false;
- int error = EMAIL_ERROR_NONE;
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
- EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
-
- /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
- char sql_query_string[QUERY_SIZE] = {0, };
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
- /* get password file name */
- if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
- goto FINISH_OFF;
- }
-#endif
- /* delete from db */
- memset(sql_query_string, 0x00, sizeof(sql_query_string));
- SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
- error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
- if (error != EMAIL_ERROR_NONE) {
- EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
- goto FINISH_OFF;
- }
-
- /* validate account existence */
- rc = sqlite3_changes(local_db_handle);
- if (rc == 0) {
- EM_DEBUG_EXCEPTION(" no matched certificate found...");
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- goto FINISH_OFF;
- }
-
- ret = true;
-
-FINISH_OFF:
- EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-
-INTERNAL_FUNC int emstorage_update_certificate(char *multi_user_name, int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
-{
- EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
-
- if (certificate_id < 1) {
- EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
- if (err_code)
- *err_code = EMAIL_ERROR_INVALID_PARAM;
- return false;
- }
-
- int error = EMAIL_ERROR_NONE;
- int rc, ret = false;
-
- DB_STMT hStmt = NULL;
- char sql_query_string[QUERY_SIZE] = {0, };
-#ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
- char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
-#endif
-
- sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
- EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
-
- SNPRINTF(sql_query_string, sizeof(sql_query_string),
- "UPDATE mail_certificate_tbl SET"
- " issue_year = ?"
- ", issue_month = ?" /* Index of certificate */
- ", issue_day = ?" /* Select the account */
- ", expiration_year = ?" /* Year of issue */
- ", expiration_month = ?" /* Month of issue */
- ", expiration_day = ?" /* Day of issue */
- ", issue_organization_name = ?" /* Year of expiration */
- ", email_address = ?" /* Month of expiration */
- ", subject_str = ?" /* Day of expiration */
- ", filepath = ?" /* Organization of issue */
- ", password = ?"
- " WHERE certificate_id = ?");
-
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
- EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
- EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
-
- _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
- _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
- _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
- _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
- _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
- _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
- _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
- _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
-
- /* rc = sqlite3_step(hStmt); */
- EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
- EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
- EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
- ("sqlite3_step fail:%d", rc));
-
- /* validate account existence */
- rc = sqlite3_changes(local_db_handle);
- if (rc == 0) {
- EM_DEBUG_EXCEPTION(" no matched account found...");
-
- error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
- goto FINISH_OFF;
- }
-
- ret = true;
-
-FINISH_OFF:
- EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
-
- if (hStmt != NULL) {
- rc = sqlite3_finalize(hStmt);
- if (rc != SQLITE_OK) {
- EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
- error = EMAIL_ERROR_DB_FAILURE;
- }
- }
-
- if (error == EMAIL_ERROR_NONE) {
- if (!emcore_notify_storage_event (NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
- EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
- }
-
- if (err_code != NULL)
- *err_code = error;
-
- EM_DEBUG_FUNC_END("ret [%d]", ret);
- return ret;
-}
-