Initialize Tizen 2.3
[framework/connectivity/net-config.git] / mobile / src / wifi-eap-config.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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 <unistd.h>
22
23 #include "wifi-eap-config.h"
24 #include "log.h"
25 #include "wifi.h"
26
27 static char *__get_ssid(const char *name)
28 {
29         char *buf = NULL;
30         char buf_tmp[32] = {0,};
31         int i = 0;
32         int len = 0;
33
34         if (NULL == name)
35                 return NULL;
36
37         len = strlen(name);
38
39         buf = g_try_malloc0(len * 2 + 1);
40         if (buf == NULL)
41                 return NULL;
42
43         for (i = 0; i < len; i++) {
44                 snprintf(buf_tmp, 3, "%02x", name[i]);
45                 strcat(buf, buf_tmp);
46         }
47
48         DBG("SSID - [%s]\n", buf);
49
50         return buf;
51 }
52
53 static gboolean __config_save(GKeyFile *keyfile, char *file_name)
54 {
55         gchar *data = NULL;
56         gsize length = 0;
57         FILE *file = NULL;
58         int ret = TRUE;
59
60         data = g_key_file_to_data(keyfile, &length, NULL);
61         DBG("Data lenght-[%d]", length);
62
63         file = fopen(file_name, "w");
64         if (NULL == file) {
65                 DBG("fopen() fails!");
66                 ret = FALSE;
67         } else {
68                 fputs(data, file);
69                 fclose(file);
70                 DBG("Wrote data successfully to [%s] file!", file_name);
71         }
72
73         g_free(data);
74
75         return ret;
76 }
77
78 static gboolean __config_delete(const char *ssid)
79 {
80         gchar *config_file = NULL;
81         gboolean ret = FALSE;
82
83         config_file = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR,
84                         ssid);
85         if(config_file == NULL)
86                 return FALSE;
87
88         if (g_file_test(config_file, G_FILE_TEST_EXISTS) == FALSE) {
89                 ret = TRUE;
90         } else if (g_file_test(config_file, G_FILE_TEST_IS_REGULAR) == TRUE) {
91                 unlink(config_file);
92                 ret = TRUE;
93         }
94
95         g_free(config_file);
96
97         return ret;
98 }
99
100 gboolean netconfig_iface_wifi_create_config(NetconfigWifi *wifi,
101                 GHashTable *fields, GError **error)
102 {
103         DBG("netconfig_iface_wifi_create_config");
104         g_return_val_if_fail(wifi != NULL, FALSE);
105
106         gboolean ret = TRUE;
107         GKeyFile *keyfile = NULL;
108         GHashTableIter iter;
109         gpointer field, value;
110         gchar *file_name = NULL;
111         gchar *ssid_hex = NULL;
112         gchar *grp_name = NULL;
113
114         g_hash_table_iter_init(&iter, fields);
115         while (g_hash_table_iter_next(&iter, &field, &value)) {
116                 if (NULL != value) {
117                         if (!strcmp(field, CONNMAN_CONFIG_FIELD_NAME)) {
118                                 ssid_hex = __get_ssid(value);
119                                 break;
120                         } else if (!strcmp(field, CONNMAN_CONFIG_FIELD_SSID)) {
121                                 ssid_hex = g_strdup_printf("%s",
122                                                 (gchar *)value);
123                                 break;
124                         }
125                 }
126         }
127
128         if (NULL == ssid_hex) {
129                 DBG("Fail! Could not fetch the ssid");
130                 return FALSE;
131         }
132
133         /* Create unique service group name */
134         grp_name = g_strdup_printf("service_%s", ssid_hex);
135         if(NULL == grp_name) {
136                 DBG("Fail! Could not create the service group name");
137                 g_free(ssid_hex);
138                 return FALSE;
139         }
140
141         keyfile = g_key_file_new();
142         if (NULL == keyfile) {
143                 DBG("g_key_file_new() fails!");
144                 g_free(grp_name);
145                 g_free(ssid_hex);
146                 return FALSE;
147         }
148
149         g_hash_table_iter_init(&iter, fields);
150         while (g_hash_table_iter_next(&iter, &field, &value)) {
151                 DBG("Field - [%s] Value - [%s]", field, value);
152
153                 if (NULL != value)
154                         g_key_file_set_string(keyfile, grp_name, field, value);
155         }
156
157         file_name = g_strdup_printf("%s/%s.config", CONNMAN_STORAGEDIR,
158                         ssid_hex);
159         if(NULL == file_name) {
160                 DBG("g_strdup_printf() fails. Could not save config!");
161                 g_key_file_free(keyfile);
162                 g_free(grp_name);
163                 g_free(ssid_hex);
164                 return FALSE;
165         }
166
167         ret = __config_save(keyfile, file_name);
168         if (FALSE == ret)
169                 DBG("Could not save config!");
170         else
171                 DBG("Saved config in [%s] successfully", file_name);
172
173         g_key_file_free(keyfile);
174         g_free(file_name);
175         g_free(grp_name);
176         g_free(ssid_hex);
177
178         return ret;
179 }
180
181 gboolean netconfig_iface_wifi_delete_config(NetconfigWifi *wifi,
182                 gchar *profile, GError **error)
183 {
184         DBG("netconfig_iface_wifi_delete_config");
185         g_return_val_if_fail(wifi != NULL, FALSE);
186
187         gboolean ret = TRUE;
188         char *str1 = NULL;
189         char *str2 = NULL;
190         char *str3 = NULL;
191         char ssid[512] = "";
192         int ssid_len = 0;
193
194         str1 = strstr(profile, "wifi_");
195         if (NULL != str1) {
196                 str2 = strchr(str1 + 5, '_');
197                 if (NULL != str2) {
198                         str3 = strchr(str2 + 1, '_');
199                         ssid_len = str3 - str2 - 1;
200                         strncpy(ssid, str2 + 1, ssid_len);
201                         DBG("ssid_len - [%d] SSID - [%s]", ssid_len, ssid);
202
203                         ret = __config_delete(ssid);
204                         if (TRUE == ret)
205                                 DBG("Deleted the config file successfully");
206                         else
207                                 DBG("Deletion of config file failed");
208                 } else {
209                         DBG("Fetching of SSID fails");
210                 }
211         } else {
212                 DBG("Fetching of SSID fails");
213         }
214
215         return ret;
216 }