nm-setting-vpn.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 <errno.h>
  24. #include <stdlib.h>
  25. #include "nm-utils/nm-secret-utils.h"
  26. #include "nm-setting-vpn.h"
  27. #include "nm-utils.h"
  28. #include "nm-utils-private.h"
  29. #include "nm-setting-private.h"
  30. /**
  31. * SECTION:nm-setting-vpn
  32. * @short_description: Describes connection properties for Virtual Private Networks
  33. *
  34. * The #NMSettingVpn object is a #NMSetting subclass that describes properties
  35. * necessary for connection to Virtual Private Networks. NetworkManager uses
  36. * a plugin architecture to allow easier use of new VPN types, and this
  37. * setting abstracts the configuration for those plugins. Since the configuration
  38. * options are only known to the VPN plugins themselves, the VPN configuration
  39. * options are stored as key/value pairs of strings rather than GObject
  40. * properties.
  41. **/
  42. G_DEFINE_TYPE (NMSettingVpn, nm_setting_vpn, NM_TYPE_SETTING)
  43. #define NM_SETTING_VPN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_VPN, NMSettingVpnPrivate))
  44. typedef struct {
  45. char *service_type;
  46. /* username of the user requesting this connection, thus
  47. * it's really only valid for user connections, and it also
  48. * should never be saved out to persistent config.
  49. */
  50. char *user_name;
  51. /* Whether the VPN stays up across link changes, until the user
  52. * explicitly disconnects it.
  53. */
  54. gboolean persistent;
  55. /* The hash table is created at setting object
  56. * init time and should not be replaced. It is
  57. * a char * -> char * mapping, and both the key
  58. * and value are owned by the hash table, and should
  59. * be allocated with functions whose value can be
  60. * freed with g_free(). Should not contain secrets.
  61. */
  62. GHashTable *data;
  63. /* The hash table is created at setting object
  64. * init time and should not be replaced. It is
  65. * a char * -> char * mapping, and both the key
  66. * and value are owned by the hash table, and should
  67. * be allocated with functions whose value can be
  68. * freed with g_free(). Should contain secrets only.
  69. */
  70. GHashTable *secrets;
  71. /* Timeout for the VPN service to establish the connection */
  72. guint32 timeout;
  73. } NMSettingVpnPrivate;
  74. enum {
  75. PROP_0,
  76. PROP_SERVICE_TYPE,
  77. PROP_USER_NAME,
  78. PROP_PERSISTENT,
  79. PROP_DATA,
  80. PROP_SECRETS,
  81. PROP_TIMEOUT,
  82. LAST_PROP
  83. };
  84. /**
  85. * nm_setting_vpn_new:
  86. *
  87. * Creates a new #NMSettingVpn object with default values.
  88. *
  89. * Returns: (transfer full): the new empty #NMSettingVpn object
  90. **/
  91. NMSetting *
  92. nm_setting_vpn_new (void)
  93. {
  94. return (NMSetting *) g_object_new (NM_TYPE_SETTING_VPN, NULL);
  95. }
  96. /**
  97. * nm_setting_vpn_get_service_type:
  98. * @setting: the #NMSettingVpn
  99. *
  100. * Returns the service name of the VPN, which identifies the specific VPN
  101. * plugin that should be used to connect to this VPN.
  102. *
  103. * Returns: the VPN plugin's service name
  104. **/
  105. const char *
  106. nm_setting_vpn_get_service_type (NMSettingVpn *setting)
  107. {
  108. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  109. return NM_SETTING_VPN_GET_PRIVATE (setting)->service_type;
  110. }
  111. /**
  112. * nm_setting_vpn_get_user_name:
  113. * @setting: the #NMSettingVpn
  114. *
  115. * Returns: the #NMSettingVpn:user-name property of the setting
  116. **/
  117. const char *
  118. nm_setting_vpn_get_user_name (NMSettingVpn *setting)
  119. {
  120. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  121. return NM_SETTING_VPN_GET_PRIVATE (setting)->user_name;
  122. }
  123. /**
  124. * nm_setting_vpn_get_persistent:
  125. * @setting: the #NMSettingVpn
  126. *
  127. * Returns: the #NMSettingVpn:persistent property of the setting
  128. **/
  129. gboolean
  130. nm_setting_vpn_get_persistent (NMSettingVpn *setting)
  131. {
  132. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), FALSE);
  133. return NM_SETTING_VPN_GET_PRIVATE (setting)->persistent;
  134. }
  135. /**
  136. * nm_setting_vpn_get_num_data_items:
  137. * @setting: the #NMSettingVpn
  138. *
  139. * Gets number of key/value pairs of VPN configuration data.
  140. *
  141. * Returns: the number of VPN plugin specific configuration data items
  142. **/
  143. guint32
  144. nm_setting_vpn_get_num_data_items (NMSettingVpn *setting)
  145. {
  146. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), 0);
  147. return g_hash_table_size (NM_SETTING_VPN_GET_PRIVATE (setting)->data);
  148. }
  149. /**
  150. * nm_setting_vpn_add_data_item:
  151. * @setting: the #NMSettingVpn
  152. * @key: a name that uniquely identifies the given value @item
  153. * @item: the value to be referenced by @key
  154. *
  155. * Establishes a relationship between @key and @item internally in the
  156. * setting which may be retrieved later. Should not be used to store passwords
  157. * or other secrets, which is what nm_setting_vpn_add_secret() is for.
  158. **/
  159. void
  160. nm_setting_vpn_add_data_item (NMSettingVpn *setting,
  161. const char *key,
  162. const char *item)
  163. {
  164. g_return_if_fail (NM_IS_SETTING_VPN (setting));
  165. g_return_if_fail (key != NULL);
  166. g_return_if_fail (strlen (key) > 0);
  167. g_return_if_fail (item != NULL);
  168. g_return_if_fail (strlen (item) > 0);
  169. g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
  170. g_strdup (key), g_strdup (item));
  171. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_DATA);
  172. }
  173. /**
  174. * nm_setting_vpn_get_data_item:
  175. * @setting: the #NMSettingVpn
  176. * @key: the name of the data item to retrieve
  177. *
  178. * Retrieves the data item of a key/value relationship previously established
  179. * by nm_setting_vpn_add_data_item().
  180. *
  181. * Returns: the data item, if any
  182. **/
  183. const char *
  184. nm_setting_vpn_get_data_item (NMSettingVpn *setting, const char *key)
  185. {
  186. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  187. return (const char *) g_hash_table_lookup (NM_SETTING_VPN_GET_PRIVATE (setting)->data, key);
  188. }
  189. /**
  190. * nm_setting_vpn_get_data_keys:
  191. * @setting: the #NMSettingVpn
  192. * @out_length: (allow-none): (out): the length of the returned array
  193. *
  194. * Retrieves every data key inside @setting, as an array.
  195. *
  196. * Returns: (array length=out_length) (transfer container): a
  197. * %NULL-terminated array containing each data key or %NULL if
  198. * there are no data items.
  199. *
  200. * Since: 1.12
  201. */
  202. const char **
  203. nm_setting_vpn_get_data_keys (NMSettingVpn *setting,
  204. guint *out_length)
  205. {
  206. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  207. return nm_utils_strdict_get_keys (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
  208. TRUE,
  209. out_length);
  210. }
  211. /**
  212. * nm_setting_vpn_remove_data_item:
  213. * @setting: the #NMSettingVpn
  214. * @key: the name of the data item to remove
  215. *
  216. * Deletes a key/value relationship previously established by
  217. * nm_setting_vpn_add_data_item().
  218. *
  219. * Returns: %TRUE if the data item was found and removed from the internal list,
  220. * %FALSE if it was not.
  221. **/
  222. gboolean
  223. nm_setting_vpn_remove_data_item (NMSettingVpn *setting, const char *key)
  224. {
  225. gboolean found;
  226. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), FALSE);
  227. found = g_hash_table_remove (NM_SETTING_VPN_GET_PRIVATE (setting)->data, key);
  228. if (found)
  229. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_DATA);
  230. return found;
  231. }
  232. static void
  233. foreach_item_helper (NMSettingVpn *self,
  234. gboolean is_secrets,
  235. NMVpnIterFunc func,
  236. gpointer user_data)
  237. {
  238. NMSettingVpnPrivate *priv;
  239. guint len, i;
  240. gs_strfreev char **keys = NULL;
  241. GHashTable *hash;
  242. nm_assert (NM_IS_SETTING_VPN (self));
  243. nm_assert (func);
  244. priv = NM_SETTING_VPN_GET_PRIVATE (self);
  245. if (is_secrets) {
  246. keys = (char **) nm_setting_vpn_get_secret_keys (self, &len);
  247. hash = priv->secrets;
  248. } else {
  249. keys = (char **) nm_setting_vpn_get_data_keys (self, &len);
  250. hash = priv->data;
  251. }
  252. if (!len) {
  253. nm_assert (!keys);
  254. return;
  255. }
  256. for (i = 0; i < len; i++) {
  257. nm_assert (keys[i]);
  258. keys[i] = g_strdup (keys[i]);
  259. }
  260. nm_assert (!keys[i]);
  261. for (i = 0; i < len; i++) {
  262. const char *value;
  263. value = g_hash_table_lookup (hash, keys[i]);
  264. /* NOTE: note that we call the function with a clone of @key,
  265. * not with the actual key from the dictionary.
  266. *
  267. * The @value on the other hand, is actually inside our dictionary,
  268. * it's not a clone. However, it might be %NULL, in case the key was
  269. * deleted while iterating. */
  270. func (keys[i], value, user_data);
  271. }
  272. }
  273. /**
  274. * nm_setting_vpn_foreach_data_item:
  275. * @setting: a #NMSettingVpn
  276. * @func: (scope call): an user provided function
  277. * @user_data: data to be passed to @func
  278. *
  279. * Iterates all data items stored in this setting. It is safe to add, remove,
  280. * and modify data items inside @func, though any additions or removals made
  281. * during iteration will not be part of the iteration.
  282. */
  283. void
  284. nm_setting_vpn_foreach_data_item (NMSettingVpn *setting,
  285. NMVpnIterFunc func,
  286. gpointer user_data)
  287. {
  288. g_return_if_fail (NM_IS_SETTING_VPN (setting));
  289. g_return_if_fail (func);
  290. foreach_item_helper (setting, FALSE, func, user_data);
  291. }
  292. /**
  293. * nm_setting_vpn_get_num_secrets:
  294. * @setting: the #NMSettingVpn
  295. *
  296. * Gets number of VPN plugin specific secrets in the setting.
  297. *
  298. * Returns: the number of VPN plugin specific secrets
  299. **/
  300. guint32
  301. nm_setting_vpn_get_num_secrets (NMSettingVpn *setting)
  302. {
  303. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), 0);
  304. return g_hash_table_size (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets);
  305. }
  306. /**
  307. * nm_setting_vpn_add_secret:
  308. * @setting: the #NMSettingVpn
  309. * @key: a name that uniquely identifies the given secret @secret
  310. * @secret: the secret to be referenced by @key
  311. *
  312. * Establishes a relationship between @key and @secret internally in the
  313. * setting which may be retrieved later.
  314. **/
  315. void
  316. nm_setting_vpn_add_secret (NMSettingVpn *setting,
  317. const char *key,
  318. const char *secret)
  319. {
  320. g_return_if_fail (NM_IS_SETTING_VPN (setting));
  321. g_return_if_fail (key != NULL);
  322. g_return_if_fail (strlen (key) > 0);
  323. g_return_if_fail (secret != NULL);
  324. g_return_if_fail (strlen (secret) > 0);
  325. g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets,
  326. g_strdup (key), g_strdup (secret));
  327. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_SECRETS);
  328. }
  329. /**
  330. * nm_setting_vpn_get_secret:
  331. * @setting: the #NMSettingVpn
  332. * @key: the name of the secret to retrieve
  333. *
  334. * Retrieves the secret of a key/value relationship previously established
  335. * by nm_setting_vpn_add_secret().
  336. *
  337. * Returns: the secret, if any
  338. **/
  339. const char *
  340. nm_setting_vpn_get_secret (NMSettingVpn *setting, const char *key)
  341. {
  342. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  343. return (const char *) g_hash_table_lookup (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets, key);
  344. }
  345. /**
  346. * nm_setting_vpn_get_secret_keys:
  347. * @setting: the #NMSettingVpn
  348. * @out_length: (allow-none): (out): the length of the returned array
  349. *
  350. * Retrieves every secret key inside @setting, as an array.
  351. *
  352. * Returns: (array length=out_length) (transfer container): a
  353. * %NULL-terminated array containing each secret key or %NULL if
  354. * there are no secrets.
  355. *
  356. * Since: 1.12
  357. */
  358. const char **
  359. nm_setting_vpn_get_secret_keys (NMSettingVpn *setting,
  360. guint *out_length)
  361. {
  362. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), NULL);
  363. return nm_utils_strdict_get_keys (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets,
  364. TRUE,
  365. out_length);
  366. }
  367. /**
  368. * nm_setting_vpn_remove_secret:
  369. * @setting: the #NMSettingVpn
  370. * @key: the name of the secret to remove
  371. *
  372. * Deletes a key/value relationship previously established by
  373. * nm_setting_vpn_add_secret().
  374. *
  375. * Returns: %TRUE if the secret was found and removed from the internal list,
  376. * %FALSE if it was not.
  377. **/
  378. gboolean
  379. nm_setting_vpn_remove_secret (NMSettingVpn *setting, const char *key)
  380. {
  381. gboolean found;
  382. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), FALSE);
  383. found = g_hash_table_remove (NM_SETTING_VPN_GET_PRIVATE (setting)->secrets, key);
  384. if (found)
  385. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_SECRETS);
  386. return found;
  387. }
  388. /**
  389. * nm_setting_vpn_foreach_secret:
  390. * @setting: a #NMSettingVpn
  391. * @func: (scope call): an user provided function
  392. * @user_data: data to be passed to @func
  393. *
  394. * Iterates all secrets stored in this setting. It is safe to add, remove,
  395. * and modify secrets inside @func, though any additions or removals made during
  396. * iteration will not be part of the iteration.
  397. */
  398. void
  399. nm_setting_vpn_foreach_secret (NMSettingVpn *setting,
  400. NMVpnIterFunc func,
  401. gpointer user_data)
  402. {
  403. g_return_if_fail (NM_IS_SETTING_VPN (setting));
  404. g_return_if_fail (func);
  405. foreach_item_helper (setting, TRUE, func, user_data);
  406. }
  407. /**
  408. * nm_setting_vpn_get_timeout:
  409. * @setting: the #NMSettingVpn
  410. *
  411. * Returns: the #NMSettingVpn:timeout property of the setting
  412. *
  413. * Since: 1.2
  414. **/
  415. guint32
  416. nm_setting_vpn_get_timeout (NMSettingVpn *setting)
  417. {
  418. g_return_val_if_fail (NM_IS_SETTING_VPN (setting), 0);
  419. return NM_SETTING_VPN_GET_PRIVATE (setting)->timeout;
  420. }
  421. static gboolean
  422. verify (NMSetting *setting, NMConnection *connection, GError **error)
  423. {
  424. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  425. NMSettingConnection *s_con;
  426. if (!priv->service_type) {
  427. g_set_error_literal (error,
  428. NM_CONNECTION_ERROR,
  429. NM_CONNECTION_ERROR_MISSING_PROPERTY,
  430. _("property is missing"));
  431. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, NM_SETTING_VPN_SERVICE_TYPE);
  432. return FALSE;
  433. }
  434. if (!strlen (priv->service_type)) {
  435. g_set_error_literal (error,
  436. NM_CONNECTION_ERROR,
  437. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  438. _("property is empty"));
  439. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, NM_SETTING_VPN_SERVICE_TYPE);
  440. return FALSE;
  441. }
  442. /* default username can be NULL, but can't be zero-length */
  443. if (priv->user_name && !strlen (priv->user_name)) {
  444. g_set_error_literal (error,
  445. NM_CONNECTION_ERROR,
  446. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  447. _("property is empty"));
  448. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, NM_SETTING_VPN_USER_NAME);
  449. return FALSE;
  450. }
  451. if ( connection
  452. && (s_con = nm_connection_get_setting_connection (connection))
  453. && nm_setting_connection_get_multi_connect (s_con) != NM_CONNECTION_MULTI_CONNECT_DEFAULT) {
  454. g_set_error_literal (error,
  455. NM_CONNECTION_ERROR,
  456. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  457. _("cannot set connection.multi-connect for VPN setting"));
  458. return FALSE;
  459. }
  460. return TRUE;
  461. }
  462. static NMSettingUpdateSecretResult
  463. update_secret_string (NMSetting *setting,
  464. const char *key,
  465. const char *value,
  466. GError **error)
  467. {
  468. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  469. g_return_val_if_fail (key != NULL, NM_SETTING_UPDATE_SECRET_ERROR);
  470. g_return_val_if_fail (value != NULL, NM_SETTING_UPDATE_SECRET_ERROR);
  471. if (!value || !strlen (value)) {
  472. g_set_error (error, NM_CONNECTION_ERROR,
  473. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  474. _("secret was empty"));
  475. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, key);
  476. return NM_SETTING_UPDATE_SECRET_ERROR;
  477. }
  478. if (g_strcmp0 (g_hash_table_lookup (priv->secrets, key), value) == 0)
  479. return NM_SETTING_UPDATE_SECRET_SUCCESS_UNCHANGED;
  480. g_hash_table_insert (priv->secrets, g_strdup (key), g_strdup (value));
  481. return NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED;
  482. }
  483. static NMSettingUpdateSecretResult
  484. update_secret_dict (NMSetting *setting,
  485. GVariant *secrets,
  486. GError **error)
  487. {
  488. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  489. GVariantIter iter;
  490. const char *name, *value;
  491. NMSettingUpdateSecretResult result = NM_SETTING_UPDATE_SECRET_SUCCESS_UNCHANGED;
  492. g_return_val_if_fail (secrets != NULL, NM_SETTING_UPDATE_SECRET_ERROR);
  493. /* Make sure the items are valid */
  494. g_variant_iter_init (&iter, secrets);
  495. while (g_variant_iter_next (&iter, "{&s&s}", &name, &value)) {
  496. if (!name || !strlen (name)) {
  497. g_set_error_literal (error, NM_CONNECTION_ERROR,
  498. NM_CONNECTION_ERROR_INVALID_SETTING,
  499. _("setting contained a secret with an empty name"));
  500. g_prefix_error (error, "%s: ", NM_SETTING_VPN_SETTING_NAME);
  501. return NM_SETTING_UPDATE_SECRET_ERROR;
  502. }
  503. if (!value || !strlen (value)) {
  504. g_set_error (error, NM_CONNECTION_ERROR,
  505. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  506. _("secret value was empty"));
  507. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, name);
  508. return NM_SETTING_UPDATE_SECRET_ERROR;
  509. }
  510. }
  511. /* Now add the items to the settings' secrets list */
  512. g_variant_iter_init (&iter, secrets);
  513. while (g_variant_iter_next (&iter, "{&s&s}", &name, &value)) {
  514. if (value == NULL) {
  515. g_warn_if_fail (value != NULL);
  516. continue;
  517. }
  518. if (strlen (value) == 0) {
  519. g_warn_if_fail (strlen (value) > 0);
  520. continue;
  521. }
  522. if (g_strcmp0 (g_hash_table_lookup (priv->secrets, name), value) == 0)
  523. continue;
  524. g_hash_table_insert (priv->secrets, g_strdup (name), g_strdup (value));
  525. result = NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED;
  526. }
  527. return result;
  528. }
  529. static int
  530. update_one_secret (NMSetting *setting, const char *key, GVariant *value, GError **error)
  531. {
  532. NMSettingUpdateSecretResult success = NM_SETTING_UPDATE_SECRET_ERROR;
  533. g_return_val_if_fail (key != NULL, NM_SETTING_UPDATE_SECRET_ERROR);
  534. g_return_val_if_fail (value != NULL, NM_SETTING_UPDATE_SECRET_ERROR);
  535. if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) {
  536. /* Passing the string properties individually isn't correct, and won't
  537. * produce the correct result, but for some reason that's how it used
  538. * to be done. So even though it's not correct, keep the code around
  539. * for compatibility's sake.
  540. */
  541. success = update_secret_string (setting, key, g_variant_get_string (value, NULL), error);
  542. } else if (g_variant_is_of_type (value, G_VARIANT_TYPE ("a{ss}"))) {
  543. if (strcmp (key, NM_SETTING_VPN_SECRETS) != 0) {
  544. g_set_error_literal (error, NM_CONNECTION_ERROR,
  545. NM_CONNECTION_ERROR_PROPERTY_NOT_SECRET,
  546. _("not a secret property"));
  547. g_prefix_error (error, "%s.%s ", NM_SETTING_VPN_SETTING_NAME, key);
  548. } else
  549. success = update_secret_dict (setting, value, error);
  550. } else {
  551. g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY,
  552. _("secret is not of correct type"));
  553. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, key);
  554. }
  555. if (success == NM_SETTING_UPDATE_SECRET_SUCCESS_MODIFIED)
  556. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_SECRETS);
  557. return success;
  558. }
  559. static gboolean
  560. get_secret_flags (NMSetting *setting,
  561. const char *secret_name,
  562. gboolean verify_secret,
  563. NMSettingSecretFlags *out_flags,
  564. GError **error)
  565. {
  566. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  567. gs_free char *flags_key = NULL;
  568. gpointer val;
  569. unsigned long tmp;
  570. NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
  571. flags_key = g_strdup_printf ("%s-flags", secret_name);
  572. if (g_hash_table_lookup_extended (priv->data, flags_key, NULL, &val)) {
  573. errno = 0;
  574. tmp = strtoul ((const char *) val, NULL, 10);
  575. if ((errno != 0) || (tmp > NM_SETTING_SECRET_FLAGS_ALL)) {
  576. g_set_error (error,
  577. NM_CONNECTION_ERROR,
  578. NM_CONNECTION_ERROR_INVALID_PROPERTY,
  579. _("failed to convert value '%s' to uint"),
  580. (const char *) val);
  581. g_prefix_error (error, "%s.%s: ", NM_SETTING_VPN_SETTING_NAME, flags_key);
  582. return FALSE;
  583. }
  584. flags = (NMSettingSecretFlags) tmp;
  585. }
  586. if (out_flags)
  587. *out_flags = flags;
  588. return TRUE;
  589. }
  590. static gboolean
  591. set_secret_flags (NMSetting *setting,
  592. const char *secret_name,
  593. gboolean verify_secret,
  594. NMSettingSecretFlags flags,
  595. GError **error)
  596. {
  597. g_hash_table_insert (NM_SETTING_VPN_GET_PRIVATE (setting)->data,
  598. g_strdup_printf ("%s-flags", secret_name),
  599. g_strdup_printf ("%u", flags));
  600. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_SECRETS);
  601. return TRUE;
  602. }
  603. static GPtrArray *
  604. need_secrets (NMSetting *setting)
  605. {
  606. /* Assume that VPN connections need secrets since they almost always will */
  607. return g_ptr_array_sized_new (1);
  608. }
  609. static gboolean
  610. _compare_secrets (NMSettingVpn *a,
  611. NMSettingVpn *b,
  612. NMSettingCompareFlags flags)
  613. {
  614. GHashTable *a_secrets;
  615. GHashTableIter iter;
  616. const char *key, *val;
  617. a_secrets = NM_SETTING_VPN_GET_PRIVATE (a)->secrets;
  618. g_hash_table_iter_init (&iter, a_secrets);
  619. while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &val)) {
  620. NMSettingSecretFlags a_secret_flags = NM_SETTING_SECRET_FLAG_NONE;
  621. NMSettingSecretFlags b_secret_flags = NM_SETTING_SECRET_FLAG_NONE;
  622. nm_setting_get_secret_flags (NM_SETTING (a), key, &a_secret_flags, NULL);
  623. nm_setting_get_secret_flags (NM_SETTING (b), key, &b_secret_flags, NULL);
  624. /* If the secret flags aren't the same, the settings aren't the same */
  625. if (a_secret_flags != b_secret_flags)
  626. return FALSE;
  627. if ( (flags & NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS)
  628. && (a_secret_flags & NM_SETTING_SECRET_FLAG_AGENT_OWNED))
  629. continue;
  630. if ( (flags & NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)
  631. && (a_secret_flags & NM_SETTING_SECRET_FLAG_NOT_SAVED))
  632. continue;
  633. /* Now compare the values themselves */
  634. if (g_strcmp0 (val, nm_setting_vpn_get_secret (b, key)) != 0)
  635. return FALSE;
  636. }
  637. return TRUE;
  638. }
  639. static gboolean
  640. compare_one_secret (NMSettingVpn *a,
  641. NMSettingVpn *b,
  642. NMSettingCompareFlags flags)
  643. {
  644. if (!_compare_secrets (a, b, flags))
  645. return FALSE;
  646. if (!_compare_secrets (b, a, flags))
  647. return FALSE;
  648. return TRUE;
  649. }
  650. static gboolean
  651. compare_property (NMSetting *setting,
  652. NMSetting *other,
  653. const GParamSpec *prop_spec,
  654. NMSettingCompareFlags flags)
  655. {
  656. gboolean same;
  657. /* We only need to treat the 'secrets' property specially */
  658. if (g_strcmp0 (prop_spec->name, NM_SETTING_VPN_SECRETS) != 0)
  659. return NM_SETTING_CLASS (nm_setting_vpn_parent_class)->compare_property (setting, other, prop_spec, flags);
  660. /* Compare A to B to ensure everything in A is found in B */
  661. same = compare_one_secret (NM_SETTING_VPN (setting), NM_SETTING_VPN (other), flags);
  662. if (same) {
  663. /* And then B to A to ensure everything in B is also found in A */
  664. same = compare_one_secret (NM_SETTING_VPN (other), NM_SETTING_VPN (setting), flags);
  665. }
  666. return same;
  667. }
  668. static gboolean
  669. clear_secrets_with_flags (NMSetting *setting,
  670. GParamSpec *pspec,
  671. NMSettingClearSecretsWithFlagsFn func,
  672. gpointer user_data)
  673. {
  674. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  675. GHashTableIter iter;
  676. const char *secret;
  677. gboolean changed = TRUE;
  678. if (priv->secrets == NULL)
  679. return FALSE;
  680. /* Iterate through secrets hash and check each entry */
  681. g_hash_table_iter_init (&iter, priv->secrets);
  682. while (g_hash_table_iter_next (&iter, (gpointer) &secret, NULL)) {
  683. NMSettingSecretFlags flags = NM_SETTING_SECRET_FLAG_NONE;
  684. nm_setting_get_secret_flags (setting, secret, &flags, NULL);
  685. if (func (setting, pspec->name, flags, user_data) == TRUE) {
  686. g_hash_table_iter_remove (&iter);
  687. changed = TRUE;
  688. }
  689. }
  690. if (changed)
  691. g_object_notify (G_OBJECT (setting), NM_SETTING_VPN_SECRETS);
  692. return changed;
  693. }
  694. static void
  695. nm_setting_vpn_init (NMSettingVpn *setting)
  696. {
  697. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  698. priv->data = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, g_free);
  699. priv->secrets = g_hash_table_new_full (nm_str_hash, g_str_equal, g_free, (GDestroyNotify) nm_free_secret);
  700. }
  701. static void
  702. finalize (GObject *object)
  703. {
  704. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object);
  705. g_free (priv->service_type);
  706. g_free (priv->user_name);
  707. g_hash_table_destroy (priv->data);
  708. g_hash_table_destroy (priv->secrets);
  709. G_OBJECT_CLASS (nm_setting_vpn_parent_class)->finalize (object);
  710. }
  711. static void
  712. set_property (GObject *object, guint prop_id,
  713. const GValue *value, GParamSpec *pspec)
  714. {
  715. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object);
  716. switch (prop_id) {
  717. case PROP_SERVICE_TYPE:
  718. g_free (priv->service_type);
  719. priv->service_type = g_value_dup_string (value);
  720. break;
  721. case PROP_USER_NAME:
  722. g_free (priv->user_name);
  723. priv->user_name = g_value_dup_string (value);
  724. break;
  725. case PROP_PERSISTENT:
  726. priv->persistent = g_value_get_boolean (value);
  727. break;
  728. case PROP_DATA:
  729. g_hash_table_unref (priv->data);
  730. priv->data = _nm_utils_copy_strdict (g_value_get_boxed (value));
  731. break;
  732. case PROP_SECRETS:
  733. g_hash_table_unref (priv->secrets);
  734. priv->secrets = _nm_utils_copy_strdict (g_value_get_boxed (value));
  735. break;
  736. case PROP_TIMEOUT:
  737. priv->timeout = g_value_get_uint (value);
  738. break;
  739. default:
  740. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  741. break;
  742. }
  743. }
  744. static void
  745. get_property (GObject *object, guint prop_id,
  746. GValue *value, GParamSpec *pspec)
  747. {
  748. NMSettingVpn *setting = NM_SETTING_VPN (object);
  749. NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (setting);
  750. switch (prop_id) {
  751. case PROP_SERVICE_TYPE:
  752. g_value_set_string (value, nm_setting_vpn_get_service_type (setting));
  753. break;
  754. case PROP_USER_NAME:
  755. g_value_set_string (value, nm_setting_vpn_get_user_name (setting));
  756. break;
  757. case PROP_PERSISTENT:
  758. g_value_set_boolean (value, priv->persistent);
  759. break;
  760. case PROP_DATA:
  761. g_value_take_boxed (value, _nm_utils_copy_strdict (priv->data));
  762. break;
  763. case PROP_SECRETS:
  764. g_value_take_boxed (value, _nm_utils_copy_strdict (priv->secrets));
  765. break;
  766. case PROP_TIMEOUT:
  767. g_value_set_uint (value, nm_setting_vpn_get_timeout (setting));
  768. break;
  769. default:
  770. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  771. break;
  772. }
  773. }
  774. static void
  775. nm_setting_vpn_class_init (NMSettingVpnClass *klass)
  776. {
  777. GObjectClass *object_class = G_OBJECT_CLASS (klass);
  778. NMSettingClass *setting_class = NM_SETTING_CLASS (klass);
  779. GArray *properties_override = _nm_sett_info_property_override_create_array ();
  780. g_type_class_add_private (klass, sizeof (NMSettingVpnPrivate));
  781. object_class->set_property = set_property;
  782. object_class->get_property = get_property;
  783. object_class->finalize = finalize;
  784. setting_class->verify = verify;
  785. setting_class->update_one_secret = update_one_secret;
  786. setting_class->get_secret_flags = get_secret_flags;
  787. setting_class->set_secret_flags = set_secret_flags;
  788. setting_class->need_secrets = need_secrets;
  789. setting_class->compare_property = compare_property;
  790. setting_class->clear_secrets_with_flags = clear_secrets_with_flags;
  791. /**
  792. * NMSettingVpn:service-type:
  793. *
  794. * D-Bus service name of the VPN plugin that this setting uses to connect to
  795. * its network. i.e. org.freedesktop.NetworkManager.vpnc for the vpnc
  796. * plugin.
  797. **/
  798. g_object_class_install_property
  799. (object_class, PROP_SERVICE_TYPE,
  800. g_param_spec_string (NM_SETTING_VPN_SERVICE_TYPE, "", "",
  801. NULL,
  802. G_PARAM_READWRITE |
  803. G_PARAM_STATIC_STRINGS));
  804. /**
  805. * NMSettingVpn:user-name:
  806. *
  807. * If the VPN connection requires a user name for authentication, that name
  808. * should be provided here. If the connection is available to more than one
  809. * user, and the VPN requires each user to supply a different name, then
  810. * leave this property empty. If this property is empty, NetworkManager
  811. * will automatically supply the username of the user which requested the
  812. * VPN connection.
  813. **/
  814. g_object_class_install_property
  815. (object_class, PROP_USER_NAME,
  816. g_param_spec_string (NM_SETTING_VPN_USER_NAME, "", "",
  817. NULL,
  818. G_PARAM_READWRITE |
  819. G_PARAM_STATIC_STRINGS));
  820. /**
  821. * NMSettingVpn:persistent:
  822. *
  823. * If the VPN service supports persistence, and this property is %TRUE,
  824. * the VPN will attempt to stay connected across link changes and outages,
  825. * until explicitly disconnected.
  826. **/
  827. g_object_class_install_property
  828. (object_class, PROP_PERSISTENT,
  829. g_param_spec_boolean (NM_SETTING_VPN_PERSISTENT, "", "",
  830. FALSE,
  831. G_PARAM_READWRITE |
  832. G_PARAM_STATIC_STRINGS));
  833. /**
  834. * NMSettingVpn:data: (type GHashTable(utf8,utf8)):
  835. *
  836. * Dictionary of key/value pairs of VPN plugin specific data. Both keys and
  837. * values must be strings.
  838. **/
  839. /* ---keyfile---
  840. * property: data
  841. * variable: separate variables named after keys of the dictionary
  842. * description: The keys of the data dictionary are used as variable names directly
  843. * under [vpn] section.
  844. * example: remote=ovpn.corp.com cipher=AES-256-CBC username=joe
  845. * ---end---
  846. */
  847. g_object_class_install_property
  848. (object_class, PROP_DATA,
  849. g_param_spec_boxed (NM_SETTING_VPN_DATA, "", "",
  850. G_TYPE_HASH_TABLE,
  851. G_PARAM_READWRITE |
  852. G_PARAM_STATIC_STRINGS));
  853. _properties_override_add_transform (properties_override,
  854. g_object_class_find_property (G_OBJECT_CLASS (setting_class),
  855. NM_SETTING_VPN_DATA),
  856. G_VARIANT_TYPE ("a{ss}"),
  857. _nm_utils_strdict_to_dbus,
  858. _nm_utils_strdict_from_dbus);
  859. /**
  860. * NMSettingVpn:secrets: (type GHashTable(utf8,utf8)):
  861. *
  862. * Dictionary of key/value pairs of VPN plugin specific secrets like
  863. * passwords or private keys. Both keys and values must be strings.
  864. **/
  865. /* ---keyfile---
  866. * property: secrets
  867. * variable: separate variables named after keys of the dictionary
  868. * description: The keys of the secrets dictionary are used as variable names directly
  869. * under [vpn-secrets] section.
  870. * example: password=Popocatepetl
  871. * ---end---
  872. */
  873. g_object_class_install_property
  874. (object_class, PROP_SECRETS,
  875. g_param_spec_boxed (NM_SETTING_VPN_SECRETS, "", "",
  876. G_TYPE_HASH_TABLE,
  877. G_PARAM_READWRITE |
  878. NM_SETTING_PARAM_SECRET |
  879. G_PARAM_STATIC_STRINGS));
  880. _properties_override_add_transform (properties_override,
  881. g_object_class_find_property (G_OBJECT_CLASS (setting_class),
  882. NM_SETTING_VPN_SECRETS),
  883. G_VARIANT_TYPE ("a{ss}"),
  884. _nm_utils_strdict_to_dbus,
  885. _nm_utils_strdict_from_dbus);
  886. /**
  887. * NMSettingVpn:timeout:
  888. *
  889. * Timeout for the VPN service to establish the connection. Some services
  890. * may take quite a long time to connect.
  891. * Value of 0 means a default timeout, which is 60 seconds (unless overridden
  892. * by vpn.timeout in configuration file). Values greater than zero mean
  893. * timeout in seconds.
  894. *
  895. * Since: 1.2
  896. **/
  897. g_object_class_install_property
  898. (object_class, PROP_TIMEOUT,
  899. g_param_spec_uint (NM_SETTING_VPN_TIMEOUT, "", "",
  900. 0, G_MAXUINT32, 0,
  901. G_PARAM_READWRITE |
  902. G_PARAM_STATIC_STRINGS));
  903. _nm_setting_class_commit_full (setting_class, NM_META_SETTING_TYPE_VPN,
  904. NULL, properties_override);
  905. }