vpn-provider: Send domain name to connman when connection is ready
[platform/upstream/connman.git] / vpn / vpn-provider.c
index 8606adc..513d926 100644 (file)
@@ -53,6 +53,7 @@ struct vpn_route {
 
 struct vpn_setting {
        gboolean hide_value;
+       gboolean immutable;
        char *value;
 };
 
@@ -82,6 +83,7 @@ struct vpn_provider {
        guint notify_id;
        char *config_file;
        char *config_entry;
+       connman_bool_t immutable;
 };
 
 static void append_properties(DBusMessageIter *iter,
@@ -406,6 +408,9 @@ static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
 
        DBG("conn %p", conn);
 
+       if (provider->immutable == TRUE)
+               return __connman_error_not_supported(msg);
+
        if (dbus_message_iter_init(msg, &iter) == FALSE)
                return __connman_error_invalid_arguments(msg);
 
@@ -438,8 +443,12 @@ static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg,
                                send_routes(provider, provider->user_routes,
                                                                "UserRoutes");
                }
-       } else
-               return __connman_error_invalid_property(msg);
+       } else {
+               const char *str;
+
+               dbus_message_iter_get_basic(&value, &str);
+               vpn_provider_set_string(provider, name, str);
+       }
 
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
@@ -452,6 +461,9 @@ static DBusMessage *clear_property(DBusConnection *conn, DBusMessage *msg,
 
        DBG("conn %p", conn);
 
+       if (provider->immutable == TRUE)
+               return __connman_error_not_supported(msg);
+
        dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name,
                                                        DBUS_TYPE_INVALID);
 
@@ -822,7 +834,16 @@ static int vpn_provider_save(struct vpn_provider *provider)
 {
        GKeyFile *keyfile;
 
-       DBG("provider %p", provider);
+       DBG("provider %p immutable %s", provider,
+                                       provider->immutable ? "yes" : "no");
+
+       if (provider->immutable == TRUE) {
+               /*
+                * Do not save providers that are provisioned via .config
+                * file.
+                */
+               return -EPERM;
+       }
 
        keyfile = g_key_file_new();
        if (keyfile == NULL)
@@ -1006,8 +1027,6 @@ static void configuration_count_del(void)
 
        if (__sync_fetch_and_sub(&configuration_count, 1) != 1)
                return;
-
-       raise(SIGTERM);
 }
 
 int __vpn_provider_disconnect(struct vpn_provider *provider)
@@ -1215,6 +1234,27 @@ static const char *state2string(enum vpn_provider_state state)
        return NULL;
 }
 
+static void append_nameservers(DBusMessageIter *iter, char **servers)
+{
+       int i;
+
+       DBG("%p", servers);
+
+       for (i = 0; servers[i] != NULL; i++) {
+               DBG("servers[%d] %s", i, servers[i]);
+               dbus_message_iter_append_basic(iter,
+                                       DBUS_TYPE_STRING, &servers[i]);
+       }
+}
+
+static void append_dns(DBusMessageIter *iter, void *user_data)
+{
+       struct vpn_provider *provider = user_data;
+
+       if (provider->nameservers != NULL)
+               append_nameservers(iter, provider->nameservers);
+}
+
 static int provider_indicate_state(struct vpn_provider *provider,
                                enum vpn_provider_state state)
 {
@@ -1242,6 +1282,19 @@ static int provider_indicate_state(struct vpn_provider *provider,
                        connman_dbus_property_changed_dict(provider->path,
                                        VPN_CONNECTION_INTERFACE, "IPv6",
                                        append_ipv6, provider);
+
+               connman_dbus_property_changed_array(provider->path,
+                                               VPN_CONNECTION_INTERFACE,
+                                               "Nameservers",
+                                               DBUS_TYPE_STRING,
+                                               append_dns, provider);
+
+               if (provider->domain != NULL)
+                       connman_dbus_property_changed_basic(provider->path,
+                                               VPN_CONNECTION_INTERFACE,
+                                               "Domain",
+                                               DBUS_TYPE_STRING,
+                                               &provider->domain);
        }
 
        if (old_state != state)
@@ -1259,27 +1312,6 @@ static int provider_indicate_state(struct vpn_provider *provider,
        return 0;
 }
 
-static void append_nameservers(DBusMessageIter *iter, char **servers)
-{
-       int i;
-
-       DBG("%p", servers);
-
-       for (i = 0; servers[i] != NULL; i++) {
-               DBG("servers[%d] %s", i, servers[i]);
-               dbus_message_iter_append_basic(iter,
-                                       DBUS_TYPE_STRING, &servers[i]);
-       }
-}
-
-static void append_dns(DBusMessageIter *iter, void *user_data)
-{
-       struct vpn_provider *provider = user_data;
-
-       if (provider->nameservers != NULL)
-               append_nameservers(iter, provider->nameservers);
-}
-
 static void append_state(DBusMessageIter *iter,
                                        struct vpn_provider *provider)
 {
@@ -1337,6 +1369,9 @@ static void append_properties(DBusMessageIter *iter,
                connman_dbus_dict_append_basic(&dict, "Domain",
                                        DBUS_TYPE_STRING, &provider->domain);
 
+       connman_dbus_dict_append_basic(&dict, "Immutable", DBUS_TYPE_BOOLEAN,
+                                       &provider->immutable);
+
        if (provider->family == AF_INET)
                connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4,
                                                provider);
@@ -1578,6 +1613,7 @@ static void provider_initialize(struct vpn_provider *provider)
        provider->type = NULL;
        provider->domain = NULL;
        provider->identifier = NULL;
+       provider->immutable = FALSE;
        provider->user_networks = NULL;
        provider->routes = g_hash_table_new_full(g_direct_hash, g_direct_equal,
                                        NULL, free_route);
@@ -1865,9 +1901,13 @@ static const char *get_string(GHashTable *settings, const char *key)
 static GSList *parse_user_networks(const char *network_str)
 {
        GSList *networks = NULL;
-       char **elems = g_strsplit(network_str, ",", 0);
+       char **elems;
        int i = 0;
 
+       if (network_str == NULL)
+               return NULL;
+
+       elems = g_strsplit(network_str, ",", 0);
        if (elems == NULL)
                return NULL;
 
@@ -1986,8 +2026,7 @@ int __vpn_provider_create_from_config(GHashTable *settings,
                provider->config_file = g_strdup(config_ident);
                provider->config_entry = g_strdup(config_entry);
 
-               if (provider_register(provider) == 0)
-                       vpn_provider_load(provider);
+               provider_register(provider);
 
                provider_resolv_host_addr(provider);
        }
@@ -2001,7 +2040,9 @@ int __vpn_provider_create_from_config(GHashTable *settings,
        g_hash_table_iter_init(&hash, settings);
 
        while (g_hash_table_iter_next(&hash, &key, &value) == TRUE)
-               vpn_provider_set_string(provider, key, value);
+               __vpn_provider_set_string_immutable(provider, key, value);
+
+       provider->immutable = TRUE;
 
        vpn_provider_save(provider);
 
@@ -2077,9 +2118,11 @@ const char * __vpn_provider_get_ident(struct vpn_provider *provider)
 }
 
 static int set_string(struct vpn_provider *provider,
-               const char *key, const char *value, gboolean hide_value)
+                       const char *key, const char *value,
+                       gboolean hide_value, gboolean immutable)
 {
-       DBG("provider %p key %s value %s", provider, key,
+       DBG("provider %p key %s immutable %s value %s", provider, key,
+               immutable ? "yes" : "no",
                hide_value ? "<not printed>" : value);
 
        if (g_str_equal(key, "Type") == TRUE) {
@@ -2102,13 +2145,23 @@ static int set_string(struct vpn_provider *provider,
        } else {
                struct vpn_setting *setting;
 
-               setting = g_try_new(struct vpn_setting, 1);
+               setting = g_hash_table_lookup(provider->setting_strings, key);
+               if (setting != NULL && immutable == FALSE &&
+                                               setting->immutable == TRUE) {
+                       DBG("Trying to set immutable variable %s", key);
+                       return -EPERM;
+               }
+
+               setting = g_try_new0(struct vpn_setting, 1);
                if (setting == NULL)
                        return -ENOMEM;
 
                setting->value = g_strdup(value);
                setting->hide_value = hide_value;
 
+               if (immutable == TRUE)
+                       setting->immutable = TRUE;
+
                if (hide_value == FALSE)
                        send_value(provider->path, key, setting->value);
 
@@ -2122,13 +2175,19 @@ static int set_string(struct vpn_provider *provider,
 int vpn_provider_set_string(struct vpn_provider *provider,
                                        const char *key, const char *value)
 {
-       return set_string(provider, key, value, FALSE);
+       return set_string(provider, key, value, FALSE, FALSE);
 }
 
 int vpn_provider_set_string_hide_value(struct vpn_provider *provider,
                                        const char *key, const char *value)
 {
-       return set_string(provider, key, value, TRUE);
+       return set_string(provider, key, value, TRUE, FALSE);
+}
+
+int __vpn_provider_set_string_immutable(struct vpn_provider *provider,
+                                       const char *key, const char *value)
+{
+       return set_string(provider, key, value, FALSE, TRUE);
 }
 
 const char *vpn_provider_get_string(struct vpn_provider *provider,
@@ -2463,28 +2522,6 @@ void vpn_provider_driver_unregister(struct vpn_provider_driver *driver)
        }
 }
 
-static gboolean check_vpn_count(gpointer data)
-{
-       if (configuration_count == 0) {
-               connman_info("No VPN configurations found, quitting.");
-               raise(SIGTERM);
-       }
-
-       return FALSE;
-}
-
-void __vpn_provider_check_connections(void)
-{
-       /*
-        * If we were started when there is no providers configured,
-        * then just quit. This happens when connman starts and its
-        * vpn plugin asks connman-vpnd if it has any connections
-        * configured. If there are none, then we can stop the vpn
-        * daemon.
-        */
-       g_timeout_add(1000, check_vpn_count, NULL);
-}
-
 const char *vpn_provider_get_name(struct vpn_provider *provider)
 {
        return provider->name;