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