3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sync_agent.h>
29 #include "password_generator.h"
32 #define EXPORT_API __attribute__ ((visibility("default")))
35 #ifndef OMADM_AGENT_LOG
37 #define LOG_TAG "PLUGIN_DMACC_GCF"
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"
51 //#define CSC_GCF_SERVER_NUM 3
52 #define CSC_GCF_SERVER_NUM 1
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 ""
67 static int _get_index(const char *name);
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);
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);
75 static sync_agent_get_devinfo_cb func_get_dev_info = 0;
77 EXPORT_API char **sync_agent_plugin_get_server_id_list(int *server_id_list_cnt)
81 *server_id_list_cnt = 0;
82 char **temp_id_list = (char **)calloc(SERVER_NUM, sizeof(char *));
83 if (temp_id_list == NULL) {
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);
92 *temp_id_list = server_id;
93 *server_id_list_cnt = 1;
95 _DEBUG_INFO("server_id = %s\n", server_id);
97 _DEBUG_INFO("server_id_list_cnt = %d\n", *server_id_list_cnt);
103 EXPORT_API int sync_agent_plugin_get_mo_value(const char *mo_full_path, const char *mo_name, char **mo_value)
107 retvm_if((mo_full_path) == NULL, -1, "mo_full_path is NULL!!");
108 retvm_if((mo_name) == NULL, -1, "mo_name is NULL!!");
110 _DEBUG_INFO("mo_name : %s", mo_name);
113 int n_index = _get_index(ALTER_SYNCMLDM_ACCNAME);
115 if (!strcmp(mo_name, "AppID")) {
116 *mo_value = strdup("w7");
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);
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);
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);
139 } else if (!strcmp(mo_name, "AddrType")) { /* ./DMAcc/gcf/AppAddr/MSCserver_AppAddr/AddrType */
140 *mo_value = strdup("IPv4");
142 } else if (!strcmp(mo_name, "PortNbr")) { /* ./DMAcc/gcf/AppAddr/MSCserver_AppAddr/Port/MSCserver_Port/PortNbr */
144 int port = _get_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_SERVPORT, n_index);
146 port = ALTER_SYNCMLDM_SERVPORT;
147 _set_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_SERVPORT, n_index, ALTER_SYNCMLDM_SERVPORT);
149 *mo_value = g_strdup_printf("%d", port);
152 } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthLevel")) {
153 *mo_value = strdup("SRVCRED");
155 } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ServerAppAuth/AAuthType")) {
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");
168 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
169 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
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);
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);
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");
193 _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
196 _DEBUG_INFO("Device Id : %s", device_id);
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);
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);
206 if (*mo_value == NULL) {
207 _DEBUG_INFO("generate device password fail");
208 *mo_value = strdup(ALTER_SYNCMLDM_SERVPWD);
211 _DEBUG_INFO("server pw : %s", *mo_value);
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);
223 } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthLevel")) {
224 *mo_value = strdup("CLCRED");
226 } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthType")) {
228 int auth_type = _get_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index);
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");
240 *mo_value = strdup(ALTER_SYNCMLDM_AUTHTYPE);
241 _set_mo_value_str(CSC_VCONF_KEY_SYNCMLDM_AUTHTYPE, n_index, ALTER_SYNCMLDM_AUTHTYPE);
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");
254 _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
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);
262 *mo_value = generate_device_password((const char *)device_id, (const char *)server_id, CLIENT_ID_KEY);
264 if (*mo_value == NULL) {
265 _DEBUG_INFO("generate device password fail");
266 *mo_value = strdup(ALTER_SYNCMLDM_SERVPWD);
272 /* *mo_value = strdup("IMEI:357584040054001"); */
273 } else if (!strcmp(mo_full_path, "./DMAcc/gcf/AppAuth/ClientAppAuth/AAuthSecret")) {
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");
283 _DEBUG_INFO("pFunc_Get_DevInfo is NULL");
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);
291 *mo_value = generate_device_password((const char *)device_id, (const char *)server_id, CLIENT_PW_KEY);
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);
303 _DEBUG_INFO("---------------------------------------------------not pased mo element : %s---------------------------------------------------", mo_full_path);
306 _DEBUG_INFO("mo_value : %s", *mo_value);
313 EXPORT_API void sync_agent_set_function_get_devinfo_plugin(sync_agent_get_devinfo_cb func)
317 func_get_dev_info = func;
322 static int _system_command(const char *command)
326 int pid = 0, status = 0;
327 char *const environ[] = { NULL };
329 retvm_if((command) == NULL, -1, "command is NULL!!");
333 _DEBUG_INFO("ERROR : fork failed to create a process\n");
337 } else if (pid == 0) {
341 argv[2] = (char *)command;
344 execve("/bin/sh", argv, environ);
348 if (waitpid(pid, &status, 0) == -1) {
349 if (errno != EINTR) {
364 static int _get_index(const char *name)
369 if (vconf_get_int(CSC_VCONF_KEY_SYNCMLDM_NBFIELD, &n_acc_count)) {
370 _DEBUG_TRACE("vconf_get_int FAIL");
372 //n_acc_count =ALTER_SYNCMLDM_NBFIELD;
373 //_set_mo_value_int(CSC_VCONF_KEY_SYNCMLDM_NBFIELD, CSC_GCF_SERVER_NUM, ALTER_SYNCMLDM_NBFIELD);
375 //////// CSC don't include vconf value.
376 int ret = _system_command("/usr/share/oma-dm-cfg/csc-setting/change_csc_value.sh");
378 _DEBUG_INFO("success");
382 n_acc_count = ALTER_SYNCMLDM_NBFIELD;
385 _DEBUG_TRACE("nAccCount : %d", n_acc_count);
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) {
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);
399 if (compare_name != NULL) {
408 return CSC_GCF_SERVER_NUM;
411 static char *_get_mo_value_str(const char *vconf_key, int nIndex)
415 retvm_if((vconf_key) == NULL, NULL, "vconf_key is NULL!!");
421 char input_vconf_path[200] = { 0, };
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);
428 char *value = vconf_get_str(input_vconf_path);
430 _DEBUG_TRACE("vconf_get_str FAIL");
438 static int _get_mo_value_int(const char *vconf_key, int nIndex)
442 retvm_if((vconf_key) == NULL, -1, "vconf_key is NULL!!");
444 char input_vconf_path[200] = { 0, };
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);
452 if (vconf_get_int(input_vconf_path, &value) == 0) {
453 _DEBUG_TRACE("vconf_get_int : %d", value);
455 _DEBUG_TRACE("vconf_get_int error");
463 static void _set_mo_value_str(const char *vconf_key, int nIndex, char *value)
467 retm_if((vconf_key) == NULL, "vconf_key is NULL!!");
469 char input_vconf_path[200] = { 0, };
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);
477 result = vconf_set_str(input_vconf_path, value);
478 _DEBUG_TRACE("vconf set reult : %d", result);
484 static void _set_mo_value_int(const char *vconf_key, int nIndex, int value)
488 retm_if((vconf_key) == NULL, "vconf_key is NULL!!");
490 char input_vconf_path[200] = { 0, };
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);
497 vconf_set_int(input_vconf_path, value);
503 EXPORT_API int sync_agent_plugin_initialize()