device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-core / nm-setting-cdma.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  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25
26 #include "nm-setting-cdma.h"
27 #include "nm-utils.h"
28 #include "nm-setting-private.h"
29 #include "nm-core-enum-types.h"
30
31 /**
32  * SECTION:nm-setting-cdma
33  * @short_description: Describes CDMA-based mobile broadband properties
34  *
35  * The #NMSettingCdma object is a #NMSetting subclass that describes
36  * properties that allow connections to IS-95-based mobile broadband
37  * networks, including those using CDMA2000/EVDO technology.
38  */
39
40 G_DEFINE_TYPE_WITH_CODE (NMSettingCdma, nm_setting_cdma, NM_TYPE_SETTING,
41                          _nm_register_setting (CDMA, 1))
42 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_CDMA)
43
44 #define NM_SETTING_CDMA_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_CDMA, NMSettingCdmaPrivate))
45
46 typedef struct {
47         char *number; /* For dialing, duh */
48         char *username;
49         char *password;
50         NMSettingSecretFlags password_flags;
51 } NMSettingCdmaPrivate;
52
53 enum {
54         PROP_0,
55         PROP_NUMBER,
56         PROP_USERNAME,
57         PROP_PASSWORD,
58         PROP_PASSWORD_FLAGS,
59
60         LAST_PROP
61 };
62
63 /**
64  * nm_setting_cdma_new:
65  *
66  * Creates a new #NMSettingCdma object with default values.
67  *
68  * Returns: the new empty #NMSettingCdma object
69  **/
70 NMSetting *
71 nm_setting_cdma_new (void)
72 {
73         return (NMSetting *) g_object_new (NM_TYPE_SETTING_CDMA, NULL);
74 }
75
76 /**
77  * nm_setting_cdma_get_number:
78  * @setting: the #NMSettingCdma
79  *
80  * Returns: the #NMSettingCdma:number property of the setting
81  **/
82 const char *
83 nm_setting_cdma_get_number (NMSettingCdma *setting)
84 {
85         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
86
87         return NM_SETTING_CDMA_GET_PRIVATE (setting)->number;
88 }
89
90 /**
91  * nm_setting_cdma_get_username:
92  * @setting: the #NMSettingCdma
93  *
94  * Returns: the #NMSettingCdma:username property of the setting
95  **/
96 const char *
97 nm_setting_cdma_get_username (NMSettingCdma *setting)
98 {
99         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
100
101         return NM_SETTING_CDMA_GET_PRIVATE (setting)->username;
102 }
103
104 /**
105  * nm_setting_cdma_get_password:
106  * @setting: the #NMSettingCdma
107  *
108  * Returns: the #NMSettingCdma:password property of the setting
109  **/
110 const char *
111 nm_setting_cdma_get_password (NMSettingCdma *setting)
112 {
113         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NULL);
114
115         return NM_SETTING_CDMA_GET_PRIVATE (setting)->password;
116 }
117
118 /**
119  * nm_setting_cdma_get_password_flags:
120  * @setting: the #NMSettingCdma
121  *
122  * Returns: the #NMSettingSecretFlags pertaining to the #NMSettingCdma:password
123  **/
124 NMSettingSecretFlags
125 nm_setting_cdma_get_password_flags (NMSettingCdma *setting)
126 {
127         g_return_val_if_fail (NM_IS_SETTING_CDMA (setting), NM_SETTING_SECRET_FLAG_NONE);
128
129         return NM_SETTING_CDMA_GET_PRIVATE (setting)->password_flags;
130 }
131
132 static gboolean
133 verify (NMSetting *setting, NMConnection *connection, GError **error)
134 {
135         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (setting);
136
137         if (!priv->number) {
138                 g_set_error_literal (error,
139                                      NM_CONNECTION_ERROR,
140                                      NM_CONNECTION_ERROR_MISSING_PROPERTY,
141                                      _("property is missing"));
142                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_NUMBER);
143                 return FALSE;
144         } else if (!strlen (priv->number)) {
145                 g_set_error_literal (error,
146                                      NM_CONNECTION_ERROR,
147                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
148                                      _("property is empty"));
149                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_NUMBER);
150                 return FALSE;
151         }
152
153         if (priv->username && !strlen (priv->username)) {
154                 g_set_error_literal (error,
155                                      NM_CONNECTION_ERROR,
156                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
157                                      _("property is empty"));
158                 g_prefix_error (error, "%s.%s: ", NM_SETTING_CDMA_SETTING_NAME, NM_SETTING_CDMA_USERNAME);
159                 return FALSE;
160         }
161
162         return TRUE;
163 }
164
165 static gboolean
166 verify_secrets (NMSetting *setting, NMConnection *connection, GError **error)
167 {
168         return _nm_setting_verify_secret_string (NM_SETTING_CDMA_GET_PRIVATE (setting)->password,
169                                                  NM_SETTING_CDMA_SETTING_NAME,
170                                                  NM_SETTING_CDMA_PASSWORD,
171                                                  error);
172 }
173
174 static GPtrArray *
175 need_secrets (NMSetting *setting)
176 {
177         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (setting);
178         GPtrArray *secrets = NULL;
179
180         if (priv->password && *priv->password)
181                 return NULL;
182
183         if (priv->username) {
184                 if (!(priv->password_flags & NM_SETTING_SECRET_FLAG_NOT_REQUIRED)) {
185                         secrets = g_ptr_array_sized_new (1);
186                         g_ptr_array_add (secrets, NM_SETTING_CDMA_PASSWORD);
187                 }
188         }
189
190         return secrets;
191 }
192
193 static void
194 nm_setting_cdma_init (NMSettingCdma *setting)
195 {
196 }
197
198 static void
199 finalize (GObject *object)
200 {
201         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (object);
202
203         g_free (priv->number);
204         g_free (priv->username);
205         g_free (priv->password);
206
207         G_OBJECT_CLASS (nm_setting_cdma_parent_class)->finalize (object);
208 }
209
210 static void
211 set_property (GObject *object, guint prop_id,
212               const GValue *value, GParamSpec *pspec)
213 {
214         NMSettingCdmaPrivate *priv = NM_SETTING_CDMA_GET_PRIVATE (object);
215
216         switch (prop_id) {
217         case PROP_NUMBER:
218                 g_free (priv->number);
219                 priv->number = g_value_dup_string (value);
220                 break;
221         case PROP_USERNAME:
222                 g_free (priv->username);
223                 priv->username = g_value_dup_string (value);
224                 break;
225         case PROP_PASSWORD:
226                 g_free (priv->password);
227                 priv->password = g_value_dup_string (value);
228                 break;
229         case PROP_PASSWORD_FLAGS:
230                 priv->password_flags = g_value_get_flags (value);
231                 break;
232         default:
233                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234                 break;
235         }
236 }
237
238 static void
239 get_property (GObject *object, guint prop_id,
240               GValue *value, GParamSpec *pspec)
241 {
242         NMSettingCdma *setting = NM_SETTING_CDMA (object);
243
244         switch (prop_id) {
245         case PROP_NUMBER:
246                 g_value_set_string (value, nm_setting_cdma_get_number (setting));
247                 break;
248         case PROP_USERNAME:
249                 g_value_set_string (value, nm_setting_cdma_get_username (setting));
250                 break;
251         case PROP_PASSWORD:
252                 g_value_set_string (value, nm_setting_cdma_get_password (setting));
253                 break;
254         case PROP_PASSWORD_FLAGS:
255                 g_value_set_flags (value, nm_setting_cdma_get_password_flags (setting));
256                 break;
257         default:
258                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259                 break;
260         }
261 }
262
263 static void
264 nm_setting_cdma_class_init (NMSettingCdmaClass *setting_class)
265 {
266         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
267         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
268
269         g_type_class_add_private (setting_class, sizeof (NMSettingCdmaPrivate));
270
271         /* virtual methods */
272         object_class->set_property = set_property;
273         object_class->get_property = get_property;
274         object_class->finalize     = finalize;
275         parent_class->verify       = verify;
276         parent_class->verify_secrets = verify_secrets;
277         parent_class->need_secrets = need_secrets;
278
279         /* Properties */
280
281         /**
282          * NMSettingCdma:number:
283          *
284          * The number to dial to establish the connection to the CDMA-based mobile
285          * broadband network, if any.  If not specified, the default number (#777)
286          * is used when required.
287          **/
288         g_object_class_install_property
289                 (object_class, PROP_NUMBER,
290                  g_param_spec_string (NM_SETTING_CDMA_NUMBER, "", "",
291                                       NULL,
292                                       G_PARAM_READWRITE |
293                                       G_PARAM_STATIC_STRINGS));
294
295         /**
296          * NMSettingCdma:username:
297          *
298          * The username used to authenticate with the network, if required.  Many
299          * providers do not require a username, or accept any username.  But if a
300          * username is required, it is specified here.
301          **/
302         g_object_class_install_property
303                 (object_class, PROP_USERNAME,
304                  g_param_spec_string (NM_SETTING_CDMA_USERNAME, "", "",
305                                       NULL,
306                                       G_PARAM_READWRITE |
307                                       G_PARAM_STATIC_STRINGS));
308
309         /**
310          * NMSettingCdma:password:
311          *
312          * The password used to authenticate with the network, if required.  Many
313          * providers do not require a password, or accept any password.  But if a
314          * password is required, it is specified here.
315          **/
316         g_object_class_install_property
317                 (object_class, PROP_PASSWORD,
318                  g_param_spec_string (NM_SETTING_CDMA_PASSWORD, "", "",
319                                       NULL,
320                                       G_PARAM_READWRITE |
321                                       NM_SETTING_PARAM_SECRET |
322                                       G_PARAM_STATIC_STRINGS));
323
324         /**
325          * NMSettingCdma:password-flags:
326          *
327          * Flags indicating how to handle the #NMSettingCdma:password property.
328          **/
329         g_object_class_install_property
330                 (object_class, PROP_PASSWORD_FLAGS,
331                  g_param_spec_flags (NM_SETTING_CDMA_PASSWORD_FLAGS, "", "",
332                                      NM_TYPE_SETTING_SECRET_FLAGS,
333                                      NM_SETTING_SECRET_FLAG_NONE,
334                                      G_PARAM_READWRITE |
335                                      G_PARAM_STATIC_STRINGS));
336 }