Tizen 2.0 Release
[framework/system/oma-dm-agent.git] / src / plugins / dm-private / dmacc-gcf / src / plugin_interface.c
1 /*
2  * oma-dm-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <vconf.h>
22 #include <glib.h>
23
24 #include <wait.h>
25
26 /*sync-agent*/
27 #include <sync_agent.h>
28
29 #include "password_generator.h"
30
31 #ifndef EXPORT_API
32 #define EXPORT_API __attribute__ ((visibility("default")))
33 #endif
34
35 #ifndef OMADM_AGENT_LOG
36 #undef LOG_TAG
37 #define LOG_TAG "PLUGIN_DMACC_GCF"
38 #endif
39
40 #define  CSC_VCONF_KEY_SYNCMLDM_NBFIELD                                 "db/SyncML/oma-dm-service/DevManagement/NbDevManagement"
41 #define  CSC_VCONF_KEY_SYNCMLDM_ACCNAME                         "db/SyncML/oma-dm-service/DevManagement/%d/AccName"
42 #define  CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE                                "db/SyncML/oma-dm-service/DevManagement/%d/AuthType"
43 #define  CSC_VCONF_KEY_SYNCMLDM_CLIENTNONCE                     "db/SyncML/oma-dm-service/DevManagement/%d/ClientNonce"
44 #define  CSC_VCONF_KEY_SYNCMLDM_SERVADDR                                "db/SyncML/oma-dm-service/DevManagement/%d/ServAddr"
45 #define  CSC_VCONF_KEY_SYNCMLDM_SERVID                                  "db/SyncML/oma-dm-service/DevManagement/%d/ServID"
46 #define  CSC_VCONF_KEY_SYNCMLDM_SERVNONCE                       "db/SyncML/oma-dm-service/DevManagement/%d/ServNonce"
47 #define  CSC_VCONF_KEY_SYNCMLDM_SERVPORT                                "db/SyncML/oma-dm-service/DevManagement/%d/Port"
48 #define  CSC_VCONF_KEY_SYNCMLDM_SERVPWD                         "db/SyncML/oma-dm-service/DevManagement/%d/ServPwd"
49 #define  CSC_VCONF_KEY_SYNCMLDM_PASSWORD                                "db/SyncML/oma-dm-service/DevManagement/%d/Password"
50
51 //#define CSC_GCF_SERVER_NUM                                                                    3
52 #define CSC_GCF_SERVER_NUM                                                                      1
53
54 #define ALTER_SYNCMLDM_NBFIELD 3
55 #define ALTER_SYNCMLDM_ACCNAME "gcf"
56 #define ALTER_SYNCMLDM_AUTHTYPE "DIGEST"
57 #define ALTER_SYNCMLDM_CLIENTNONCE "12345"
58 #define ALTER_SYNCMLDM_SERVADDR "http://192.168.168.169/dm"
59 #define ALTER_SYNCMLDM_SERVID "RSate"
60 #define ALTER_SYNCMLDM_SERVNONCE "12345"
61 #define ALTER_SYNCMLDM_SERVPORT 80
62 #define ALTER_SYNCMLDM_SERVPWD "T1NQIERNIFNlcnZlcg=="
63 #define ALTER_SYNCMLDM_PASSWORD ""
64
65 #define SERVER_NUM                                                                                                              1
66
67 static int _get_index(const char *name);
68
69 static char *_get_mo_value_str(const char *vconf_key, int nIndex);
70 static int _get_mo_value_int(const char *vconf_key, int nIndex);
71
72 static void _set_mo_value_str(const char *vconf_key, int nIndex, char *value);
73 static void _set_mo_value_int(const char *vconf_key, int nIndex, int value);
74
75 static sync_agent_get_devinfo_cb func_get_dev_info = 0;
76
77 EXPORT_API char **sync_agent_plugin_get_server_id_list(int *server_id_list_cnt)
78 {
79         _EXTERN_FUNC_ENTER;
80
81         *server_id_list_cnt = 0;
82         char **temp_id_list = (char **)calloc(SERVER_NUM, sizeof(char *));
83         if (temp_id_list == NULL) {
84                 return 0;
85         }
86
87         int server_idx = _get_index(ALTER_SYNCMLDM_ACCNAME);
88         char *server_id = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, server_idx);
89         if (server_id == NULL) {
90                 server_id = strdup(ALTER_SYNCMLDM_SERVID);
91         }
92         *temp_id_list = server_id;
93         *server_id_list_cnt = 1;
94
95         _DEBUG_INFO("server_id = %s\n", server_id);
96
97         _DEBUG_INFO("server_id_list_cnt = %d\n", *server_id_list_cnt);
98
99         _EXTERN_FUNC_EXIT;
100         return temp_id_list;
101 }
102
103 EXPORT_API int sync_agent_plugin_get_mo_value(const char *mo_full_path, const char *mo_name, char **mo_value)
104 {
105         _EXTERN_FUNC_ENTER;
106
107         retvm_if((mo_full_path) == NULL, -1, "mo_full_path is NULL!!");
108         retvm_if((mo_name) == NULL, -1, "mo_name is NULL!!");
109
110         _DEBUG_INFO("mo_name : %s", mo_name);
111         *mo_value = NULL;
112
113         int n_index = _get_index(ALTER_SYNCMLDM_ACCNAME);
114
115         if (!strcmp(mo_name, "AppID")) {
116                 *mo_value = strdup("w7");
117
118         } else if (!strcmp(mo_name, "ServerID")) {
119                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index);
120                 if ((*mo_value) == NULL) {
121                         *mo_value = strdup(ALTER_SYNCMLDM_SERVID);
122                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index, ALTER_SYNCMLDM_SERVID);
123                 }
124
125         } else if (!strcmp(mo_name, "Name")) {
126                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_ACCNAME, n_index);
127                 if ((*mo_value) == NULL) {
128                         *mo_value = strdup(ALTER_SYNCMLDM_ACCNAME);
129                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_ACCNAME, n_index, ALTER_SYNCMLDM_ACCNAME);
130                 }
131
132         } else if (!strcmp(mo_name, "Addr")) {  /* ./DMAcc/gcf/AppAddr/MSCserver_AppAddr/Addr */
133                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVADDR, n_index);
134                 if ((*mo_value) == NULL) {
135                         *mo_value = strdup(ALTER_SYNCMLDM_SERVADDR);
136                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVADDR, n_index, ALTER_SYNCMLDM_SERVADDR);
137                 }
138
139         } else if (!strcmp(mo_name, "AddrType")) {      /* ./DMAcc/gcf/AppAddr/MSCserver_AppAddr/AddrType */
140                 *mo_value = strdup("IPv4");
141
142         } else if (!strcmp(mo_name, "PortNbr")) {       /* ./DMAcc/gcf/AppAddr/MSCserver_AppAddr/Port/MSCserver_Port/PortNbr */
143                 if (n_index != -1) {
144                         int port = _get_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_SERVPORT, n_index);
145                         if (port == -1) {
146                                 port = ALTER_SYNCMLDM_SERVPORT;
147                                 _set_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_SERVPORT, n_index, ALTER_SYNCMLDM_SERVPORT);
148                         }
149                         *mo_value = g_strdup_printf("%d", port);
150                 }
151
152         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthLevel")) {
153                 *mo_value = strdup("SRVCRED");
154
155         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthType")) {
156                 if (n_index != -1) {
157                         int auth_type = _get_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index);
158                         if (auth_type == -1) {
159                                 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
160                                 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
161                         } else if (auth_type == 0) {    /* basic */
162                                 *mo_value = strdup("BASIC");
163                         } else if (auth_type == 1) {    /* md5 */
164                                 *mo_value = strdup("DIGEST");
165                         } else if (auth_type == 2) {    /* hmac */
166                                 *mo_value = strdup("HMAC");
167                         } else {
168                                 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
169                                 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
170                         }
171                 }
172
173         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthName")) {
174                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index);
175                 if ((*mo_value) == NULL) {
176                         *mo_value = strdup(ALTER_SYNCMLDM_SERVID);
177                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index, ALTER_SYNCMLDM_SERVID);
178                 }
179
180         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthSecret")) {
181                 /* *mo_value = __get_MO_Value_STR(CSC_VCONF_KEY_SYNCMLDM_SERVPWD, nIndex);
182                    if((*mo_value) == NULL) {
183                    *mo_value = strdup(ALTER_SYNCMLDM_SERVPWD);
184                    } */
185                 char *device_id = NULL;
186                 if (func_get_dev_info != 0) {
187                         func_get_dev_info(2, "DevID", &device_id);
188                         if (device_id == NULL) {
189                                 _DEBUG_INFO("device id is NULL");
190                                 return -1;
191                         }
192                 } else {
193                         _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
194                         return -1;
195                 }
196                 _DEBUG_INFO("Device Id : %s", device_id);
197
198                 char *server_id = 0;
199                 server_id = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index);
200                 if (server_id == NULL) {
201                         server_id = strdup(ALTER_SYNCMLDM_SERVID);
202                 }
203                 _DEBUG_INFO("Server Id : %s", server_id);
204                 *mo_value = generate_device_password((const char *)device_id, (const char *)server_id, SERVER_PW_KEY);
205
206                 if (*mo_value == NULL) {
207                         _DEBUG_INFO("generate device password fail");
208                         *mo_value = strdup(ALTER_SYNCMLDM_SERVPWD);
209                 }
210
211                 _DEBUG_INFO("server pw : %s", *mo_value);
212
213                 free(device_id);
214                 free(server_id);
215
216         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthData")) {
217                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVNONCE, n_index);
218                 if ((*mo_value) == NULL) {
219                         *mo_value = strdup(ALTER_SYNCMLDM_SERVNONCE);
220                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVNONCE, n_index, ALTER_SYNCMLDM_SERVNONCE);
221                 }
222
223         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthLevel")) {
224                 *mo_value = strdup("CLCRED");
225
226         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthType")) {
227                 if (n_index != -1) {
228                         int auth_type = _get_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index);
229
230                         if (auth_type == -1) {
231                                 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
232                                 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
233                         } else if (auth_type == 0) {    /* basic */
234                                 *mo_value = strdup("BASIC");
235                         } else if (auth_type == 1) {    /* md5 */
236                                 *mo_value = strdup("DIGEST");
237                         } else if (auth_type == 2) {    /* hmac */
238                                 *mo_value = strdup("HMAC");
239                         } else {
240                                 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
241                                 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
242                         }
243                 }
244
245         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthName")) {
246                 char *device_id = NULL;
247                 if (func_get_dev_info != NULL) {
248                         func_get_dev_info(2, "DevID", &device_id);
249                         if (device_id == NULL) {
250                                 _DEBUG_INFO("device id is NULL");
251                                 return -1;
252                         }
253                 } else {
254                         _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
255                         return -1;
256                 }
257                 char *server_id = NULL;
258                 server_id = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index);
259                 if (server_id == NULL) {
260                         server_id = strdup(ALTER_SYNCMLDM_SERVID);
261                 }
262                 *mo_value = generate_device_password((const char *)device_id, (const char *)server_id, CLIENT_ID_KEY);
263
264                 if (*mo_value == NULL) {
265                         _DEBUG_INFO("generate device password fail");
266                         *mo_value = strdup(ALTER_SYNCMLDM_SERVPWD);
267                 }
268
269                 free(device_id);
270                 free(server_id);
271
272                 /* *mo_value = strdup("IMEI:357584040054001"); */
273         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthSecret")) {
274
275                 char *device_id = NULL;
276                 if (func_get_dev_info != NULL) {
277                         func_get_dev_info(2, "DevID", &device_id);
278                         if (device_id == NULL) {
279                                 _DEBUG_INFO("device id is NULL");
280                                 return -1;
281                         }
282                 } else {
283                         _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
284                         return -1;
285                 }
286                 char *server_id = NULL;
287                 server_id = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_SERVID, n_index);
288                 if (server_id == NULL) {
289                         server_id = strdup(ALTER_SYNCMLDM_SERVID);
290                 }
291                 *mo_value = generate_device_password((const char *)device_id, (const char *)server_id, CLIENT_PW_KEY);
292
293                 free(device_id);
294                 free(server_id);
295
296         } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthData")) {
297                 *mo_value = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_CLIENTNONCE, n_index);
298                 if ((*mo_value) == NULL) {
299                         *mo_value = strdup(ALTER_SYNCMLDM_CLIENTNONCE);
300                         _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_CLIENTNONCE, n_index, ALTER_SYNCMLDM_CLIENTNONCE);
301                 }
302         } else {
303                 _DEBUG_INFO("---------------------------------------------------not pased mo element : %s---------------------------------------------------", mo_full_path);
304         }
305
306         _DEBUG_INFO("mo_value : %s", *mo_value);
307
308         _EXTERN_FUNC_EXIT;
309
310         return 0;
311 }
312
313 EXPORT_API void sync_agent_set_function_get_devinfo_plugin(sync_agent_get_devinfo_cb func)
314 {
315         _EXTERN_FUNC_ENTER;
316
317         func_get_dev_info = func;
318
319         _EXTERN_FUNC_ENTER;
320 }
321
322 static int _system_command(const char *command)
323 {
324         _EXTERN_FUNC_ENTER;
325
326         int pid = 0, status = 0;
327         char *const environ[] = { NULL };
328
329         retvm_if((command) == NULL, -1, "command is NULL!!");
330
331         pid = fork();
332         if (pid == -1) {
333                 _DEBUG_INFO("ERROR : fork failed to create a process\n");
334                 _EXTERN_FUNC_EXIT;
335                 return -1;
336
337         } else if (pid == 0) {
338                 char *argv[4];
339                 argv[0] = "sh";
340                 argv[1] = "-c";
341                 argv[2] = (char *)command;
342                 argv[3] = 0;
343
344                 execve("/bin/sh", argv, environ);
345                 abort();
346         } else {
347                 do {
348                         if (waitpid(pid, &status, 0) == -1) {
349                                 if (errno != EINTR) {
350                                         return -1;
351                                 }
352
353                         } else {
354                                 _EXTERN_FUNC_EXIT;
355                                 return status;
356                         }
357
358                 } while (1);
359         }
360         _EXTERN_FUNC_EXIT;
361         return -1;
362 }
363
364 static int _get_index(const char *name)
365 {
366         _INNER_FUNC_ENTER;
367
368         int n_acc_count = 0;
369         if (vconf_get_int(CSC_VCONF_KEY_SYNCMLDM_NBFIELD, &n_acc_count)) {
370                 _DEBUG_TRACE("vconf_get_int FAIL");
371                 ////            return -1;
372                 //n_acc_count =ALTER_SYNCMLDM_NBFIELD;
373                 //_set_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_NBFIELD, CSC_GCF_SERVER_NUM, ALTER_SYNCMLDM_NBFIELD);
374
375                 //////// CSC don't include vconf value.
376                 int ret = _system_command("/usr/share/oma-dm-cfg/csc-setting/change_csc_value.sh");
377                 if (ret != -1) {
378                         _DEBUG_INFO("success");
379                 } else {
380                         _DEBUG_INFO("fail");
381                 }
382                 n_acc_count = ALTER_SYNCMLDM_NBFIELD;
383         }
384
385         _DEBUG_TRACE("nAccCount : %d", n_acc_count);
386
387         int i = 0;
388         char *compare_name = NULL;
389         for (; i < n_acc_count; i++) {
390                 compare_name = _get_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_ACCNAME, i + 1);
391                 if (compare_name == NULL) {
392                         continue;
393                 } else {
394                         _DEBUG_TRACE("[%d] compare_name : %s [%d], name : %s [%d]", i, compare_name, strlen(compare_name), name, strlen(name));
395                         if (!strcmp(name, compare_name)) {
396                                 _DEBUG_TRACE("nIndex : %d", i + 1);
397                                 return i + 1;
398                         }
399                         if (compare_name != NULL) {
400                                 free(compare_name);
401                                 compare_name = NULL;
402                         }
403                 }
404         }
405
406         _INNER_FUNC_EXIT;
407
408         return CSC_GCF_SERVER_NUM;
409 }
410
411 static char *_get_mo_value_str(const char *vconf_key, int nIndex)
412 {
413         _INNER_FUNC_ENTER;
414
415         retvm_if((vconf_key) == NULL, NULL, "vconf_key is NULL!!");
416
417         if (nIndex == -1) {
418                 return 0;
419         }
420
421         char input_vconf_path[200] = { 0, };
422         char rec_idx[8];
423
424         memset(rec_idx, 0x0, sizeof(rec_idx));
425         snprintf(rec_idx, sizeof(rec_idx), "%d", nIndex);
426         snprintf(input_vconf_path, strlen(vconf_key) + strlen(rec_idx) + 1, vconf_key, nIndex);
427
428         char *value = vconf_get_str(input_vconf_path);
429         if (value == NULL) {
430                 _DEBUG_TRACE("vconf_get_str FAIL");
431         }
432
433         _INNER_FUNC_ENTER;
434
435         return value;
436 }
437
438 static int _get_mo_value_int(const char *vconf_key, int nIndex)
439 {
440         _INNER_FUNC_ENTER;
441
442         retvm_if((vconf_key) == NULL, -1, "vconf_key is NULL!!");
443
444         char input_vconf_path[200] = { 0, };
445         char rec_idx[8];
446
447         memset(rec_idx, 0x0, sizeof(rec_idx));
448         snprintf(rec_idx, sizeof(rec_idx), "%d", nIndex);
449         snprintf(input_vconf_path, strlen(vconf_key) + strlen(rec_idx) + 1, vconf_key, nIndex);
450
451         int value = -1;
452         if (vconf_get_int(input_vconf_path, &value) == 0) {
453                 _DEBUG_TRACE("vconf_get_int : %d", value);
454         } else {
455                 _DEBUG_TRACE("vconf_get_int error");
456         }
457
458         _INNER_FUNC_ENTER;
459
460         return value;
461 }
462
463 static void _set_mo_value_str(const char *vconf_key, int nIndex, char *value)
464 {
465         _INNER_FUNC_ENTER;
466
467         retm_if((vconf_key) == NULL, "vconf_key is NULL!!");
468
469         char input_vconf_path[200] = { 0, };
470         char rec_idx[8];
471         int result = 0;
472
473         memset(rec_idx, 0x0, sizeof(rec_idx));
474         snprintf(rec_idx, sizeof(rec_idx), "%d", nIndex);
475         snprintf(input_vconf_path, strlen(vconf_key) + strlen(rec_idx) + 1, vconf_key, nIndex);
476
477         result = vconf_set_str(input_vconf_path, value);
478         _DEBUG_TRACE("vconf set reult : %d", result);
479
480         _INNER_FUNC_ENTER;
481
482 }
483
484 static void _set_mo_value_int(const char *vconf_key, int nIndex, int value)
485 {
486         _INNER_FUNC_ENTER;
487
488         retm_if((vconf_key) == NULL, "vconf_key is NULL!!");
489
490         char input_vconf_path[200] = { 0, };
491         char rec_idx[8];
492
493         memset(rec_idx, 0x0, sizeof(rec_idx));
494         snprintf(rec_idx, sizeof(rec_idx), "%d", nIndex);
495         snprintf(input_vconf_path, strlen(vconf_key) + strlen(rec_idx) + 1, vconf_key, nIndex);
496
497         vconf_set_int(input_vconf_path, value);
498
499         _INNER_FUNC_EXIT;
500
501 }
502
503 EXPORT_API int sync_agent_plugin_initialize()
504 {
505         _EXTERN_FUNC_ENTER;
506
507         int result = 1;
508
509         _EXTERN_FUNC_EXIT;
510         return result;
511 }