device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / 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
31 /**
32  * SECTION:nm-setting-pppoe
33  * @short_description: Describes PPPoE connection properties
34  * @include: nm-setting-pppoe.h
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 /**
42  * nm_setting_pppoe_error_quark:
43  *
44  * Registers an error quark for #NMSettingPPPOE if necessary.
45  *
46  * Returns: the error quark used for #NMSettingPPPOE errors.
47  **/
48 GQuark
49 nm_setting_pppoe_error_quark (void)
50 {
51         static GQuark quark;
52
53         if (G_UNLIKELY (!quark))
54                 quark = g_quark_from_static_string ("nm-setting-pppoe-error-quark");
55         return quark;
56 }
57
58
59 G_DEFINE_TYPE_WITH_CODE (NMSettingPPPOE, nm_setting_pppoe, NM_TYPE_SETTING,
60                          _nm_register_setting (NM_SETTING_PPPOE_SETTING_NAME,
61                                                g_define_type_id,
62                                                3,
63                                                NM_SETTING_PPPOE_ERROR))
64 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_PPPOE)
65
66 #define NM_SETTING_PPPOE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_PPPOE, NMSettingPPPOEPrivate))
67
68 typedef struct {
69         char *service;
70         char *username;
71         char *password;
72         NMSettingSecretFlags password_flags;
73 } NMSettingPPPOEPrivate;
74
75 enum {
76         PROP_0,
77         PROP_SERVICE,
78         PROP_USERNAME,
79         PROP_PASSWORD,
80         PROP_PASSWORD_FLAGS,
81
82         LAST_PROP
83 };
84
85 /**
86  * nm_setting_pppoe_new:
87  *
88  * Creates a new #NMSettingPPPOE object with default values.
89  *
90  * Returns: (transfer full): the new empty #NMSettingPPPOE object
91  **/
92 NMSetting *
93 nm_setting_pppoe_new (void)
94 {
95         return (NMSetting *) g_object_new (NM_TYPE_SETTING_PPPOE, NULL);
96 }
97
98 /**
99  * nm_setting_pppoe_get_service:
100  * @setting: the #NMSettingPPPOE
101  *
102  * Returns: the #NMSettingPPPOE:service property of the setting
103  **/
104 const char *
105 nm_setting_pppoe_get_service  (NMSettingPPPOE *setting)
106 {
107         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
108
109         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->service;
110 }
111
112 /**
113  * nm_setting_pppoe_get_username:
114  * @setting: the #NMSettingPPPOE
115  *
116  * Returns: the #NMSettingPPPOE:username property of the setting
117  **/
118 const char *
119 nm_setting_pppoe_get_username (NMSettingPPPOE *setting)
120 {
121         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
122
123         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->username;
124 }
125
126 /**
127  * nm_setting_pppoe_get_password:
128  * @setting: the #NMSettingPPPOE
129  *
130  * Returns: the #NMSettingPPPOE:password property of the setting
131  **/
132 const char *
133 nm_setting_pppoe_get_password (NMSettingPPPOE *setting)
134 {
135         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NULL);
136
137         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->password;
138 }
139
140 /**
141  * nm_setting_pppoe_get_password_flags:
142  * @setting: the #NMSettingPPPOE
143  *
144  * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingPPPOE:password
145  **/
146 NMSettingSecretFlags
147 nm_setting_pppoe_get_password_flags (NMSettingPPPOE *setting)
148 {
149         g_return_val_if_fail (NM_IS_SETTING_PPPOE (setting), NM_SETTING_SECRET_FLAG_NONE);
150
151         return NM_SETTING_PPPOE_GET_PRIVATE (setting)->password_flags;
152 }
153
154 static gboolean
155 verify (NMSetting *setting, GSList *all_settings, GError **error)
156 {
157         NMSettingPPPOEPrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (setting);
158
159         if (!priv->username) {
160                 g_set_error_literal (error,
161                                      NM_SETTING_PPPOE_ERROR,
162                                      NM_SETTING_PPPOE_ERROR_MISSING_PROPERTY,
163                                      _("property is missing"));
164                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_USERNAME);
165                 return FALSE;
166         } else if (!strlen (priv->username)) {
167                 g_set_error_literal (error,
168                                      NM_SETTING_PPPOE_ERROR,
169                                      NM_SETTING_PPPOE_ERROR_INVALID_PROPERTY,
170                                      _("property is empty"));
171                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_USERNAME);
172                 return FALSE;
173         }
174
175         if (priv->service && !strlen (priv->service)) {
176                 g_set_error_literal (error,
177                                      NM_SETTING_PPPOE_ERROR,
178                                      NM_SETTING_PPPOE_ERROR_INVALID_PROPERTY,
179                                      _("property is empty"));
180                 g_prefix_error (error, "%s.%s: ", NM_SETTING_PPPOE_SETTING_NAME, NM_SETTING_PPPOE_SERVICE);
181                 return FALSE;
182         }
183
184         return TRUE;
185 }
186
187 static GPtrArray *
188 need_secrets (NMSetting *setting)
189 {
190         NMSettingPPPOEPrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (setting);
191         GPtrArray *secrets = NULL;
192
193         if (priv->password)
194                 return NULL;
195
196         if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
197                 secrets = g_ptr_array_sized_new (1);
198                 g_ptr_array_add (secrets, NM_SETTING_PPPOE_PASSWORD);
199         }
200
201         return secrets;
202 }
203
204 static void
205 nm_setting_pppoe_init (NMSettingPPPOE *setting)
206 {
207 }
208
209 static void
210 set_property (GObject *object, guint prop_id,
211               const GValue *value, GParamSpec *pspec)
212 {
213         NMSettingPPPOEPrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (object);
214
215         switch (prop_id) {
216         case PROP_SERVICE:
217                 g_free (priv->service);
218                 priv->service = g_value_dup_string (value);
219                 break;
220         case PROP_USERNAME:
221                 g_free (priv->username);
222                 priv->username = g_value_dup_string (value);
223                 break;
224         case PROP_PASSWORD:
225                 g_free (priv->password);
226                 priv->password = g_value_dup_string (value);
227                 break;
228         case PROP_PASSWORD_FLAGS:
229                 priv->password_flags = g_value_get_uint (value);
230                 break;
231         default:
232                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233                 break;
234         }
235 }
236
237 static void
238 get_property (GObject *object, guint prop_id,
239               GValue *value, GParamSpec *pspec)
240 {
241         NMSettingPPPOE *setting = NM_SETTING_PPPOE (object);
242
243         switch (prop_id) {
244         case PROP_SERVICE:
245                 g_value_set_string (value, nm_setting_pppoe_get_service (setting));
246                 break;
247         case PROP_USERNAME:
248                 g_value_set_string (value, nm_setting_pppoe_get_username (setting));
249                 break;
250         case PROP_PASSWORD:
251                 g_value_set_string (value, nm_setting_pppoe_get_password (setting));
252                 break;
253         case PROP_PASSWORD_FLAGS:
254                 g_value_set_uint (value, nm_setting_pppoe_get_password_flags (setting));
255                 break;
256         default:
257                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
258                 break;
259         }
260 }
261
262 static void
263 finalize (GObject *object)
264 {
265         NMSettingPPPOEPrivate *priv = NM_SETTING_PPPOE_GET_PRIVATE (object);
266
267         g_free (priv->username);
268         g_free (priv->password);
269         g_free (priv->service);
270
271         G_OBJECT_CLASS (nm_setting_pppoe_parent_class)->finalize (object);
272 }
273
274 static void
275 nm_setting_pppoe_class_init (NMSettingPPPOEClass *setting_class)
276 {
277         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
278         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
279
280         g_type_class_add_private (setting_class, sizeof (NMSettingPPPOEPrivate));
281
282         /* virtual methods */
283         object_class->set_property = set_property;
284         object_class->get_property = get_property;
285         object_class->finalize     = finalize;
286         parent_class->verify       = verify;
287         parent_class->need_secrets = need_secrets;
288
289         /* Properties */
290         /**
291          * NMSettingPPPOE:service:
292          *
293          * If specified, instruct PPPoE to only initiate sessions with access
294          * concentrators that provide the specified service.  For most providers,
295          * this should be left blank.  It is only required if there are multiple
296          * access concentrators or a specific service is known to be required.
297          **/
298         g_object_class_install_property
299                 (object_class, PROP_SERVICE,
300                  g_param_spec_string (NM_SETTING_PPPOE_SERVICE, "", "",
301                                       NULL,
302                                       G_PARAM_READWRITE |
303                                       G_PARAM_STATIC_STRINGS));
304
305         /**
306          * NMSettingPPPOE:username:
307          *
308          * Username used to authenticate with the PPPoE service.
309          **/
310         g_object_class_install_property
311                 (object_class, PROP_USERNAME,
312                  g_param_spec_string (NM_SETTING_PPPOE_USERNAME, "", "",
313                                       NULL,
314                                       G_PARAM_READWRITE |
315                                       G_PARAM_STATIC_STRINGS));
316
317         /**
318          * NMSettingPPPOE:password:
319          *
320          * Password used to authenticate with the PPPoE service.
321          **/
322         g_object_class_install_property
323                 (object_class, PROP_PASSWORD,
324                  g_param_spec_string (NM_SETTING_PPPOE_PASSWORD, "", "",
325                                       NULL,
326                                       G_PARAM_READWRITE |
327                                       NM_SETTING_PARAM_SECRET |
328                                       G_PARAM_STATIC_STRINGS));
329
330         /**
331          * NMSettingPPPOE:password-flags:
332          *
333          * Flags indicating how to handle the #NMSettingPPPOE:password property.
334          **/
335         g_object_class_install_property
336                 (object_class, PROP_PASSWORD_FLAGS,
337                  g_param_spec_uint (NM_SETTING_PPPOE_PASSWORD_FLAGS, "", "",
338                                     NM_SETTING_SECRET_FLAG_NONE,
339                                     NM_SETTING_SECRET_FLAGS_ALL,
340                                     NM_SETTING_SECRET_FLAG_NONE,
341                                     G_PARAM_READWRITE |
342                                     G_PARAM_STATIC_STRINGS));
343 }