nm-setting-pppoe.c 11 KB

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