19 #include <libgupnp/gupnp.h>
25 #include <libxml/tree.h>
26 #include <libxml/parser.h>
28 #include "secure_wrapper.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
35 #include <libgupnp/gupnp-control-point.h>
36 #ifdef ENABLE_SD_NOTIFY
37 #include <systemd/sd-daemon.h>
39 #include "rdk_safeclib.h"
40 #define SERVER_CONTEXT_PORT 50769
41 #define DEVICE_PROTECTION_CONTEXT_PORT 50761
42 #define IDM_SERVICE "urn:schemas-upnp-org:service:X1IDM:1"
43 #define IDM_DP_SERVICE "urn:schemas-upnp-org:service:X1IDM_DP:1"
44 #define IDM_CERT_FILE "/tmp/idm_xpki_cert"
45 #define IDM_KEY_FILE "/tmp/idm_xpki_key"
46 #define IDM_CA_FILE "/tmp/idm_UPnP_CA"
50 #define MAC_ADDR_SIZE 18
51 #define IPv4_ADDR_SIZE 16
52 #define IPv6_ADDR_SIZE 128
53 #define ACCOUNTID_SIZE 30
54 char clientIp[IPv4_ADDR_SIZE],bcastMacaddress[MAC_ADDR_SIZE],gwyIpv6[IPv6_ADDR_SIZE],interface[IPv4_ADDR_SIZE],accountId[ACCOUNTID_SIZE],uUid[256];
55 GString *bcastmacaddress,*serial_num,*recv_id;
56 GUPnPContext *server_upnpContext,*server_upnpContextDeviceProtect;
57 void free_server_memory();
58 int check_file_presence();
59 BOOL check_empty_idm(
char *str)
66 bool check_null_idm(
char *str)
77 gupnp_service_action_set (action,
"BcastMacAddress", G_TYPE_STRING, bcastMacaddress, NULL);
78 gupnp_service_action_return (action);
84 g_value_init (value, G_TYPE_STRING);
85 g_value_set_string (value, bcastMacaddress);
89 get_client_ip_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
91 gupnp_service_action_set (action,
"ClientIP", G_TYPE_STRING, clientIp, NULL);
92 gupnp_service_action_return (action);
96 query_client_ip_cb (GUPnPService *service,
char *variable, GValue *value, gpointer user_data)
98 g_value_init (value, G_TYPE_STRING);
99 g_value_set_string (value, clientIp);
102 get_gwyipv6_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
104 gupnp_service_action_set (action,
"GatewayIPv6", G_TYPE_STRING, gwyIpv6, NULL);
105 gupnp_service_action_return (action);
108 query_gwyipv6_cb (GUPnPService *service,
char *variable, GValue *value, gpointer user_data)
110 g_value_init (value, G_TYPE_STRING);
111 g_value_set_string (value, gwyIpv6);
115 get_account_id_cb (GUPnPService *service, GUPnPServiceAction *action, gpointer user_data)
117 memset(accountId,0,ACCOUNTID_SIZE);
118 getAccountId(accountId);
119 g_message(
"accountId=%s",accountId);
120 gupnp_service_action_set (action,
"AccountId", G_TYPE_STRING, accountId,NULL);
121 gupnp_service_action_return (action);
125 query_account_id_cb (GUPnPService *service,
char *variable, GValue *value, gpointer user_data)
127 g_value_init (value, G_TYPE_STRING);
128 g_value_set_string (value, accountId);
134 ret = xmlReadFile(file_name, NULL, 0);
143 static xmlNode * get_node_by_name(xmlNode * node,
const char *node_name)
147 xmlNode * cur_node = NULL;
148 xmlNode * ret = NULL;
149 for (cur_node = node ; cur_node ; cur_node = cur_node->next)
151 rc = strcmp_s(cur_node->name, strlen(cur_node->name), node_name, &ind);
153 if ((ind ==0) && (rc == EOK))
157 ret = get_node_by_name(cur_node->children, node_name);
164 int set_content(xmlDoc* doc,
const char * node_name,
const char * new_value)
166 xmlNode * root_element = NULL;
167 xmlNode * target_node = NULL;
168 root_element = xmlDocGetRootElement(doc);
169 target_node = get_node_by_name(root_element, node_name);
170 if (target_node==NULL)
172 g_printerr(
"Couldn't locate the Target node\n");
175 xmlNodeSetContent(target_node,new_value);
179 BOOL
updatexmldata(
const char* xmlfilename,
const char* struuid,
const char* serialno)
184 g_printerr (
"Error reading the Device XML file\n");
189 g_printerr (
"Error setting the unique device id in conf xml\n");
194 g_printerr (
"Error setting the serial number in conf xml\n");
197 FILE *fp = fopen(xmlfilename,
"w");
200 g_printerr (
"Error opening the conf xml file for writing\n");
203 else if (xmlDocFormatDump(fp, doc, 1) == -1)
205 g_printerr (
"Could not write the conf to xml file\n");
218 void free_server_memory()
220 g_message(
"Inside %s",__FUNCTION__);
222 gupnp_root_device_set_available (baseDev, FALSE);
223 g_object_unref (upnpService);
224 g_object_unref (baseDev);
225 g_object_unref (server_upnpContext);
227 gupnp_root_device_set_available (dev, FALSE);
228 g_object_unref (upnpIdService);
229 g_object_unref (dev);
230 g_object_unref (server_upnpContextDeviceProtect);
233 BOOL getUidfromRecvId()
237 gchar **tokens = g_strsplit_set(bcastMacaddress,
"':''\n'", -1);
238 guint toklength = g_strv_length(tokens);
239 while (loopvar < toklength)
241 g_string_printf(recv_id,
"ebf5a0a0-1dd1-11b2-a90f-%s%s%s%s%s%s", g_strstrip(tokens[loopvar]), g_strstrip(tokens[loopvar + 1]),g_strstrip(tokens[loopvar + 2]),g_strstrip(tokens[loopvar + 3]),g_strstrip(tokens[loopvar + 4]),g_strstrip(tokens[loopvar + 5]));
242 g_message(
"getUidfromRecvId: recvId: %s", recv_id->str);
249 BOOL getUUID(
char *outValue)
252 if (!check_null_idm(outValue)) {
253 g_message(
"getUUID : NULL string !");
256 if (getUidfromRecvId()){
257 if( (check_empty_idm(recv_id->str))) {
258 sprintf(outValue,
"uuid:%s", recv_id->str);
263 g_message(
"getUUID : empty recvId");
268 g_message(
"getUUID : could not get UUID");
273 int idm_server_start(
char* Interface,
char * base_mac)
275 g_thread_init (NULL);
278 strcpy(interface,Interface);
279 g_message(
"%s %d interface=%s",__FUNCTION__,__LINE__,interface);
281 serial_num = g_string_new(NULL);
284 strcpy_s(bcastMacaddress, MAC_ADDR_SIZE, base_mac);
286 char certFile[24],keyFile[24],caFile[24]=IDM_CA_FILE;
287 strcpy(certFile,IDM_CERT_FILE);
288 strcpy(keyFile,IDM_KEY_FILE);
289 g_message(
"cert file=%s key file = %s",certFile,keyFile);
290 if((access(certFile,F_OK ) == 0) && (access(keyFile,F_OK ) == 0) && (access(caFile,F_OK ) == 0))
292 const char* struuid_dp=g_strconcat(
"uuid:", g_strstrip(bcastMacaddress),NULL);
293 int result =
updatexmldata(
"/etc/xupnp/IDM_DP.xml",struuid_dp,serial_num->str);
296 fprintf(stderr,
"Failed to open the device xml file /etc/xupnp/IDM_DP.xml\n");
299 server_upnpContextDeviceProtect = gupnp_context_new_s ( NULL,interface,DEVICE_PROTECTION_CONTEXT_PORT,certFile,keyFile, &error);
301 server_upnpContextDeviceProtect = gupnp_context_new_s ( interface,DEVICE_PROTECTION_CONTEXT_PORT,certFile,keyFile, &error);
303 g_message(
"created new upnpContext");
306 g_message(
"%s:Error creating the Device Protection Broadcast context: %s",
307 __FUNCTION__,error->message);
309 g_clear_error(&error);
313 gupnp_context_set_subscription_timeout(server_upnpContextDeviceProtect, 0);
315 gupnp_context_set_tls_params(server_upnpContextDeviceProtect,caFile,keyFile, NULL);
317 dev = gupnp_root_device_new (server_upnpContextDeviceProtect,
"/etc/xupnp/IDM_DP.xml",
"/etc/xupnp/");
319 dev = gupnp_root_device_new (server_upnpContextDeviceProtect,
"/etc/xupnp/IDM_DP.xml",
"/etc/xupnp/", &error);
321 gupnp_root_device_set_available (dev,
TRUE);
322 upnpIdService = gupnp_device_info_get_service(GUPNP_DEVICE_INFO (dev), IDM_DP_SERVICE);
325 g_message(
"Cannot get X1Identity service\n");
329 g_message(
"XUPNP Identity service successfully created");
331 g_signal_connect (upnpIdService,
"action-invoked::GetBcastMacAddress", G_CALLBACK (
get_bcastmacaddress_cb), NULL);
333 g_signal_connect (upnpIdService,
"action-invoked::GetClientIP", G_CALLBACK (
get_client_ip_cb), NULL);
334 g_signal_connect (upnpIdService,
"query-variable::ClientIP", G_CALLBACK (
query_client_ip_cb), NULL);
335 g_signal_connect (upnpIdService,
"action-invoked::GetAccountId", G_CALLBACK (get_account_id_cb), NULL);
336 g_signal_connect (upnpIdService,
"query-variable::AccountId", G_CALLBACK (query_account_id_cb), NULL);
337 g_signal_connect (upnpIdService,
"action-invoked::GetGatewayIPv6", G_CALLBACK (
get_gwyipv6_cb), NULL);
338 g_signal_connect (upnpIdService,
"query-variable::GatewayIPv6", G_CALLBACK (
query_gwyipv6_cb), NULL);
343 g_message(
"%s:mandatory files doesn't present",__FUNCTION__);
346 recv_id=g_string_new(NULL);
348 const char* struuid = uUid;
349 g_message(
"recv_id=%s",struuid);
350 int result =
updatexmldata(
"/etc/xupnp/IDM.xml",struuid,serial_num->str);
353 fprintf(stderr,
"Failed to open the device xml file /etc/xupnp/IDM.xml\n");
357 g_message(
"Updated the device xml file:IDM.XML uuid: %s",struuid);
360 server_upnpContext = gupnp_context_new (NULL, interface, SERVER_CONTEXT_PORT, &error);
362 server_upnpContext = gupnp_context_new (interface, SERVER_CONTEXT_PORT, &error);
365 g_message(
"Error creating the Broadcast context: %s",
368 g_clear_error(&error);
371 gupnp_context_set_subscription_timeout(server_upnpContext, 0);
373 baseDev = gupnp_root_device_new (server_upnpContext,
"/etc/xupnp/IDM.xml",
"/etc/xupnp/");
375 baseDev = gupnp_root_device_new (server_upnpContext,
"/etc/xupnp/IDM.xml",
"/etc/xupnp/", &error);
377 gupnp_root_device_set_available (baseDev,
TRUE);
378 upnpService = gupnp_device_info_get_service(GUPNP_DEVICE_INFO (baseDev), IDM_SERVICE);
381 g_printerr (
"Cannot get DiscoverFriendlies service\n");
384 g_signal_connect (upnpService,
"action-invoked::GetBcastMacAddress", G_CALLBACK (
get_bcastmacaddress_cb), NULL);
386 g_signal_connect (upnpService,
"action-invoked::GetClientIP", G_CALLBACK (
get_client_ip_cb), NULL);
387 g_signal_connect (upnpService,
"query-variable::ClientIP", G_CALLBACK (
query_client_ip_cb), NULL);
388 g_signal_connect (upnpService,
"action-invoked::GetGatewayIPv6", G_CALLBACK (
get_gwyipv6_cb), NULL);
389 g_signal_connect (upnpService,
"query-variable::GatewayIPv6", G_CALLBACK (
query_gwyipv6_cb), NULL);
391 g_message(
"completed %s\n",__FUNCTION__);