Base Code merged to SPIN 2.4
[platform/upstream/connman.git] / vpn / vpn-agent.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
6  *
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
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdbus.h>
31 #include <connman/log.h>
32 #include <connman/agent.h>
33 #include <vpn/vpn-provider.h>
34
35 #include "vpn-agent.h"
36 #include "vpn.h"
37
38 bool vpn_agent_check_reply_has_dict(DBusMessage *reply)
39 {
40         const char *signature = DBUS_TYPE_ARRAY_AS_STRING
41                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
42                 DBUS_TYPE_STRING_AS_STRING
43                 DBUS_TYPE_VARIANT_AS_STRING
44                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING;
45
46         if (dbus_message_has_signature(reply, signature))
47                 return true;
48
49         connman_warn("Reply %s to %s from %s has wrong signature %s",
50                         signature,
51                         dbus_message_get_interface(reply),
52                         dbus_message_get_sender(reply),
53                         dbus_message_get_signature(reply));
54
55         return false;
56 }
57
58 static void request_input_append_name(DBusMessageIter *iter, void *user_data)
59 {
60         struct vpn_provider *provider = user_data;
61         const char *str = "string";
62
63         connman_dbus_dict_append_basic(iter, "Type",
64                                 DBUS_TYPE_STRING, &str);
65         str = "informational";
66         connman_dbus_dict_append_basic(iter, "Requirement",
67                                 DBUS_TYPE_STRING, &str);
68
69         str = vpn_provider_get_name(provider);
70         connman_dbus_dict_append_basic(iter, "Value",
71                                 DBUS_TYPE_STRING, &str);
72 }
73
74 static void request_input_append_host(DBusMessageIter *iter, void *user_data)
75 {
76         struct vpn_provider *provider = user_data;
77         const char *str = "string";
78
79         connman_dbus_dict_append_basic(iter, "Type",
80                                 DBUS_TYPE_STRING, &str);
81         str = "informational";
82         connman_dbus_dict_append_basic(iter, "Requirement",
83                                 DBUS_TYPE_STRING, &str);
84
85         str = vpn_provider_get_host(provider);
86         connman_dbus_dict_append_basic(iter, "Value",
87                                 DBUS_TYPE_STRING, &str);
88 }
89
90 void vpn_agent_append_host_and_name(DBusMessageIter *iter,
91                                 struct vpn_provider *provider)
92 {
93         connman_dbus_dict_append_dict(iter, "Host",
94                                 request_input_append_host,
95                                 provider);
96
97         connman_dbus_dict_append_dict(iter, "Name",
98                                 request_input_append_name,
99                                 provider);
100 }
101
102 struct user_info_data {
103         struct vpn_provider *provider;
104         const char *username_str;
105 };
106
107 static void request_input_append_user_info(DBusMessageIter *iter,
108                                                 void *user_data)
109 {
110         struct user_info_data *data = user_data;
111         struct vpn_provider *provider = data->provider;
112         const char *str = "string";
113
114         connman_dbus_dict_append_basic(iter, "Type",
115                                 DBUS_TYPE_STRING, &str);
116         str = "mandatory";
117         connman_dbus_dict_append_basic(iter, "Requirement",
118                                 DBUS_TYPE_STRING, &str);
119
120         if (data->username_str) {
121                 str = vpn_provider_get_string(provider, data->username_str);
122                 if (str)
123                         connman_dbus_dict_append_basic(iter, "Value",
124                                                 DBUS_TYPE_STRING, &str);
125         }
126 }
127
128 void vpn_agent_append_user_info(DBusMessageIter *iter,
129                                 struct vpn_provider *provider,
130                                 const char *username_str)
131 {
132         struct user_info_data data = {
133                 .provider = provider,
134                 .username_str = username_str
135         };
136
137         connman_dbus_dict_append_dict(iter, "Username",
138                                 request_input_append_user_info,
139                                 &data);
140
141         data.username_str = NULL;
142         connman_dbus_dict_append_dict(iter, "Password",
143                                 request_input_append_user_info,
144                                 &data);
145 }