device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-adsl.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 2011 - 2013 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-setting-adsl.h"
27 #include "nm-setting-ppp.h"
28 #include "nm-setting-private.h"
29 #include "nm-utils.h"
30 #include "nm-core-enum-types.h"
31
32 /**
33  * SECTION:nm-setting-adsl
34  * @short_description: Describes ADSL-based properties
35  *
36  * The #NMSettingAdsl object is a #NMSetting subclass that describes
37  * properties of ADSL connections.
38  */
39
40 G_DEFINE_TYPE_WITH_CODE (NMSettingAdsl, nm_setting_adsl, NM_TYPE_SETTING,
41                          _nm_register_setting (ADSL, 1))
42 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_ADSL)
43
44 #define NM_SETTING_ADSL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_ADSL, NMSettingAdslPrivate))
45
46 typedef struct {
47         char *  username;
48         char *  password;
49         NMSettingSecretFlags password_flags;
50         char *  protocol;
51         char *  encapsulation;
52         guint32 vpi;
53         guint32 vci;
54 } NMSettingAdslPrivate;
55
56 enum {
57         PROP_0,
58         PROP_USERNAME,
59         PROP_PASSWORD,
60         PROP_PASSWORD_FLAGS,
61         PROP_PROTOCOL,
62         PROP_ENCAPSULATION,
63         PROP_VPI,
64         PROP_VCI,
65
66         LAST_PROP
67 };
68
69 /**
70  * nm_setting_adsl_new:
71  *
72  * Creates a new #NMSettingAdsl object with default values.
73  *
74  * Returns: the new empty #NMSettingAdsl object
75  **/
76 NMSetting *
77 nm_setting_adsl_new (void)
78 {
79         return (NMSetting *) g_object_new (NM_TYPE_SETTING_ADSL, NULL);
80 }
81
82 /**
83  * nm_setting_adsl_get_username:
84  * @setting: the #NMSettingAdsl
85  *
86  * Returns: the #NMSettingAdsl:username property of the setting
87  **/
88 const char *
89 nm_setting_adsl_get_username (NMSettingAdsl *setting)
90 {
91         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), NULL);
92
93         return NM_SETTING_ADSL_GET_PRIVATE (setting)->username;
94 }
95
96 /**
97  * nm_setting_adsl_get_password:
98  * @setting: the #NMSettingAdsl
99  *
100  * Returns: the #NMSettingAdsl:password property of the setting
101  **/
102 const char *
103 nm_setting_adsl_get_password (NMSettingAdsl *setting)
104 {
105         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), NULL);
106
107         return NM_SETTING_ADSL_GET_PRIVATE (setting)->password;
108 }
109
110 /**
111  * nm_setting_adsl_get_password_flags:
112  * @setting: the #NMSettingAdsl
113  *
114  * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingAdsl:password
115  **/
116 NMSettingSecretFlags
117 nm_setting_adsl_get_password_flags (NMSettingAdsl *setting)
118 {
119         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), NM_SETTING_SECRET_FLAG_NONE);
120
121         return NM_SETTING_ADSL_GET_PRIVATE (setting)->password_flags;
122 }
123
124 /**
125  * nm_setting_adsl_get_protocol:
126  * @setting: the #NMSettingAdsl
127  *
128  * Returns: the #NMSettingAdsl:protocol property of the setting
129  **/
130 const char *
131 nm_setting_adsl_get_protocol (NMSettingAdsl *setting)
132 {
133         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), NULL);
134
135         return NM_SETTING_ADSL_GET_PRIVATE (setting)->protocol;
136 }
137
138 /**
139  * nm_setting_adsl_get_encapsulation:
140  * @setting: the #NMSettingAdsl
141  *
142  * Returns: the #NMSettingAdsl:encapsulation property of the setting
143  **/
144 const char *
145 nm_setting_adsl_get_encapsulation (NMSettingAdsl *setting)
146 {
147         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), NULL);
148
149         return NM_SETTING_ADSL_GET_PRIVATE (setting)->encapsulation;
150 }
151
152 /**
153  * nm_setting_adsl_get_vpi:
154  * @setting: the #NMSettingAdsl
155  *
156  * Returns: the #NMSettingAdsl:vpi property of the setting
157  **/
158 guint32
159 nm_setting_adsl_get_vpi (NMSettingAdsl *setting)
160 {
161         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), 0);
162
163         return NM_SETTING_ADSL_GET_PRIVATE (setting)->vpi;
164 }
165
166 /**
167  * nm_setting_adsl_get_vci:
168  * @setting: the #NMSettingAdsl
169  *
170  * Returns: the #NMSettingAdsl:vci property of the setting
171  **/
172 guint32
173 nm_setting_adsl_get_vci (NMSettingAdsl *setting)
174 {
175         g_return_val_if_fail (NM_IS_SETTING_ADSL (setting), 0);
176
177         return NM_SETTING_ADSL_GET_PRIVATE (setting)->vci;
178 }
179
180 static gboolean
181 verify (NMSetting *setting, NMConnection *connection, GError **error)
182 {
183         NMSettingAdslPrivate *priv = NM_SETTING_ADSL_GET_PRIVATE (setting);
184
185         if (!priv->username) {
186                 g_set_error_literal (error,
187                                      NM_CONNECTION_ERROR,
188                                      NM_CONNECTION_ERROR_MISSING_PROPERTY,
189                                      _("property is missing"));
190                 g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_USERNAME);
191                 return FALSE;
192         } else if (!strlen (priv->username)) {
193                 g_set_error_literal (error,
194                                      NM_CONNECTION_ERROR,
195                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
196                                      _("property is empty"));
197                 g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_USERNAME);
198                 return FALSE;
199         }
200
201         if (   !priv->protocol
202             || (   strcmp (priv->protocol, NM_SETTING_ADSL_PROTOCOL_PPPOA)
203                 && strcmp (priv->protocol, NM_SETTING_ADSL_PROTOCOL_PPPOE)
204                 && strcmp (priv->protocol, NM_SETTING_ADSL_PROTOCOL_IPOATM))){
205                 g_set_error (error,
206                              NM_CONNECTION_ERROR,
207                              NM_CONNECTION_ERROR_INVALID_PROPERTY,
208                              _("'%s' is not a valid value for the property"),
209                              priv->protocol ? priv->protocol : "(null)");
210                 g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_PROTOCOL);
211                 return FALSE;
212         }
213
214         if (   priv->encapsulation
215             && (   strcmp (priv->encapsulation, NM_SETTING_ADSL_ENCAPSULATION_VCMUX)
216                 && strcmp (priv->encapsulation, NM_SETTING_ADSL_ENCAPSULATION_LLC) )) {
217                 g_set_error (error,
218                              NM_CONNECTION_ERROR,
219                              NM_CONNECTION_ERROR_INVALID_PROPERTY,
220                              _("'%s' is not a valid value for the property"),
221                              priv->encapsulation);
222                 g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_ENCAPSULATION);
223                 return FALSE;
224         }
225
226         return TRUE;
227 }
228
229 static gboolean
230 verify_secrets (NMSetting *setting, NMConnection *connection, GError **error)
231 {
232         return _nm_setting_verify_secret_string (NM_SETTING_ADSL_GET_PRIVATE (setting)->password,
233                                                  NM_SETTING_ADSL_SETTING_NAME,
234                                                  NM_SETTING_ADSL_PASSWORD,
235                                                  error);
236 }
237
238 static GPtrArray *
239 need_secrets (NMSetting *setting)
240 {
241         NMSettingAdslPrivate *priv = NM_SETTING_ADSL_GET_PRIVATE (setting);
242         GPtrArray *secrets = NULL;
243
244         if (priv->password && *priv->password)
245                 return NULL;
246
247         if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
248                 secrets = g_ptr_array_sized_new (1);
249                 g_ptr_array_add (secrets, NM_SETTING_ADSL_PASSWORD);
250         }
251
252         return secrets;
253 }
254
255 static void
256 nm_setting_adsl_init (NMSettingAdsl *setting)
257 {
258 }
259
260 static void
261 finalize (GObject *object)
262 {
263         NMSettingAdslPrivate *priv = NM_SETTING_ADSL_GET_PRIVATE (object);
264
265         g_free (priv->username);
266         g_free (priv->password);
267         g_free (priv->protocol);
268         g_free (priv->encapsulation);
269
270         G_OBJECT_CLASS (nm_setting_adsl_parent_class)->finalize (object);
271 }
272
273 static void
274 set_property (GObject *object, guint prop_id,
275               const GValue *value, GParamSpec *pspec)
276 {
277         NMSettingAdslPrivate *priv = NM_SETTING_ADSL_GET_PRIVATE (object);
278         const char *str;
279
280         switch (prop_id) {
281         case PROP_USERNAME:
282                 g_free (priv->username);
283                 priv->username = g_value_dup_string (value);
284                 break;
285         case PROP_PASSWORD:
286                 g_free (priv->password);
287                 priv->password = g_value_dup_string (value);
288                 break;
289         case PROP_PASSWORD_FLAGS:
290                 priv->password_flags = g_value_get_flags (value);
291                 break;
292         case PROP_PROTOCOL:
293                 g_free (priv->protocol);
294                 str = g_value_get_string (value);
295                 priv->protocol = str ? g_ascii_strdown (str, -1) : NULL;
296                 break;
297         case PROP_ENCAPSULATION:
298                 g_free (priv->encapsulation);
299                 str = g_value_get_string (value);
300                 priv->encapsulation = str ? g_ascii_strdown (str, -1) : NULL;
301                 break;
302         case PROP_VPI:
303                 priv->vpi = g_value_get_uint (value);
304                 break;
305         case PROP_VCI:
306                 priv->vci = g_value_get_uint (value);
307                 break;
308         default:
309                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310                 break;
311         }
312 }
313
314 static void
315 get_property (GObject *object, guint prop_id,
316               GValue *value, GParamSpec *pspec)
317 {
318         NMSettingAdsl *setting = NM_SETTING_ADSL (object);
319
320         switch (prop_id) {
321         case PROP_USERNAME:
322                 g_value_set_string (value, nm_setting_adsl_get_username (setting));
323                 break;
324         case PROP_PASSWORD:
325                 g_value_set_string (value, nm_setting_adsl_get_password (setting));
326                 break;
327         case PROP_PASSWORD_FLAGS:
328                 g_value_set_flags (value, nm_setting_adsl_get_password_flags (setting));
329                 break;
330         case PROP_PROTOCOL:
331                 g_value_set_string (value, nm_setting_adsl_get_protocol (setting));
332                 break;
333         case PROP_ENCAPSULATION:
334                 g_value_set_string (value, nm_setting_adsl_get_encapsulation (setting));
335                 break;
336         case PROP_VPI:
337                 g_value_set_uint (value, nm_setting_adsl_get_vpi (setting));
338                 break;
339         case PROP_VCI:
340                 g_value_set_uint (value, nm_setting_adsl_get_vci (setting));
341                 break;
342         default:
343                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344                 break;
345         }
346 }
347
348 static void
349 nm_setting_adsl_class_init (NMSettingAdslClass *setting_class)
350 {
351         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
352         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
353
354         g_type_class_add_private (setting_class, sizeof (NMSettingAdslPrivate));
355
356         /* virtual methods */
357         object_class->set_property = set_property;
358         object_class->get_property = get_property;
359         object_class->finalize     = finalize;
360         parent_class->verify       = verify;
361         parent_class->verify_secrets = verify_secrets;
362         parent_class->need_secrets = need_secrets;
363
364         /* Properties */
365
366         /**
367          * NMSettingAdsl:username:
368          *
369          * Username used to authenticate with the ADSL service.
370          **/
371         g_object_class_install_property
372                 (object_class, PROP_USERNAME,
373                  g_param_spec_string (NM_SETTING_ADSL_USERNAME, "", "",
374                                       NULL,
375                                       G_PARAM_READWRITE |
376                                       G_PARAM_STATIC_STRINGS));
377
378         /**
379          * NMSettingAdsl:password:
380          *
381          * Password used to authenticate with the ADSL service.
382          **/
383         g_object_class_install_property
384                 (object_class, PROP_PASSWORD,
385                  g_param_spec_string (NM_SETTING_ADSL_PASSWORD, "", "",
386                                       NULL,
387                                       G_PARAM_READWRITE |
388                                       NM_SETTING_PARAM_SECRET |
389                                       G_PARAM_STATIC_STRINGS));
390
391         /**
392          * NMSettingAdsl:password-flags:
393          *
394          * Flags indicating how to handle the #NMSettingAdsl:password property.
395          **/
396         g_object_class_install_property
397                 (object_class, PROP_PASSWORD_FLAGS,
398                  g_param_spec_flags (NM_SETTING_ADSL_PASSWORD_FLAGS, "", "",
399                                      NM_TYPE_SETTING_SECRET_FLAGS,
400                                      NM_SETTING_SECRET_FLAG_NONE,
401                                      G_PARAM_READWRITE |
402                                      G_PARAM_STATIC_STRINGS));
403
404         /**
405          * NMSettingAdsl:protocol:
406          *
407          * ADSL connection protocol.  Can be "pppoa", "pppoe" or "ipoatm".
408          **/
409         g_object_class_install_property
410                 (object_class, PROP_PROTOCOL,
411                  g_param_spec_string (NM_SETTING_ADSL_PROTOCOL, "", "",
412                                       NULL,
413                                       G_PARAM_READWRITE |
414                                       G_PARAM_STATIC_STRINGS));
415
416         /**
417          * NMSettingAdsl:encapsulation:
418          *
419          * Encapsulation of ADSL connection.  Can be "vcmux" or "llc".
420          **/
421         g_object_class_install_property
422                 (object_class, PROP_ENCAPSULATION,
423                  g_param_spec_string (NM_SETTING_ADSL_ENCAPSULATION, "", "",
424                                       NULL,
425                                       G_PARAM_READWRITE |
426                                       G_PARAM_STATIC_STRINGS));
427
428         /**
429          * NMSettingAdsl:vpi:
430          *
431          * VPI of ADSL connection
432          **/
433         g_object_class_install_property
434                 (object_class, PROP_VPI,
435                  g_param_spec_uint (NM_SETTING_ADSL_VPI, "", "",
436                                     0, 65536, 0,
437                                     G_PARAM_READWRITE |
438                                     G_PARAM_STATIC_STRINGS));
439
440         /**
441          * NMSettingAdsl:vci:
442          *
443          * VCI of ADSL connection
444          **/
445         g_object_class_install_property
446                 (object_class, PROP_VCI,
447                  g_param_spec_uint (NM_SETTING_ADSL_VCI, "", "",
448                                     0, 65536, 0,
449                                     G_PARAM_READWRITE |
450                                     G_PARAM_STATIC_STRINGS));
451 }