summaryrefslogtreecommitdiff
path: root/src/core/settings/nm-settings-connection.h
blob: 47599c3d6eeef711da6d22554f0fae806067a912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2008 Novell, Inc.
 * Copyright (C) 2008 - 2013 Red Hat, Inc.
 */

#ifndef __NETWORKMANAGER_SETTINGS_CONNECTION_H__
#define __NETWORKMANAGER_SETTINGS_CONNECTION_H__

#include "nm-dbus-object.h"
#include "nm-connection.h"

#include "nm-settings-storage.h"

/*****************************************************************************/

typedef enum {

    NM_SETTINGS_CONNECTION_ADD_REASON_NONE = 0,

    NM_SETTINGS_CONNECTION_ADD_REASON_BLOCK_AUTOCONNECT = (1u << 0),

} NMSettingsConnectionAddReason;

typedef enum {

    NM_SETTINGS_CONNECTION_UPDATE_REASON_NONE = 0,

    /* with persist-mode != NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY_ONLY, and
     * update tries to update the profile on disk (which can always fail).
     * In some cases we want to ignore such failure and proceed. For example,
     * when we receive secrets from a secret-agent, we want to update the connection
     * at all cost and ignore failures to write them to disk. */
    NM_SETTINGS_CONNECTION_UPDATE_REASON_IGNORE_PERSIST_FAILURE = (1u << 0),

    /* When updating the profile, force renaming the file on disk. That matters
     * only for keyfile plugin. Keyfile prefers a filename based on connection.id.
     * When the connection.id changes we might want to rename the file on disk
     * (that is, don't overwrite the existing file, but delete it and write it
     * with the new name).
     * This flag forces such rename. */
    NM_SETTINGS_CONNECTION_UPDATE_REASON_FORCE_RENAME = (1u << 1),

    /* Usually, changing a profile that is currently active does not immediately
     * reapply the changes. The exception are connection.zone and connection.metered
     * properties. When this flag is set, then these two properties are reapplied
     * right away.
     *
     * See also %NM_SETTINGS_UPDATE2_FLAG_NO_REAPPLY flag, to prevent partial reapply
     * during Update2(). */
    NM_SETTINGS_CONNECTION_UPDATE_REASON_REAPPLY_PARTIAL = (1u << 2),

    NM_SETTINGS_CONNECTION_UPDATE_REASON_CLEAR_SYSTEM_SECRETS = (1u << 3),
    NM_SETTINGS_CONNECTION_UPDATE_REASON_RESET_SYSTEM_SECRETS = (1u << 4),

    NM_SETTINGS_CONNECTION_UPDATE_REASON_CLEAR_AGENT_SECRETS = (1u << 5),
    NM_SETTINGS_CONNECTION_UPDATE_REASON_RESET_AGENT_SECRETS = (1u << 6),

    /* if a profile was generated as default-wired connection for a device, then
     * when the user modifies it via D-Bus, the profile should become persisted
     * to disk and the initial purpose the profile was created should be forgotten. */
    NM_SETTINGS_CONNECTION_UPDATE_REASON_CLEAR_DEFAULT_WIRED = (1u << 7),

    NM_SETTINGS_CONNECTION_UPDATE_REASON_BLOCK_AUTOCONNECT = (1u << 8),

    /* Is anything other than secrets changing */
    NM_SETTINGS_CONNECTION_UPDATE_REASON_UPDATE_NON_SECRET = (1u << 9),

} NMSettingsConnectionUpdateReason;

typedef enum {

    /* if the profile is in-memory, update it in-memory and keep it.
     * if the profile is on-disk, update it on-disk, and keep it. */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_KEEP,

    /* persist to disk. If the profile is currently in-memory, remove
     * it from /run. Depending on the shadowed-storage, the pre-existing
     * file is reused when moving the storage.
     *
     * Corresponds to %NM_SETTINGS_UPDATE2_FLAG_TO_DISK. */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_TO_DISK,

    /* Update in-memory (i.e. persist to /run). If the profile is currently on disk,
     * then a reference to the profile is remembered as "shadowed-storage".
     * Later, when storing again to persistent storage, the shadowed-storage is
     * updated. When deleting the profile, the shadowed-storage is also deleted
     * from disk.
     *
     * Corresponds to %NM_SETTINGS_UPDATE2_FLAG_IN_MEMORY. */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY,

    /* Update in-memory (i.e. persist to /run). This is almost like
     * %NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY, except the in-memory profile
     * remembers not to own the shadowed-storage ("shadowed-owned").
     * The difference is that when deleting the in-memory profile, the original
     * profile is not deleted but instead the nmmeta tombstone remembers the
     * shadowed-storage and re-used it when re-adding the profile.
     *
     * Corresponds to %NM_SETTINGS_UPDATE2_FLAG_IN_MEMORY_DETACHED. */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY_DETACHED,

    /* Update in-memory (i.e. persist to /run). If the profile is currently on disk,
     * delete it from disk.
     *
     * If the profile is in-memory and has a shadowed-storage, the original profile
     * will be deleted from disk.
     *
     * Corresponds to %NM_SETTINGS_UPDATE2_FLAG_IN_MEMORY_ONLY. */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_IN_MEMORY_ONLY,

    /* This only updates the connection in-memory. Note that "in-memory" above
     * means to write to keyfile in /run. This mode really means to not notify the
     * settings plugin about the change. This should be only used for updating
     * secrets.
     */
    NM_SETTINGS_CONNECTION_PERSIST_MODE_NO_PERSIST,

} NMSettingsConnectionPersistMode;

/*****************************************************************************/

#define NM_TYPE_SETTINGS_CONNECTION (nm_settings_connection_get_type())
#define NM_SETTINGS_CONNECTION(obj) \
    (_NM_G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_SETTINGS_CONNECTION, NMSettingsConnection))
#define NM_SETTINGS_CONNECTION_CLASS(klass) \
    (G_TYPE_CHECK_CLASS_CAST((klass), NM_TYPE_SETTINGS_CONNECTION, NMSettingsConnectionClass))
#define NM_IS_SETTINGS_CONNECTION(obj) \
    (G_TYPE_CHECK_INSTANCE_TYPE((obj), NM_TYPE_SETTINGS_CONNECTION))
#define NM_IS_SETTINGS_CONNECTION_CLASS(klass) \
    (G_TYPE_CHECK_CLASS_TYPE((klass), NM_TYPE_SETTINGS_CONNECTION))
#define NM_SETTINGS_CONNECTION_GET_CLASS(obj) \
    (G_TYPE_INSTANCE_GET_CLASS((obj), NM_TYPE_SETTINGS_CONNECTION, NMSettingsConnectionClass))

#define NM_SETTINGS_CONNECTION_GET_SECRETS      "get-secrets"
#define NM_SETTINGS_CONNECTION_CANCEL_SECRETS   "cancel-secrets"
#define NM_SETTINGS_CONNECTION_UPDATED_INTERNAL "updated-internal"
#define NM_SETTINGS_CONNECTION_FLAGS_CHANGED    "flags-changed"

/* Properties */
#define NM_SETTINGS_CONNECTION_UNSAVED  "unsaved"
#define NM_SETTINGS_CONNECTION_FLAGS    "flags"
#define NM_SETTINGS_CONNECTION_FILENAME "filename"

/**
 * NMSettingsConnectionIntFlags:
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_NONE: no flag set
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_UNSAVED: the connection is not saved to disk.
 *  See also #NM_SETTINGS_CONNECTION_FLAG_UNSAVED.
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED: A connection is "nm-generated" if
 *  it was generated by NetworkManger. If the connection gets modified or saved
 *  by the user, the flag gets cleared. A nm-generated is implicitly unsaved.
 *  See also #NM_SETTINGS_CONNECTION_FLAG_NM_GENERATED.
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_VOLATILE: The connection will be deleted
 *  when it disconnects. That is for in-memory connections (unsaved), which are
 *  currently active but cleanup on disconnect.
 *  See also #NM_SETTINGS_CONNECTION_FLAG_VOLATILE.
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_EXTERNAL: the profile was generated to
 *  represent the external activation of a device. See also #NM_SETTINGS_CONNECTION_FLAG_EXTERNAL.
 * @NM_SETTINGS_CONNECTION_INT_FLAGS_VISIBLE: The connection is visible
 * @_NM_SETTINGS_CONNECTION_INT_FLAGS_EXPORTED_MASK: the entire enum is
 *   internal, however, parts of it is public API as #NMSettingsConnectionFlags.
 *   This mask, are the public flags.
 * @_NM_SETTINGS_CONNECTION_INT_FLAGS_ALL: special mask, for all known flags
 *
 * #NMSettingsConnection flags.
 **/
typedef enum _NMSettingsConnectionIntFlags {
    NM_SETTINGS_CONNECTION_INT_FLAGS_NONE = 0,

    NM_SETTINGS_CONNECTION_INT_FLAGS_UNSAVED      = NM_SETTINGS_CONNECTION_FLAG_UNSAVED,
    NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED = NM_SETTINGS_CONNECTION_FLAG_NM_GENERATED,
    NM_SETTINGS_CONNECTION_INT_FLAGS_VOLATILE     = NM_SETTINGS_CONNECTION_FLAG_VOLATILE,
    NM_SETTINGS_CONNECTION_INT_FLAGS_EXTERNAL     = NM_SETTINGS_CONNECTION_FLAG_EXTERNAL,

    NM_SETTINGS_CONNECTION_INT_FLAGS_VISIBLE = 0x10,

    _NM_SETTINGS_CONNECTION_INT_FLAGS_LAST,

    _NM_SETTINGS_CONNECTION_INT_FLAGS_EXPORTED_MASK =
        0 | NM_SETTINGS_CONNECTION_INT_FLAGS_UNSAVED | NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED
        | NM_SETTINGS_CONNECTION_INT_FLAGS_VOLATILE | NM_SETTINGS_CONNECTION_INT_FLAGS_EXTERNAL | 0,

    _NM_SETTINGS_CONNECTION_INT_FLAGS_PERSISTENT_MASK =
        0 | NM_SETTINGS_CONNECTION_INT_FLAGS_NM_GENERATED
        | NM_SETTINGS_CONNECTION_INT_FLAGS_VOLATILE | NM_SETTINGS_CONNECTION_INT_FLAGS_EXTERNAL | 0,

    _NM_SETTINGS_CONNECTION_INT_FLAGS_ALL = ((_NM_SETTINGS_CONNECTION_INT_FLAGS_LAST - 1) << 1) - 1,
} NMSettingsConnectionIntFlags;

typedef enum {
    NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_NONE = 0,

    NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_USER_REQUEST = (1LL << 0),
    NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_FAILED       = (1LL << 1),
    NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_NO_SECRETS   = (1LL << 2),

    NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_ALL =
        (NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_USER_REQUEST
         | NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_FAILED
         | NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_NO_SECRETS),
} NMSettingsAutoconnectBlockedReason;

typedef struct _NMSettingsConnectionCallId NMSettingsConnectionCallId;

typedef struct _NMSettingsConnectionClass NMSettingsConnectionClass;

struct _NMSettingsConnectionPrivate;

struct _NMSettingsConnection {
    NMDBusObject                         parent;
    CList                                _connections_lst;
    struct _NMSettingsConnectionPrivate *_priv;
};

GType nm_settings_connection_get_type(void);

NMSettingsConnection *nm_settings_connection_new(void);

NMConnection *nm_settings_connection_get_connection(NMSettingsConnection *self);

void _nm_settings_connection_set_connection(NMSettingsConnection            *self,
                                            NMConnection                    *new_connection,
                                            NMConnection                   **out_old_connection,
                                            NMSettingsConnectionUpdateReason update_reason);

NMSettingsStorage *nm_settings_connection_get_storage(NMSettingsConnection *self);

void _nm_settings_connection_set_storage(NMSettingsConnection *self, NMSettingsStorage *storage);

gboolean nm_settings_connection_still_valid(NMSettingsConnection *self);

const char *nm_settings_connection_get_filename(NMSettingsConnection *self);

guint64 nm_settings_connection_get_last_secret_agent_version_id(NMSettingsConnection *self);

gboolean
nm_settings_connection_has_unmodified_applied_connection(NMSettingsConnection *self,
                                                         NMConnection         *applied_connection,
                                                         NMSettingCompareFlags compare_flage);

gboolean nm_settings_connection_update(NMSettingsConnection            *self,
                                       const char                      *plugin_name,
                                       NMConnection                    *new_connection,
                                       NMSettingsConnectionPersistMode  persist_mode,
                                       NMSettingsConnectionIntFlags     sett_flags,
                                       NMSettingsConnectionIntFlags     sett_mask,
                                       NMSettingsConnectionUpdateReason update_reason,
                                       const char                      *log_context_name,
                                       GError                         **error);

void nm_settings_connection_delete(NMSettingsConnection *self,
                                   gboolean              allow_add_to_no_auto_default);

typedef void (*NMSettingsConnectionSecretsFunc)(NMSettingsConnection       *self,
                                                NMSettingsConnectionCallId *call_id,
                                                const char                 *agent_username,
                                                const char                 *setting_name,
                                                GError                     *error,
                                                gpointer                    user_data);

gboolean nm_settings_connection_new_secrets(NMSettingsConnection *self,
                                            NMConnection         *applied_connection,
                                            const char           *setting_name,
                                            GVariant             *secrets,
                                            GError              **error);

NMSettingsConnectionCallId *
nm_settings_connection_get_secrets(NMSettingsConnection           *self,
                                   NMConnection                   *applied_connection,
                                   NMAuthSubject                  *subject,
                                   const char                     *setting_name,
                                   NMSecretAgentGetSecretsFlags    flags,
                                   const char *const              *hints,
                                   NMSettingsConnectionSecretsFunc callback,
                                   gpointer                        callback_data);

void nm_settings_connection_cancel_secrets(NMSettingsConnection       *self,
                                           NMSettingsConnectionCallId *call_id);

gboolean nm_settings_connection_check_visibility(NMSettingsConnection *self,
                                                 NMSessionMonitor     *session_monitor);

gboolean nm_settings_connection_check_permission(NMSettingsConnection *self,
                                                 const char           *permission);

/*****************************************************************************/

NMDevice *nm_settings_connection_default_wired_get_device(NMSettingsConnection *self);
void nm_settings_connection_default_wired_set_device(NMSettingsConnection *self, NMDevice *device);

/*****************************************************************************/

NMSettingsConnectionIntFlags nm_settings_connection_get_flags(NMSettingsConnection *self);

static inline gboolean
nm_settings_connection_get_unsaved(NMSettingsConnection *self)
{
    return NM_FLAGS_HAS(nm_settings_connection_get_flags(self),
                        NM_SETTINGS_CONNECTION_INT_FLAGS_UNSAVED);
}

NMSettingsConnectionIntFlags
nm_settings_connection_set_flags_full(NMSettingsConnection        *self,
                                      NMSettingsConnectionIntFlags mask,
                                      NMSettingsConnectionIntFlags value);

static inline NMSettingsConnectionIntFlags
nm_settings_connection_set_flags(NMSettingsConnection        *self,
                                 NMSettingsConnectionIntFlags flags,
                                 gboolean                     set)
{
    return nm_settings_connection_set_flags_full(self,
                                                 flags,
                                                 set ? flags
                                                     : NM_SETTINGS_CONNECTION_INT_FLAGS_NONE);
}

/*****************************************************************************/

int nm_settings_connection_cmp_timestamp(NMSettingsConnection *ac, NMSettingsConnection *ab);
int nm_settings_connection_cmp_timestamp_p_with_data(gconstpointer pa,
                                                     gconstpointer pb,
                                                     gpointer      user_data);
int nm_settings_connection_cmp_autoconnect_priority(NMSettingsConnection *a,
                                                    NMSettingsConnection *b);
int nm_settings_connection_cmp_autoconnect_priority_with_data(gconstpointer pa,
                                                              gconstpointer pb,
                                                              gpointer      user_data);
int nm_settings_connection_cmp_autoconnect_priority_p_with_data(gconstpointer pa,
                                                                gconstpointer pb,
                                                                gpointer      user_data);

struct _NMKeyFileDB;

void _nm_settings_connection_register_kf_dbs(NMSettingsConnection *self,
                                             struct _NMKeyFileDB  *kf_db_timestamps,
                                             struct _NMKeyFileDB  *kf_db_seen_bssids);

gboolean nm_settings_connection_get_timestamp(NMSettingsConnection *self, guint64 *out_timestamp);

void nm_settings_connection_update_timestamp(NMSettingsConnection *self, guint64 timestamp);

gboolean nm_settings_connection_has_seen_bssid(NMSettingsConnection *self, const char *bssid);

void nm_settings_connection_add_seen_bssid(NMSettingsConnection *self, const char *seen_bssid);

int  nm_settings_connection_autoconnect_retries_get(NMSettingsConnection *self);
void nm_settings_connection_autoconnect_retries_set(NMSettingsConnection *self, int retries);
void nm_settings_connection_autoconnect_retries_reset(NMSettingsConnection *self);

gint32 nm_settings_connection_autoconnect_retries_blocked_until(NMSettingsConnection *self);

NMSettingsAutoconnectBlockedReason
         nm_settings_connection_autoconnect_blocked_reason_get(NMSettingsConnection *self);
gboolean nm_settings_connection_autoconnect_blocked_reason_set_full(
    NMSettingsConnection              *self,
    NMSettingsAutoconnectBlockedReason mask,
    NMSettingsAutoconnectBlockedReason value);

static inline gboolean
nm_settings_connection_autoconnect_blocked_reason_set(NMSettingsConnection              *self,
                                                      NMSettingsAutoconnectBlockedReason mask,
                                                      gboolean                           set)
{
    return nm_settings_connection_autoconnect_blocked_reason_set_full(
        self,
        mask,
        set ? mask : NM_SETTINGS_AUTO_CONNECT_BLOCKED_REASON_NONE);
}

gboolean nm_settings_connection_autoconnect_is_blocked(NMSettingsConnection *self);

const char *nm_settings_connection_get_id(NMSettingsConnection *connection);
const char *nm_settings_connection_get_uuid(NMSettingsConnection *connection);
const char *nm_settings_connection_get_connection_type(NMSettingsConnection *connection);

/*****************************************************************************/

NMConnection **
nm_settings_connections_array_to_connections(NMSettingsConnection *const *connections,
                                             gssize                       n_connections);

/*****************************************************************************/

void _nm_settings_connection_emit_dbus_signal_updated(NMSettingsConnection *self);
void _nm_settings_connection_emit_dbus_signal_removed(NMSettingsConnection *self);

void _nm_settings_connection_emit_signal_updated_internal(
    NMSettingsConnection            *self,
    NMSettingsConnectionUpdateReason update_reason);

void _nm_settings_connection_cleanup_after_remove(NMSettingsConnection *self);

#endif /* __NETWORKMANAGER_SETTINGS_CONNECTION_H__ */