device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-pppoe.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301 USA.
18  *
19  * Copyright 2007 - 2013 Red Hat, Inc.
20  * Copyright 2007 - 2008 Novell, Inc.
21  */
22
23 #include "nm-default.h"
24
25 #include <string.h>
26
27 #include "nm-setting-pppoe.h"
28 #include "nm-setting-ppp.h"
29 #include "nm-setting-private.h"
30 #include "nm-core-enum-types.h"
31
32 /**
33  * SECTION:nm-setting-pppoe
34  * @short_description: Describes PPPoE connection properties
35  *
36  * The #NMSettingPppoe object is a #NMSetting subclass that describes
37  * properties necessary for connection to networks that require PPPoE connections
38  * to provide IP transport, for example cable or DSL modems.
39  **/
40
41 G_DEFINE_TYPE_WITH_CODE (NMSettingPppoe, nm_setting_pppoe, NM_TYPE_SETTING,
42                          _nm_register_setting (PPPOE, 3))
43 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_PPPOE)
44
45 #define NM_SETTING_PPPOE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_PPPOE, NMSettingPppoePrivate))
46
47 typedef struct {
48         char *service;
49         char *username;
50         char *password;
51         NMSettingSecretFlags password_flags;
52 } NMSettingPppoePrivate;
53
54 enum {
55         PROP_0,
56         PROP_SERVICE,
57         PROP_USERNAME,
58         PROP_PASSWORD,
59         PROP_PASSWORD_FLAGS,
60
61         LAST_PROP
62 };
63
64 /**
65  * nm_setting_pppoe_new:
66  *
67  * Creates a new #NMSettingPppoe object with default values.
68  *
69  * Returns: (transfer full): the new empty #NMSettingPppoe object
70  **/
71 NMSetting *
72 nm_setting_pppoe_new (void)
73 {
74         return (NMSetting *) g_object_new (NM_TYPE_SETTING_PPPOE, NULL);
75 }
76
77 /**
78  * nm_setting_pppoe_get_service:
79  * @setting: the #NMSettingPppoe
80  *
81  * Returns: the #NMSettingPppoe:service property of the setting
82  **/
83 const char *
84 nm_setting_pppoe_get_service  (NMSettingPppoe *setting)
85 {
86         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
87
88         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->service;
89 }
90
91 /**
92  * nm_setting_pppoe_get_username:
93  * @setting: the #NMSettingPppoe
94  *
95  * Returns: the #NMSettingPppoe:username property of the setting
96  **/
97 const char *
98 nm_setting_pppoe_get_username (NMSettingPppoe *setting)
99 {
100         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
101
102         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->username;
103 }
104
105 /**
106  * nm_setting_pppoe_get_password:
107  * @setting: the #NMSettingPppoe
108  *
109  * Returns: the #NMSettingPppoe:password property of the setting
110  **/
111 const char *
112 nm_setting_pppoe_get_password (NMSettingPppoe *setting)
113 {
114         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
115
116         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->password;
117 }
118
119 /**
120  * nm_setting_pppoe_get_password_flags:
121  * @setting: the #NMSettingPppoe
122  *
123  * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingPppoe:password
124  **/
125 NMSettingSecretFlags
126 nm_setting_pppoe_get_password_flags (NMSettingPppoe *setting)
127 {
128         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NM_SETTING_SECRET_FLAG_NONE);
129
130         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->password_flags;
131 }
132
133 static gboolean
134 verify (NMSetting *setting, NMConnection *connection, GError **error)
135 {
136         NMSettingPppoePrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (setting);
137
138         if (!priv->username) {
139                 g_set_error_literal (error,
140                                      NM_CONNECTION_ERROR,
141                                      NM_CONNECTION_ERROR_MISSING_PROPERTY,
142                                      _("property is missing"));
143                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_USERNAME);
144                 return FALSE;
145         } else if (!strlen (priv->username)) {
146                 g_set_error_literal (error,
147                                      NM_CONNECTION_ERROR,
148                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
149                                      _("property is empty"));
150                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_USERNAME);
151                 return FALSE;
152         }
153
154         if (priv->service && !strlen (priv->service)) {
155                 g_set_error_literal (error,
156                                      NM_CONNECTION_ERROR,
157                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
158                                      _("property is empty"));
159                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_SERVICE);
160                 return FALSE;
161         }
162
163         return TRUE;
164 }
165
166 static GPtrArray *
167 need_secrets (NMSetting *setting)
168 {
169         NMSettingPppoePrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (setting);
170         GPtrArray *secrets = NULL;
171
172         if (priv->password)
173                 return NULL;
174
175         if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
176                 secrets = g_ptr_array_sized_new (1);
177                 g_ptr_array_add (secrets, NM_SETTING_PPPOE_PASSWORD);
178         }
179
180         return secrets;
181 }
182
183 static void
184 nm_setting_pppoe_init (NMSettingPppoe *setting)
185 {
186 }
187
188 static void
189 set_property (GObject *object, guint prop_id,
190               const GValue *value, GParamSpec *pspec)
191 {
192         NMSettingPppoePrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (object);
193
194         switch (prop_id) {
195         case PROP_SERVICE:
196                 g_free (priv->service);
197                 priv->service = g_value_dup_string (value);
198                 break;
199         case PROP_USERNAME:
200                 g_free (priv->username);
201                 priv->username = g_value_dup_string (value);
202                 break;
203         case PROP_PASSWORD:
204                 g_free (priv->password);
205                 priv->password = g_value_dup_string (value);
206                 break;
207         case PROP_PASSWORD_FLAGS:
208                 priv->password_flags = g_value_get_flags (value);
209                 break;
210         default:
211                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212                 break;
213         }
214 }
215
216 static void
217 get_property (GObject *object, guint prop_id,
218               GValue *value, GParamSpec *pspec)
219 {
220         NMSettingPppoe *setting = NM_SETTING_PPPOE (object);
221
222         switch (prop_id) {
223         case PROP_SERVICE:
224                 g_value_set_string (value, nm_setting_pppoe_get_service (setting));
225                 break;
226         case PROP_USERNAME:
227                 g_value_set_string (value, nm_setting_pppoe_get_username (setting));
228                 break;
229         case PROP_PASSWORD:
230                 g_value_set_string (value, nm_setting_pppoe_get_password (setting));
231                 break;
232         case PROP_PASSWORD_FLAGS:
233                 g_value_set_flags (value, nm_setting_pppoe_get_password_flags (setting));
234                 break;
235         default:
236                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237                 break;
238         }
239 }
240
241 static void
242 finalize (GObject *object)
243 {
244         NMSettingPppoePrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (object);
245
246         g_free (priv->username);
247         g_free (priv->password);
248         g_free (priv->service);
249
250         G_OBJECT_CLASS (nm_setting_pppoe_parent_class)->finalize (object);
251 }
252
253 static void
254 nm_setting_pppoe_class_init (NMSettingPppoeClass *setting_class)
255 {
256         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
257         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
258
259         g_type_class_add_private (setting_class, sizeof (NMSettingPppoePrivate));
260
261         /* virtual methods */
262         object_class->set_property = set_property;
263         object_class->get_property = get_property;
264         object_class->finalize     = finalize;
265         parent_class->verify       = verify;
266         parent_class->need_secrets = need_secrets;
267
268         /* Properties */
269         /**
270          * NMSettingPppoe:service:
271          *
272          * If specified, instruct PPPoE to only initiate sessions with access
273          * concentrators that provide the specified service.  For most providers,
274          * this should be left blank.  It is only required if there are multiple
275          * access concentrators or a specific service is known to be required.
276          **/
277         g_object_class_install_property
278                 (object_class, PROP_SERVICE,
279                  g_param_spec_string (NM_SETTING_PPPOE_SERVICE, "", "",
280                                       NULL,
281                                       G_PARAM_READWRITE |
282                                       G_PARAM_STATIC_STRINGS));
283
284         /**
285          * NMSettingPppoe:username:
286          *
287          * Username used to authenticate with the PPPoE service.
288          **/
289         g_object_class_install_property
290                 (object_class, PROP_USERNAME,
291                  g_param_spec_string (NM_SETTING_PPPOE_USERNAME, "", "",
292                                       NULL,
293                                       G_PARAM_READWRITE |
294                                       G_PARAM_STATIC_STRINGS));
295
296         /**
297          * NMSettingPppoe:password:
298          *
299          * Password used to authenticate with the PPPoE service.
300          **/
301         g_object_class_install_property
302                 (object_class, PROP_PASSWORD,
303                  g_param_spec_string (NM_SETTING_PPPOE_PASSWORD, "", "",
304                                       NULL,
305                                       G_PARAM_READWRITE |
306                                       NM_SETTING_PARAM_SECRET |
307                                       G_PARAM_STATIC_STRINGS));
308
309         /**
310          * NMSettingPppoe:password-flags:
311          *
312          * Flags indicating how to handle the #NMSettingPppoe:password property.
313          **/
314         g_object_class_install_property
315                 (object_class, PROP_PASSWORD_FLAGS,
316                  g_param_spec_flags (NM_SETTING_PPPOE_PASSWORD_FLAGS, "", "",
317                                      NM_TYPE_SETTING_SECRET_FLAGS,
318                                      NM_SETTING_SECRET_FLAG_NONE,
319                                      G_PARAM_READWRITE |
320                                      G_PARAM_STATIC_STRINGS));
321 }