Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * | 2 * |
| 3 * Connection Manager | 3 * Connection Manager |
| 4 * | 4 * |
| 5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved. | 5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved. |
| 6 * | 6 * |
| 7 * This program is free software; you can redistribute it and/or modify | 7 * This program is free software; you can redistribute it and/or modify |
| 8 * it under the terms of the GNU General Public License version 2 as | 8 * it under the terms of the GNU General Public License version 2 as |
| 9 * published by the Free Software Foundation. | 9 * published by the Free Software Foundation. |
| 10 * | 10 * |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 * Remove a previously registered storage module | 64 * Remove a previously registered storage module |
| 65 */ | 65 */ |
| 66 void connman_storage_unregister(struct connman_storage *storage) | 66 void connman_storage_unregister(struct connman_storage *storage) |
| 67 { | 67 { |
| 68 _DBG_STORAGE("storage %p name %s", storage, storage->name); | 68 _DBG_STORAGE("storage %p name %s", storage, storage->name); |
| 69 | 69 |
| 70 storage_list = g_slist_remove(storage_list, storage); | 70 storage_list = g_slist_remove(storage_list, storage); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void __connman_storage_set_encrypted_value(GKeyFile *keyfile, | 73 void __connman_storage_set_encrypted_value(GKeyFile *keyfile, |
| 74 const char *profile_name, const char *section, const char *key, | 74 const char *section, const char *key, const char *value) |
| 75 const char *value) | |
| 76 { | 75 { |
| 77 char *cryptvalue = __connman_crypto_encrypt_keyvalue( | 76 char *cryptvalue = __connman_crypto_encrypt_keyvalue(key, value); |
| 78 profile_name, key, value); | |
| 79 g_key_file_set_string(keyfile, section, key, cryptvalue); | 77 g_key_file_set_string(keyfile, section, key, cryptvalue); |
| 80 g_free(cryptvalue); | 78 g_free(cryptvalue); |
| 81 } | 79 } |
| 82 | 80 |
| 83 char *__connman_storage_get_encrypted_value(GKeyFile *keyfile, | 81 char *__connman_storage_get_encrypted_value(GKeyFile *keyfile, |
| 84 const char *profile_name, const char *section, const char *key) | 82 const char *section, const char *key) |
| 85 { | 83 { |
| 86 char *ciphertext = g_key_file_get_string(keyfile, section, key, NULL); | 84 char *ciphertext, *plaintext; |
| 85 | |
| 86 ciphertext = g_key_file_get_string(keyfile, section, key, NULL); | |
| 87 if (ciphertext == NULL) | 87 if (ciphertext == NULL) |
| 88 return NULL; | 88 return NULL; |
| 89 | 89 |
| 90 char *plaintext = __connman_crypto_decrypt_keyvalue( | 90 plaintext = __connman_crypto_decrypt_keyvalue(key, ciphertext); |
| 91 profile_name, key, ciphertext); | |
| 92 g_free(ciphertext); | 91 g_free(ciphertext); |
| 93 | 92 |
| 94 return plaintext; | 93 return plaintext; |
| 95 } | 94 } |
| 96 | 95 |
| 97 GKeyFile *__connman_storage_open(const char *ident) | 96 #include <stdio.h> |
|
Nathan Williams
2011/03/09 21:28:10
What's this doing all the way down here?
Sam Leffler
2011/03/09 22:31:14
Moved to the top. It was added for snprintf use i
| |
| 97 | |
| 98 #define»STORAGE_HOMEDIR "/home/%s/flimflam" | |
| 99 | |
| 100 static const char *__getpath(const struct connman_storage_ident *ident) | |
| 101 { | |
| 102 » static char path[80]; | |
| 103 | |
| 104 » if (ident->user != NULL) { | |
| 105 » » /* TODO(sleffler) worth using getpwnam & co? */ | |
| 106 » » /* TODO(sleffler) at least use #define */ | |
| 107 » » snprintf(path, sizeof(path), STORAGE_HOMEDIR "/%s.profile", | |
| 108 » » ident->user, ident->ident); | |
| 109 » } else { | |
| 110 » » snprintf(path, sizeof(path), STORAGEDIR "/%s.profile", | |
| 111 » » ident->ident); | |
| 112 » } | |
| 113 » return path; | |
| 114 } | |
| 115 | |
| 116 static char *getpath(const struct connman_storage_ident *ident) | |
| 117 { | |
| 118 » if (ident->user != NULL) { | |
| 119 » » /* TODO(sleffler) worth using getpwnam & co? */ | |
| 120 » » /* TODO(sleffler) at least use #define */ | |
| 121 » » return g_strdup_printf(STORAGE_HOMEDIR "/%s.profile", | |
| 122 » » ident->user, ident->ident); | |
| 123 » } else { | |
| 124 » » return g_strdup_printf(STORAGEDIR "/%s.profile", ident->ident); | |
| 125 » } | |
| 126 } | |
| 127 | |
| 128 gboolean __connman_storage_exists(const struct connman_storage_ident *ident) | |
| 129 { | |
| 130 » gchar *pathname, *data = NULL; | |
| 131 » gboolean result; | |
| 132 » gsize length; | |
| 133 | |
| 134 » _DBG_STORAGE("ident %s", __getpath(ident)); | |
| 135 | |
| 136 » pathname = getpath(ident); | |
| 137 » if (pathname == NULL) | |
| 138 » » return FALSE; | |
| 139 | |
| 140 » result = g_file_get_contents(pathname, &data, &length, NULL); | |
| 141 | |
| 142 » g_free(pathname); | |
| 143 » g_free(data); | |
| 144 | |
| 145 » return result; | |
| 146 } | |
| 147 | |
| 148 GKeyFile *__connman_storage_open(const struct connman_storage_ident *ident) | |
| 98 { | 149 { |
| 99 GKeyFile *keyfile; | 150 GKeyFile *keyfile; |
| 100 gchar *pathname, *data = NULL; | 151 gchar *pathname, *data = NULL; |
| 101 gboolean result; | 152 gboolean result; |
| 102 gsize length; | 153 gsize length; |
| 103 | 154 |
| 104 » _DBG_STORAGE("ident %s", ident); | 155 » _DBG_STORAGE("ident %s", __getpath(ident)); |
| 105 | 156 |
| 106 » pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident); | 157 » pathname = getpath(ident); |
| 107 if (pathname == NULL) | 158 if (pathname == NULL) |
| 108 return NULL; | 159 return NULL; |
| 109 | 160 |
| 110 result = g_file_get_contents(pathname, &data, &length, NULL); | 161 result = g_file_get_contents(pathname, &data, &length, NULL); |
| 111 | 162 |
| 112 g_free(pathname); | 163 g_free(pathname); |
| 113 | 164 |
| 114 keyfile = g_key_file_new(); | 165 keyfile = g_key_file_new(); |
| 115 | 166 |
| 116 if (result == FALSE) | 167 if (result == FALSE) |
| 117 goto done; | 168 goto done; |
| 118 | 169 |
| 119 if (length > 0) | 170 if (length > 0) |
| 120 g_key_file_load_from_data(keyfile, data, length, 0, NULL); | 171 g_key_file_load_from_data(keyfile, data, length, 0, NULL); |
| 121 | 172 |
| 122 g_free(data); | 173 g_free(data); |
| 123 | 174 |
| 124 done: | 175 done: |
| 125 _DBG_STORAGE("keyfile %p", keyfile); | 176 _DBG_STORAGE("keyfile %p", keyfile); |
| 126 | 177 |
| 127 return keyfile; | 178 return keyfile; |
| 128 } | 179 } |
| 129 | 180 |
| 130 void __connman_storage_close(const char *ident, | 181 void __connman_storage_close(const struct connman_storage_ident *ident, |
| 131 GKeyFile *keyfile, gboolean save) | 182 GKeyFile *keyfile, gboolean save) |
| 132 { | 183 { |
| 133 gchar *pathname, *data = NULL; | 184 gchar *pathname, *data = NULL; |
| 134 gsize length = 0; | 185 gsize length = 0; |
| 135 | 186 |
| 136 » _DBG_STORAGE("ident %s keyfile %p save %d", ident, keyfile, save); | 187 » _DBG_STORAGE("ident %s keyfile %p save %d", __getpath(ident), |
| 188 » keyfile, save); | |
| 137 | 189 |
| 138 if (save == FALSE) { | 190 if (save == FALSE) { |
| 139 g_key_file_free(keyfile); | 191 g_key_file_free(keyfile); |
| 140 return; | 192 return; |
| 141 } | 193 } |
| 142 | 194 |
| 143 » pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident); | 195 » pathname = getpath(ident); |
| 144 if (pathname == NULL) | 196 if (pathname == NULL) |
| 145 return; | 197 return; |
| 146 | 198 |
| 147 data = g_key_file_to_data(keyfile, &length, NULL); | 199 data = g_key_file_to_data(keyfile, &length, NULL); |
| 148 | 200 |
| 149 if (g_file_set_contents(pathname, data, length, NULL) == FALSE) | 201 if (g_file_set_contents(pathname, data, length, NULL) == FALSE) |
| 150 » » connman_error("Failed to store information"); | 202 » » connman_error("%s: failed to store data for %s:%s", |
| 203 » » __func__, ident->user, ident->ident); | |
| 151 | 204 |
| 152 g_free(data); | 205 g_free(data); |
| 153 | 206 |
| 154 g_free(pathname); | 207 g_free(pathname); |
| 155 | 208 |
| 156 g_key_file_free(keyfile); | 209 g_key_file_free(keyfile); |
| 157 } | 210 } |
| 158 | 211 |
| 159 void __connman_storage_delete(const char *ident) | 212 void __connman_storage_delete(const struct connman_storage_ident *ident) |
| 160 { | 213 { |
| 161 gchar *pathname; | 214 gchar *pathname; |
| 162 | 215 |
| 163 » _DBG_STORAGE("ident %s", ident); | 216 » _DBG_STORAGE("ident %s", __getpath(ident)); |
| 164 | 217 |
| 165 » pathname = g_strdup_printf("%s/%s.profile", STORAGEDIR, ident); | 218 » pathname = getpath(ident); |
| 166 if (pathname == NULL) | 219 if (pathname == NULL) |
| 167 return; | 220 return; |
| 168 | 221 |
| 169 if (unlink(pathname) < 0) | 222 if (unlink(pathname) < 0) |
| 170 connman_error("Failed to remove %s", pathname); | 223 connman_error("Failed to remove %s", pathname); |
| 224 | |
| 225 g_free(pathname); | |
| 171 } | 226 } |
| 172 | 227 |
| 173 int __connman_storage_init_profile(void) | 228 int __connman_storage_init_profile(void) |
| 174 { | 229 { |
| 175 GSList *list; | 230 GSList *list; |
| 176 | 231 |
| 177 _DBG_STORAGE(""); | 232 _DBG_STORAGE(""); |
| 178 | 233 |
| 179 for (list = storage_list; list; list = list->next) { | 234 for (list = storage_list; list; list = list->next) { |
| 180 struct connman_storage *storage = list->data; | 235 struct connman_storage *storage = list->data; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 | 272 |
| 218 if (storage->profile_save) { | 273 if (storage->profile_save) { |
| 219 if (storage->profile_save(profile) == 0) | 274 if (storage->profile_save(profile) == 0) |
| 220 return 0; | 275 return 0; |
| 221 } | 276 } |
| 222 } | 277 } |
| 223 | 278 |
| 224 return -ENOENT; | 279 return -ENOENT; |
| 225 } | 280 } |
| 226 | 281 |
| 227 int __connman_storage_load_service(struct connman_service *service) | 282 /* |
| 283 * Load/Save object support. Walk the list of registered | |
| 284 * storage implementors until we find one that has a handler | |
| 285 * for the object type. The storage implementor template | |
| 286 * has per-object types (apparently) but these are not used | |
| 287 * at the moment. | |
| 288 */ | |
| 289 #define»DOLOAD(func, arg, profile_ident) do {» » » » \ | |
| 290 » int err;» » » » » » » \ | |
| 291 » GKeyFile *keyfile = __connman_storage_open(profile_ident);» \ | |
| 292 » if (keyfile == NULL)» » » » » » \ | |
| 293 » » return -ENXIO;» » » » » » \ | |
| 294 » err = func(arg, keyfile);» » » » » \ | |
| 295 » __connman_storage_close(profile_ident, keyfile, FALSE);»» \ | |
| 296 » return err;» » » » » » » \ | |
| 297 } while (0) | |
| 298 | |
| 299 #define»DOSAVE(func, arg, profile_ident) do {» » » » \ | |
| 300 » int err;» » » » » » » \ | |
| 301 » GKeyFile *keyfile = __connman_storage_open(profile_ident);» \ | |
| 302 » if (keyfile == NULL)» » » » » » \ | |
| 303 » » return -ENXIO;» » » » » » \ | |
| 304 » err = func(arg, keyfile);» » » » » \ | |
| 305 » /* NB: always write-back for compatibility */» » » \ | |
| 306 » __connman_storage_close(profile_ident, keyfile, TRUE);» » \ | |
| 307 » return err;» » » » » » » \ | |
| 308 } while (0) | |
| 309 | |
| 310 int __connman_storage_load_service(struct connman_service *service, | |
| 311 const struct connman_storage_ident *profile_ident) | |
| 228 { | 312 { |
| 229 GSList *list; | 313 GSList *list; |
| 230 | 314 |
| 231 _DBG_STORAGE("service %p", service); | 315 _DBG_STORAGE("service %p", service); |
| 232 | 316 |
| 233 for (list = storage_list; list; list = list->next) { | 317 for (list = storage_list; list; list = list->next) { |
| 234 struct connman_storage *storage = list->data; | 318 struct connman_storage *storage = list->data; |
| 235 | 319 |
| 236 » » if (storage->service_load) { | 320 » » if (storage->service_load) |
| 237 » » » if (storage->service_load(service) == 0) | 321 » » » DOLOAD(storage->service_load, service, profile_ident); |
| 238 » » » » return 0; | |
| 239 » » } | |
| 240 } | 322 } |
| 241 | |
| 242 return -ENOENT; | 323 return -ENOENT; |
| 243 } | 324 } |
| 244 | 325 |
| 245 int __connman_storage_save_service(struct connman_service *service) | 326 int __connman_storage_save_service(struct connman_service *service, |
| 327 const struct connman_storage_ident *profile_ident) | |
| 246 { | 328 { |
| 247 GSList *list; | 329 GSList *list; |
| 248 | 330 |
| 249 _DBG_STORAGE("service %p", service); | 331 _DBG_STORAGE("service %p", service); |
| 250 | 332 |
| 251 for (list = storage_list; list; list = list->next) { | 333 for (list = storage_list; list; list = list->next) { |
| 252 struct connman_storage *storage = list->data; | 334 struct connman_storage *storage = list->data; |
| 253 | 335 |
| 254 » » if (storage->service_save) { | 336 » » if (storage->service_save) |
| 255 » » » if (storage->service_save(service) == 0) | 337 » » » DOSAVE(storage->service_save, service, profile_ident); |
| 256 » » » » return 0; | |
| 257 » » } | |
| 258 } | 338 } |
| 259 | |
| 260 return -ENOENT; | 339 return -ENOENT; |
| 261 } | 340 } |
| 262 | 341 |
| 263 int __connman_storage_load_device(struct connman_device *device) | 342 int __connman_storage_load_device(struct connman_device *device, |
| 343 const struct connman_storage_ident *profile_ident) | |
| 264 { | 344 { |
| 265 GSList *list; | 345 GSList *list; |
| 266 | 346 |
| 267 _DBG_STORAGE("device %p", device); | 347 _DBG_STORAGE("device %p", device); |
| 268 | 348 |
| 269 for (list = storage_list; list; list = list->next) { | 349 for (list = storage_list; list; list = list->next) { |
| 270 struct connman_storage *storage = list->data; | 350 struct connman_storage *storage = list->data; |
| 271 | 351 |
| 272 » » if (storage->device_load) { | 352 » » if (storage->device_load) |
| 273 » » » if (storage->device_load(device) == 0) | 353 » » » DOLOAD(storage->device_load, device, profile_ident); |
| 274 » » » » return 0; | |
| 275 » » } | |
| 276 } | 354 } |
| 277 | |
| 278 return -ENOENT; | 355 return -ENOENT; |
| 279 } | 356 } |
| 280 | 357 |
| 281 int __connman_storage_save_device(struct connman_device *device) | 358 int __connman_storage_save_device(struct connman_device *device, |
| 359 const struct connman_storage_ident *profile_ident) | |
| 282 { | 360 { |
| 283 GSList *list; | 361 GSList *list; |
| 284 | 362 |
| 285 _DBG_STORAGE("device %p", device); | 363 _DBG_STORAGE("device %p", device); |
| 286 | 364 |
| 287 for (list = storage_list; list; list = list->next) { | 365 for (list = storage_list; list; list = list->next) { |
| 288 struct connman_storage *storage = list->data; | 366 struct connman_storage *storage = list->data; |
| 289 | 367 |
| 290 » » if (storage->device_save) { | 368 » » if (storage->device_save) |
| 291 » » » if (storage->device_save(device) == 0) | 369 » » » DOSAVE(storage->device_save, device, profile_ident); |
| 292 » » » » return 0; | |
| 293 » » } | |
| 294 } | 370 } |
| 295 | |
| 296 return -ENOENT; | 371 return -ENOENT; |
| 297 } | 372 } |
| 298 | 373 |
| 299 int __connman_storage_load_ipconfig(struct connman_ipconfig *ipconfig) | 374 int __connman_storage_load_ipconfig(struct connman_ipconfig *ipconfig, |
| 375 const struct connman_storage_ident *profile_ident) | |
| 300 { | 376 { |
| 301 GSList *list; | 377 GSList *list; |
| 302 | 378 |
| 303 _DBG_STORAGE("ipconfig %p", ipconfig); | 379 _DBG_STORAGE("ipconfig %p", ipconfig); |
| 304 | 380 |
| 305 for (list = storage_list; list; list = list->next) { | 381 for (list = storage_list; list; list = list->next) { |
| 306 struct connman_storage *storage = list->data; | 382 struct connman_storage *storage = list->data; |
| 307 | 383 |
| 308 » » if (storage->ipconfig_load) { | 384 » » if (storage->ipconfig_load) |
| 309 » » » if (storage->ipconfig_load(ipconfig) == 0) | 385 » » » DOLOAD(storage->ipconfig_load, ipconfig, profile_ident); |
| 310 » » » » return 0; | |
| 311 » » } | |
| 312 } | 386 } |
| 313 | |
| 314 return -ENOENT; | 387 return -ENOENT; |
| 315 } | 388 } |
| 316 | 389 |
| 317 int __connman_storage_save_ipconfig(const struct connman_ipconfig *ipconfig) | 390 int __connman_storage_save_ipconfig(const struct connman_ipconfig *ipconfig, |
| 391 const struct connman_storage_ident *profile_ident) | |
| 318 { | 392 { |
| 319 GSList *list; | 393 GSList *list; |
| 320 | 394 |
| 321 _DBG_STORAGE("ipconfig %p", ipconfig); | 395 _DBG_STORAGE("ipconfig %p", ipconfig); |
| 322 | 396 |
| 323 for (list = storage_list; list; list = list->next) { | 397 for (list = storage_list; list; list = list->next) { |
| 324 struct connman_storage *storage = list->data; | 398 struct connman_storage *storage = list->data; |
| 325 | 399 |
| 326 » » if (storage->ipconfig_save) { | 400 » » if (storage->ipconfig_save) |
| 327 » » » if (storage->ipconfig_save(ipconfig) == 0) | 401 » » » DOSAVE(storage->ipconfig_save, ipconfig, profile_ident); |
| 328 » » » » return 0; | |
| 329 » » } | |
| 330 } | 402 } |
| 331 | |
| 332 return -ENOENT; | 403 return -ENOENT; |
| 333 } | 404 } |
| 334 | 405 |
| 335 int __connman_storage_init(void) | 406 int __connman_storage_init(void) |
| 336 { | 407 { |
| 337 _DBG_STORAGE(""); | 408 _DBG_STORAGE(""); |
| 338 | 409 |
| 339 return 0; | 410 return 0; |
| 340 } | 411 } |
| 341 | 412 |
| 342 void __connman_storage_cleanup(void) | 413 void __connman_storage_cleanup(void) |
| 343 { | 414 { |
| 344 _DBG_STORAGE(""); | 415 _DBG_STORAGE(""); |
| 345 } | 416 } |
| OLD | NEW |