Don't write to wpa_supplicant.conf directly
[mech_eap.git] / wpa_supplicant / config_file.c
index 246db0d..cdc6e39 100644 (file)
@@ -11,6 +11,9 @@
  */
 
 #include "includes.h"
+#ifdef ANDROID
+#include <sys/stat.h>
+#endif /* ANDROID */
 
 #include "common.h"
 #include "config.h"
@@ -608,7 +611,7 @@ static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
        int res;
 
        res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
-       if (res < 0 || (size_t) res >= sizeof(field))
+       if (os_snprintf_error(sizeof(field), res))
                return;
        value = wpa_config_get(ssid, field);
        if (value) {
@@ -691,6 +694,7 @@ static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
        STR(subject_match);
        STR(altsubject_match);
        STR(domain_suffix_match);
+       STR(domain_match);
        STR(ca_cert2);
        STR(ca_path2);
        STR(client_cert2);
@@ -700,6 +704,7 @@ static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
        STR(subject_match2);
        STR(altsubject_match2);
        STR(domain_suffix_match2);
+       STR(domain_match2);
        STR(phase1);
        STR(phase2);
        STR(pcsc);
@@ -716,6 +721,7 @@ static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
        INTe(engine);
        INTe(engine2);
        INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
+       INTe(erp);
 #endif /* IEEE8021X_EAPOL */
        for (i = 0; i < 4; i++)
                write_wep_key(f, i, ssid);
@@ -753,8 +759,11 @@ static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
 #endif /* CONFIG_HS20 */
        write_int(f, "mac_addr", ssid->mac_addr, -1);
 #ifdef CONFIG_MESH
-       STR(mesh_ht_mode);
        STR(mesh_basic_rates);
+       INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
+       INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
+       INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
+       INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
 #endif /* CONFIG_MESH */
 
 #undef STR
@@ -1211,6 +1220,19 @@ static void wpa_config_write_global(FILE *f, struct wpa_config *config)
 
        if (config->user_mpm != DEFAULT_USER_MPM)
                fprintf(f, "user_mpm=%d\n", config->user_mpm);
+
+       if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
+               fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
+
+       if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
+               fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
+
+       if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
+               fprintf(f, "mesh_max_inactivity=%d\n",
+                       config->mesh_max_inactivity);
+
+       if (config->passive_scan)
+               fprintf(f, "cert_in_cb=%d\n", config->passive_scan);
 }
 
 #endif /* CONFIG_NO_CONFIG_WRITE */
@@ -1226,12 +1248,21 @@ int wpa_config_write(const char *name, struct wpa_config *config)
        struct wpa_config_blob *blob;
 #endif /* CONFIG_NO_CONFIG_BLOBS */
        int ret = 0;
+       const char *orig_name = name;
+       int tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
+       char *tmp_name = os_malloc(tmp_len);
+
+       if (tmp_name) {
+               os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
+               name = tmp_name;
+       }
 
        wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
 
        f = fopen(name, "w");
        if (f == NULL) {
                wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
+               os_free(tmp_name);
                return -1;
        }
 
@@ -1266,8 +1297,21 @@ int wpa_config_write(const char *name, struct wpa_config *config)
 
        fclose(f);
 
+       if (tmp_name) {
+               int chmod_ret = 0;
+
+#ifdef ANDROID
+               chmod_ret = chmod(tmp_name,
+                                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+#endif /* ANDROID */
+               if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
+                       ret = -1;
+
+               os_free(tmp_name);
+       }
+
        wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
-                  name, ret ? "un" : "");
+                  orig_name, ret ? "un" : "");
        return ret;
 #else /* CONFIG_NO_CONFIG_WRITE */
        return -1;