81b0759df7533cbc5fda6a42f4d17e961c8cbaae
[platform/core/pim/contacts-service.git] / server / ctsvc_server.c
1 /*
2  * Contact Service
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <pims-ipc.h>
27 #include <pims-ipc-svc.h>
28 #include <unistd.h>
29 #include <grp.h>
30
31 #include "ctsvc_internal.h"
32 #include "ctsvc_db_init.h"
33 #include "ctsvc_db_schema.h"
34 #include "ctsvc_schema_recovery.h"
35 #include "ctsvc_server_socket.h"
36 #include "ctsvc_server_utils.h"
37 #include "ctsvc_server_bg.h"
38 #include "ctsvc_server_update.h"
39 #include "ctsvc_server_service.h"
40
41 #include "ctsvc_db_access_control.h"
42
43 #include "ctsvc_ipc_define.h"
44 #include "ctsvc_ipc_server.h"
45 #include "ctsvc_ipc_server2.h"
46 #include "ctsvc_notify.h"
47
48 #define CTSVC_TIMEOUT_FOR_DEFAULT 0
49
50 static int ctsvc_timeout_sec = CTSVC_TIMEOUT_FOR_DEFAULT;
51 static GMainLoop *main_loop = NULL;
52
53 static int __server_main(void)
54 {
55         int ret;
56
57         char sock_file[CTSVC_PATH_MAX_LEN] = {0};
58         snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s", getuid(), CTSVC_IPC_SERVICE);
59         pims_ipc_svc_init(sock_file, CTS_SECURITY_FILE_GROUP, 0777);
60
61         do {
62                 /*
63                  * register handle functions
64                  * These functions will be called when requesting from client module depends on module name and function name (pims_ipc_call, ctsvc_ipc_call)
65                  * pims_ipc_svc_register(MODULE_NAME, FUNCTION_NAME ...);
66                  */
67                 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CONNECT, ctsvc_ipc_server_connect, NULL) != 0) break;
68                 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_DISCONNECT, ctsvc_ipc_server_disconnect, NULL) != 0) break;
69                 if (pims_ipc_svc_register(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CHECK_PERMISSION, ctsvc_ipc_server_check_permission, NULL) != 0) break;
70
71                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORD, ctsvc_ipc_server_db_insert_record, NULL) != 0) break;
72                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORD, ctsvc_ipc_server_db_get_record, NULL) != 0) break;
73                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORD, ctsvc_ipc_server_db_update_record, NULL) != 0) break;
74                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORD, ctsvc_ipc_server_db_delete_record, NULL) != 0) break;
75                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_REPLACE_RECORD, ctsvc_ipc_server_db_replace_record, NULL) != 0) break;
76                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_ALL_RECORDS, ctsvc_ipc_server_db_get_all_records, NULL) != 0) break;
77                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_RECORDS_WITH_QUERY, ctsvc_ipc_server_db_get_records_with_query, NULL) != 0) break;
78                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT, ctsvc_ipc_server_db_get_count, NULL) != 0) break;
79                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_COUNT_WITH_QUERY, ctsvc_ipc_server_db_get_count_with_query, NULL) != 0) break;
80                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_INSERT_RECORDS, ctsvc_ipc_server_db_insert_records, NULL) != 0) break;
81                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_UPDATE_RECORDS, ctsvc_ipc_server_db_update_records, NULL) != 0) break;
82                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_DELETE_RECORDS, ctsvc_ipc_server_db_delete_records, NULL) != 0) break;
83                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_REPLACE_RECORDS, ctsvc_ipc_server_db_replace_records, NULL) != 0) break;
84                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_CHANGES_BY_VERSION, ctsvc_ipc_server_db_get_changes_by_version, NULL) != 0) break;
85                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_CURRENT_VERSION, ctsvc_ipc_server_db_get_current_version, NULL) != 0) break;
86                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS, ctsvc_ipc_server_db_search_records, NULL) != 0) break;
87                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_RANGE, ctsvc_ipc_server_db_search_records_with_range, NULL) != 0) break;
88                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_SEARCH_RECORDS_WITH_QUERY, ctsvc_ipc_server_db_search_records_with_query, NULL) != 0) break;
89                 if (pims_ipc_svc_register(CTSVC_IPC_DB_MODULE, CTSVC_IPC_SERVER_DB_GET_STATUS, ctsvc_ipc_server_db_get_status, NULL) != 0) break;
90
91                 if (pims_ipc_svc_register(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_CONTACT_ID, ctsvc_ipc_activity_delete_by_contact_id, NULL) != 0) break;
92                 if (pims_ipc_svc_register(CTSVC_IPC_ACTIVITY_MODULE, CTSVC_IPC_SERVER_ACTIVITY_DELETE_BY_ACCOUNT_ID, ctsvc_ipc_activity_delete_by_account_id, NULL) != 0) break;
93
94                 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_ADD_CONTACT, ctsvc_ipc_group_add_contact, NULL) != 0) break;
95                 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_REMOVE_CONTACT, ctsvc_ipc_group_remove_contact, NULL) != 0) break;
96                 if (pims_ipc_svc_register(CTSVC_IPC_GROUP_MODULE, CTSVC_IPC_SERVER_GROUP_SET_GROUP_ORDER, ctsvc_ipc_group_set_group_order, NULL) != 0) break;
97
98                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_LINK_PERSON, ctsvc_ipc_person_link_person, NULL) != 0) break;
99                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_UNLINK_CONTACT, ctsvc_ipc_person_unlink_contact, NULL) != 0) break;
100                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_RESET_USAGE, ctsvc_ipc_person_reset_usage, NULL) != 0) break;
101                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_FAVORITE_ORDER, ctsvc_ipc_person_set_favorite_order, NULL) != 0) break;
102                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_SET_DEFAULT_PROPERTY, ctsvc_ipc_person_set_default_property, NULL) != 0) break;
103                 if (pims_ipc_svc_register(CTSVC_IPC_PERSON_MODULE, CTSVC_IPC_SERVER_PERSON_GET_DEFAULT_PROPERTY, ctsvc_ipc_person_get_default_property, NULL) != 0) break;
104
105 #ifdef ENABLE_LOG_FEATURE
106                 if (pims_ipc_svc_register(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_RESET_STATISTICS, ctsvc_ipc_phone_log_reset_statistics, NULL) != 0) break;
107                 if (pims_ipc_svc_register(CTSVC_IPC_PHONELOG_MODULE, CTSVC_IPC_SERVER_PHONELOG_DELETE, ctsvc_ipc_phone_log_delete, NULL) != 0) break;
108 #endif /* ENABLE_LOG_FEATURE */
109
110                 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_DISPLAY_ORDER, ctsvc_ipc_setting_get_name_display_order, NULL) != 0) break;
111                 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_DISPLAY_ORDER, ctsvc_ipc_setting_set_name_display_order, NULL) != 0) break;
112                 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_GET_NAME_SORTING_ORDER, ctsvc_ipc_setting_get_name_sorting_order, NULL) != 0) break;
113                 if (pims_ipc_svc_register(CTSVC_IPC_SETTING_MODULE, CTSVC_IPC_SERVER_SETTING_SET_NAME_SORTING_ORDER, ctsvc_ipc_setting_set_name_sorting_order, NULL) != 0) break;
114
115                 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s_for_subscribe", getuid(), CTSVC_IPC_SERVICE);
116                 pims_ipc_svc_init_for_publish(sock_file, CTS_SECURITY_FILE_GROUP, 0660);
117                 ctsvc_noti_publish_socket_initialize();
118
119                 ret = ctsvc_connect();
120                 if (CONTACTS_ERROR_NONE != ret) {
121                         CTS_ERR("contacts_connect fail(%d)", ret);
122                         break;
123                 }
124                 ctsvc_set_client_access_info(NULL, NULL);
125
126                 ctsvc_server_bg_add_cb();
127                 ctsvc_server_bg_delete_start();
128
129                 ret = ctsvc_server_init_configuration();
130                 CTS_DBG("%d", ret);
131
132                 main_loop = g_main_loop_new(NULL, FALSE);
133
134                 pims_ipc_svc_run_main_loop(main_loop);
135
136                 ctsvc_server_final_configuration();
137
138                 ctsvc_server_bg_remove_cb();
139
140                 ctsvc_unset_client_access_info();
141
142                 ret = ctsvc_disconnect();
143                 if (CONTACTS_ERROR_NONE != ret)
144                         CTS_DBG("%d", ret);
145
146                 pims_ipc_svc_deinit_for_publish();
147
148                 pims_ipc_svc_deinit();
149
150                 return 0;
151
152         } while (0);
153
154         CTS_ERR("pims_ipc_svc_register error");
155         return -1;
156 }
157
158 void ctsvc_server_quit(void)
159 {
160         g_main_loop_quit(main_loop);
161         main_loop = NULL;
162 }
163
164 int ctsvc_server_get_timeout_sec(void)
165 {
166         CTS_DBG("ctsvc_timeout_sec:%d", ctsvc_timeout_sec);
167         return ctsvc_timeout_sec;
168 }
169
170 #define CTSVC_SECURITY_FILE_GROUP 6005
171 void ctsvc_create_file_set_permission(const char* file, mode_t mode)
172 {
173         int fd, ret;
174         fd = creat(file, mode);
175         if (0 <= fd)
176         {
177                 ret = fchown(fd, -1, CTSVC_SECURITY_FILE_GROUP);
178                 if (-1 == ret)
179                 {
180                         printf("Fail to fchown\n");
181                         return;
182                 }
183                 close(fd);
184         }
185
186 }
187
188 void ctsvc_create_rep_set_permission(const char* directory, mode_t mode)
189 {
190         if (-1 == access (directory, F_OK)) {
191                 mkdir(directory, mode);
192         }
193 }
194
195 int main(int argc, char *argv[])
196 {
197         CTS_FN_CALL;
198         INFO("Start contacts-service");
199         int ret;
200
201         if (getuid() == 0) {   /* root */
202                 gid_t glist[] = {CTS_SECURITY_FILE_GROUP};
203                 ret = setgroups(1, glist);   /* client and server should have same Groups */
204                 WARN_IF(ret <0, "setgroups Fail(%d)", ret);
205         }
206
207         if (2 <= argc && STRING_EQUAL == strcmp(argv[1], "timeout"))
208                 ctsvc_timeout_sec = atoi(argv[2]);
209
210         ctsvc_server_check_schema();
211
212         ctsvc_create_rep_set_permission(DATA_REPERTORY, 0755);
213         ctsvc_create_rep_set_permission(CTSVC_NOTI_REPERTORY, 0775);
214         ctsvc_create_rep_set_permission(CTSVC_NOTI_IMG_REPERTORY, 0750);
215         ctsvc_create_rep_set_permission(CTSVC_VCARD_IMAGE_LOCATION, 0770);
216         ctsvc_create_rep_set_permission(CTS_MY_IMAGE_LOCATION, 0750);
217         ctsvc_create_rep_set_permission(CTS_GROUP_IMAGE_LOCATION, 0750);
218         ctsvc_create_rep_set_permission(CTS_LOGO_IMAGE_LOCATION, 0750);
219         ctsvc_create_rep_set_permission(CTSVC_CONTACT_IMG_FULL_LOCATION, 0750);
220
221         ctsvc_create_file_set_permission(CTSVC_NOTI_IPC_READY, 0660);
222         ctsvc_create_file_set_permission(CTSVC_NOTI_ADDRESSBOOK_CHANGED, 0660);
223         ctsvc_create_file_set_permission(CTSVC_NOTI_GROUP_CHANGED, 0660);
224         ctsvc_create_file_set_permission(CTSVC_NOTI_PERSON_CHANGED, 0660);
225         ctsvc_create_file_set_permission(CTSVC_NOTI_CONTACT_CHANGED, 0660);
226         ctsvc_create_file_set_permission(CTSVC_NOTI_MY_PROFILE_CHANGED, 0660);
227         ctsvc_create_file_set_permission(CTSVC_NOTI_NAME_CHANGED, 0660);
228         ctsvc_create_file_set_permission(CTSVC_NOTI_NUMBER_CHANGED, 0660);
229         ctsvc_create_file_set_permission(CTSVC_NOTI_EMAIL_CHANGED, 0660);
230         ctsvc_create_file_set_permission(CTSVC_NOTI_EVENT_CHANGED, 0660);
231         ctsvc_create_file_set_permission(CTSVC_NOTI_URL_CHANGED, 0660);
232         ctsvc_create_file_set_permission(CTSVC_NOTI_GROUP_RELATION_CHANGED, 0660);
233         ctsvc_create_file_set_permission(CTSVC_NOTI_ADDRESS_CHANGED, 0660);
234         ctsvc_create_file_set_permission(CTSVC_NOTI_NOTE_CHANGED, 0660);
235         ctsvc_create_file_set_permission(CTSVC_NOTI_COMPANY_CHANGED, 0660);
236         ctsvc_create_file_set_permission(CTSVC_NOTI_RELATIONSHIP_CHANGED, 0660);
237         ctsvc_create_file_set_permission(CTSVC_NOTI_IMAGE_CHANGED, 0660);
238         ctsvc_create_file_set_permission(CTSVC_NOTI_NICKNAME_CHANGED, 0660);
239         ctsvc_create_file_set_permission(CTSVC_NOTI_MESSENGER_CHANGED, 0660);
240         ctsvc_create_file_set_permission(CTSVC_NOTI_DATA_CHANGED, 0660);
241         ctsvc_create_file_set_permission(CTSVC_NOTI_SDN_CHANGED, 0660);
242         ctsvc_create_file_set_permission(CTSVC_NOTI_PROFILE_CHANGED, 0660);
243         ctsvc_create_file_set_permission(CTSVC_NOTI_ACTIVITY_CHANGED, 0660);
244         ctsvc_create_file_set_permission(CTSVC_NOTI_ACTIVITY_PHOTO_CHANGED, 0660);
245         ctsvc_create_file_set_permission(CTSVC_NOTI_PHONELOG_CHANGED, 0660);
246         ctsvc_create_file_set_permission(CTSVC_NOTI_SPEEDDIAL_CHANGED, 0660);
247
248         // update DB for compatability
249         ctsvc_server_db_update();
250
251         ctsvc_server_load_feature_list();
252         ret = ctsvc_server_socket_init();
253         CTS_DBG("%d", ret);
254
255         __server_main();
256
257         ctsvc_server_socket_deinit();
258
259         return 0;
260 }
261