Initialize Tizen 2.3
[framework/system/oma-dm-agent.git] / src / agent / serviceadapter / networkbinder / na_binder.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 /*sync-agent*/
19 #include <sync_agent.h>
20
21 /*dm-agent*/
22 #include "common/dm_common.h"
23 #include "common/util/util.h"
24 #include "serviceadapter/networkbinder/na_binder.h"
25
26 #ifndef OMADM_AGENT_LOG
27 #undef LOG_TAG
28 #define LOG_TAG "OMA_DM_NA"
29 #endif
30
31 int NA_http_header_binder(char *accType, char *loc_uri, char *sessionid, int isHMAC, char *username, char *mac, GList ** list)
32 {
33         _EXTERN_FUNC_ENTER;
34
35         retvm_if((accType) == NULL, COMMON_ERR_IS_NULL, "accType is NULL!!");
36         retvm_if((loc_uri) == NULL, COMMON_ERR_IS_NULL, "loc_uri is NULL!!");
37
38         if (strcmp(accType, "OMA_DM") == 0) {
39                 _DEBUG_INFO(" header info start\n");
40                 sync_agent_na_common_header_info_s *method = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
41                 if (method == NULL) {
42                         _DEBUG_INFO("alloc error");
43                         _EXTERN_FUNC_EXIT;
44                         return 0;
45                 }
46                 method->key = "method";
47                 method->value = "post";
48                 *list = g_list_append(*list, method);
49                 _DEBUG_INFO(" post method\n");
50
51                 sync_agent_na_common_header_info_s *uri = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
52                 if (uri == NULL) {
53                         _DEBUG_INFO("alloc error");
54                         _EXTERN_FUNC_EXIT;
55                         return 0;
56                 }
57                 uri->key = "uri";
58                 uri->value = loc_uri;
59                 *list = g_list_append(*list, uri);
60                 _DEBUG_INFO(" uri\n");
61 /*
62                 sync_agent_na_common_header_info_s *cache_control = (sync_agent_na_common_header_info_s*)malloc(sizeof(sync_agent_na_common_header_info_s));
63                 memset(cache_control, 0x00, sizeof(sync_agent_na_common_header_info_s));
64                 cache_control->key = "Cache-Control";
65                 cache_control->value = "no-store, private";
66                 *list = g_list_append(*list, cache_control);
67                 _DEBUG_INFO( " \n");
68 */
69                 sync_agent_na_common_header_info_s *connection = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
70                 if (connection == NULL) {
71                         _DEBUG_INFO("alloc error");
72                         _EXTERN_FUNC_EXIT;
73                         return 0;
74                 }
75                 connection->key = "Connection";
76                 connection->value = "Keep-Alive";
77                 *list = g_list_append(*list, connection);
78                 _DEBUG_INFO(" connection\n");
79
80                 sync_agent_na_common_header_info_s *useragent = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
81                 if (useragent == NULL) {
82                         _DEBUG_INFO("alloc error");
83                         _EXTERN_FUNC_EXIT;
84                         return 0;
85                 }
86                 useragent->key = "User-Agent";
87                 useragent->value = "Samsung electronics Tizen SyncML_DM Client";
88                 *list = g_list_append(*list, useragent);
89                 _DEBUG_INFO(" user agent\n");
90
91                 sync_agent_na_common_header_info_s *accept = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
92                 if (accept == NULL) {
93                         _DEBUG_INFO("alloc error");
94                         _EXTERN_FUNC_EXIT;
95                         return 0;
96                 }
97                 accept->key = "Accept";
98                 accept->value = "application/vnd.syncml.dm+wbxml";
99                 *list = g_list_append(*list, accept);
100                 _DEBUG_INFO(" accept\n");
101
102 /*              sync_agent_na_common_header_info_s *accept_language = (sync_agent_na_common_header_info_s*)malloc(sizeof(sync_agent_na_common_header_info_s));
103                 memset(accept_language, 0x00, sizeof(sync_agent_na_common_header_info_s));
104                 accept_language->key = "Accept-Language";
105                 accept_language->value = "en-US";
106                 *list = g_list_append(*list, accept_language);
107                 _DEBUG_INFO( " \n");*/
108
109                 sync_agent_na_common_header_info_s *accept_charset = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
110                 if (accept_charset == NULL) {
111                         _DEBUG_INFO("alloc error");
112                         _EXTERN_FUNC_EXIT;
113                         return 0;
114                 }
115                 accept_charset->key = "Accept-Charset";
116                 accept_charset->value = "UTF-8";
117                 *list = g_list_append(*list, accept_charset);
118                 _DEBUG_INFO(" accept charset\n");
119
120                 sync_agent_na_common_header_info_s *content_type = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
121                 if (content_type == NULL) {
122                         _DEBUG_INFO("alloc error");
123                         _EXTERN_FUNC_EXIT;
124                         return 0;
125                 }
126                 content_type->key = "Content-Type";
127                 content_type->value = "application/vnd.syncml.dm+wbxml";
128                 *list = g_list_append(*list, content_type);
129                 _DEBUG_INFO(" content type\n");
130
131                 if (sessionid != NULL) {
132                         _DEBUG_INFO(" jsessionid : %s\n", sessionid);
133                         sync_agent_na_common_header_info_s *cookie = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
134                         if (cookie == NULL) {
135                                 _DEBUG_INFO("alloc error");
136                                 _EXTERN_FUNC_EXIT;
137                                 return 0;
138                         }
139                         cookie->key = HTTP_COOKIE;
140                         cookie->value = sessionid;
141                         *list = g_list_append(*list, cookie);
142                         _DEBUG_INFO(" cookie :  %s\n", cookie->value);
143                 }
144
145                 if (isHMAC == 1) {
146                         /*      x-syncml-hmac: algorithm=MD5, username="156354238", mac=SF2dMGut4v2Mpmsrs1ydTw== */
147                         _DEBUG_INFO(" x-syncml-hmac : %d\n", isHMAC);
148                         sync_agent_na_common_header_info_s *hmac = (sync_agent_na_common_header_info_s *) calloc(1, sizeof(sync_agent_na_common_header_info_s));
149                         if (hmac == NULL) {
150                                 _DEBUG_INFO("alloc error");
151                                 _EXTERN_FUNC_EXIT;
152                                 return 0;
153                         }
154                         hmac->key = HTTP_X_SYNCML_HMAC;
155                         if (username != NULL) {
156                                 hmac->value = g_strdup_printf("algorithm=MD5, username=\"%s\", mac=%s", username, mac);
157                                 _DEBUG_INFO(" hmac = %s : %s\n", hmac->key, hmac->value);
158                         }
159                         *list = g_list_append(*list, hmac);
160                 }
161         }
162
163         GList *iter = NULL;
164         for (iter = *list; iter != NULL; iter = g_list_next(iter)) {
165                 fputs("[NA_Header_Binder] key : ", stdout);
166                 if (fputs(((sync_agent_na_common_header_info_s *) (iter->data))->key, stdout) == EOF) {
167                         _DEBUG_INFO("debuging test error");
168                         break;
169                 }
170                 fputs(", value : ", stdout);
171                 if (fputs(((sync_agent_na_common_header_info_s *) (iter->data))->value, stdout) == EOF) {
172                         _DEBUG_INFO("debuging test error");
173                         break;
174                 }
175                 fputs("\n", stdout);
176         }
177
178         _EXTERN_FUNC_EXIT;
179         return 1;
180 }
181
182 int add_jsession_in_httpheader(GList * recv_header, char *temp_jsessionid, char **real_jsessionid, char **real_other_cookie)
183 {
184         _EXTERN_FUNC_ENTER;
185         DM_ERROR ret = DM_OK;
186
187         char *start_jessionid_msg = NULL;
188         _DEBUG_INFO(" set cookie is : %s \n", temp_jsessionid);
189
190         /*temp_jsessionid = "JSESSIONID=D15C621FD39535094F1B000307D5D520.sdm1; Path=/v1/sdm; Secure" */
191         if (temp_jsessionid != NULL) {
192
193                 _DEBUG_INFO("-------------------------------------------------------------------------------------------------------------------------------------------------");
194                 _DEBUG_INFO("cookie is = %s\n", temp_jsessionid);
195                 _DEBUG_INFO("-------------------------------------------------------------------------------------------------------------------------------------------------");
196
197                 start_jessionid_msg = strstr(temp_jsessionid, "JSESSIONID");
198                 if (start_jessionid_msg != NULL) {
199                         _DEBUG_INFO(" set cookie is : %s \n", start_jessionid_msg);
200                         (*real_jsessionid) = strdup(start_jessionid_msg);
201
202                         _DEBUG_INFO(" set cookie is : %s \n", (*real_jsessionid));
203                 } else {
204                         ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
205                 }
206
207         } else {
208                 ret = COMMON_ERR_INTERNAL_NOT_DEFINED;
209         }
210
211         _EXTERN_FUNC_EXIT;
212         return ret;
213 }