summaryrefslogtreecommitdiff
path: root/src/settings/plugins/keyfile/nms-keyfile-writer.c
blob: 1e3b0b767327d8f84345a284c674836b9d178565 (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2008 Novell, Inc.
 * Copyright (C) 2008 - 2015 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nms-keyfile-writer.h"

#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include "nm-keyfile/nm-keyfile-internal.h"

#include "nms-keyfile-utils.h"
#include "nms-keyfile-reader.h"

#include "nm-glib-aux/nm-io-utils.h"

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

typedef struct {
    const char *keyfile_dir;
} WriteInfo;

static void
cert_writer(NMConnection *                    connection,
            GKeyFile *                        file,
            NMSetting8021x *                  setting,
            const NMSetting8021xSchemeVtable *vtable,
            WriteInfo *                       info,
            GError **                         error)
{
    const char *           setting_name = nm_setting_get_name(NM_SETTING(setting));
    NMSetting8021xCKScheme scheme;
    NMSetting8021xCKFormat format;
    const char *           path = NULL, *ext = "pem";

    scheme = vtable->scheme_func(setting);
    if (scheme == NM_SETTING_802_1X_CK_SCHEME_PATH) {
        char *      tmp           = NULL;
        const char *accepted_path = NULL;

        path = vtable->path_func(setting);
        g_assert(path);

        if (g_str_has_prefix(path, info->keyfile_dir)) {
            const char *p = path + strlen(info->keyfile_dir);

            /* If the path is rooted in the keyfile directory, just use a
             * relative path instead of an absolute one.
             */
            if (*p == '/') {
                while (*p == '/')
                    p++;
                if (p[0]) {
                    /* If @p looks like an integer list, the following detection will fail too and
                     * we will file:// qualify the path below. We thus avoid writing a path string
                     * that would be interpreted as legacy binary format by reader. */
                    tmp = nm_keyfile_detect_unqualified_path_scheme(info->keyfile_dir,
                                                                    p,
                                                                    -1,
                                                                    FALSE,
                                                                    NULL);
                    if (tmp) {
                        nm_clear_g_free(&tmp);
                        accepted_path = p;
                    }
                }
            }
        }
        if (!accepted_path) {
            /* What we are about to write, must also be understood by the reader.
             * Otherwise, add a file:// prefix */
            tmp =
                nm_keyfile_detect_unqualified_path_scheme(info->keyfile_dir, path, -1, FALSE, NULL);
            if (tmp) {
                nm_clear_g_free(&tmp);
                accepted_path = path;
            }
        }

        if (!accepted_path)
            accepted_path = tmp = g_strconcat(NM_KEYFILE_CERT_SCHEME_PREFIX_PATH, path, NULL);
        nm_keyfile_plugin_kf_set_string(file, setting_name, vtable->setting_key, accepted_path);
        g_free(tmp);
    } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_PKCS11) {
        nm_keyfile_plugin_kf_set_string(file,
                                        setting_name,
                                        vtable->setting_key,
                                        vtable->uri_func(setting));
    } else if (scheme == NM_SETTING_802_1X_CK_SCHEME_BLOB) {
        GBytes *      blob;
        const guint8 *blob_data;
        gsize         blob_len;
        gboolean      success;
        GError *      local = NULL;
        char *        new_path;

        blob = vtable->blob_func(setting);
        g_assert(blob);
        blob_data = g_bytes_get_data(blob, &blob_len);

        if (vtable->format_func) {
            /* Get the extension for a private key */
            format = vtable->format_func(setting);
            if (format == NM_SETTING_802_1X_CK_FORMAT_PKCS12)
                ext = "p12";
        } else {
            /* DER or PEM format certificate? */
            if (blob_len > 2 && blob_data[0] == 0x30 && blob_data[1] == 0x82)
                ext = "der";
        }

        /* Write the raw data out to the standard file so that we can use paths
         * from now on instead of pushing around the certificate data.
         */
        new_path = g_strdup_printf("%s/%s-%s.%s",
                                   info->keyfile_dir,
                                   nm_connection_get_uuid(connection),
                                   vtable->file_suffix,
                                   ext);

        /* FIXME(keyfile-parse-in-memory): writer must not access/write to the file system before
         * being sure that the entire profile can be written and all circumstances are good to
         * proceed. That means, while writing we must only collect the blogs in-memory, and write
         * them all in the end together (or not at all). */
        success = nm_utils_file_set_contents(new_path,
                                             (const char *) blob_data,
                                             blob_len,
                                             0600,
                                             NULL,
                                             &local);
        if (success) {
            /* Write the path value to the keyfile.
             * We know, that basename(new_path) starts with a UUID, hence no conflict with "data:;base64,"  */
            nm_keyfile_plugin_kf_set_string(file,
                                            setting_name,
                                            vtable->setting_key,
                                            strrchr(new_path, '/') + 1);
        } else {
            nm_log_warn(LOGD_SETTINGS,
                        "keyfile: %s.%s: failed to write certificate to file %s: %s",
                        setting_name,
                        vtable->setting_key,
                        new_path,
                        local->message);
            g_error_free(local);
        }
        g_free(new_path);
    } else {
        /* scheme_func() returns UNKNOWN in all other cases. The only valid case
         * where a scheme is allowed to be UNKNOWN, is unsetting the value. In this
         * case, we don't expect the writer to be called, because the default value
         * will not be serialized.
         * The only other reason for the scheme to be UNKNOWN is an invalid cert.
         * But our connection verifies, so that cannot happen either. */
        g_return_if_reached();
    }
}

static gboolean
_handler_write(NMConnection *        connection,
               GKeyFile *            keyfile,
               NMKeyfileHandlerType  type,
               NMKeyfileHandlerData *type_data,
               void *                user_data)
{
    if (type == NM_KEYFILE_HANDLER_TYPE_WRITE_CERT) {
        cert_writer(connection,
                    keyfile,
                    NM_SETTING_802_1X(type_data->cur_setting),
                    type_data->write_cert.vtable,
                    user_data,
                    type_data->p_error);
        return TRUE;
    }
    return FALSE;
}

static gboolean
_internal_write_connection(NMConnection *                  connection,
                           gboolean                        is_nm_generated,
                           gboolean                        is_volatile,
                           gboolean                        is_external,
                           const char *                    shadowed_storage,
                           gboolean                        shadowed_owned,
                           const char *                    keyfile_dir,
                           const char *                    profile_dir,
                           gboolean                        with_extension,
                           uid_t                           owner_uid,
                           pid_t                           owner_grp,
                           const char *                    existing_path,
                           gboolean                        existing_path_read_only,
                           gboolean                        force_rename,
                           NMSKeyfileWriterAllowFilenameCb allow_filename_cb,
                           gpointer                        allow_filename_user_data,
                           char **                         out_path,
                           NMConnection **                 out_reread,
                           gboolean *                      out_reread_same,
                           GError **                       error)
{
    nm_auto_unref_keyfile GKeyFile *kf_file        = NULL;
    gs_free char *                  kf_content_buf = NULL;
    gsize                           kf_content_len;
    gs_free char *                  path = NULL;
    const char *                    id;
    WriteInfo                       info = {0};
    gs_free_error GError *local_err      = NULL;
    int                   errsv;
    gboolean              rename;
    int                   i_path;
    gs_unref_object NMConnection *reread      = NULL;
    gboolean                      reread_same = FALSE;

    g_return_val_if_fail(!out_path || !*out_path, FALSE);
    g_return_val_if_fail(keyfile_dir && keyfile_dir[0] == '/', FALSE);

    nm_assert(_nm_connection_verify(connection, NULL) == NM_SETTING_VERIFY_SUCCESS);

    nm_assert(!shadowed_owned || shadowed_storage);

    rename = force_rename || existing_path_read_only
             || (existing_path && !nm_utils_file_is_in_path(existing_path, keyfile_dir));

    id = nm_connection_get_id(connection);
    nm_assert(id && *id);

    info.keyfile_dir = keyfile_dir;

    kf_file =
        nm_keyfile_write(connection, NM_KEYFILE_HANDLER_FLAGS_NONE, _handler_write, &info, error);
    if (!kf_file)
        return FALSE;

    if (is_nm_generated) {
        g_key_file_set_boolean(kf_file,
                               NM_KEYFILE_GROUP_NMMETA,
                               NM_KEYFILE_KEY_NMMETA_NM_GENERATED,
                               TRUE);
    }

    if (is_volatile) {
        g_key_file_set_boolean(kf_file,
                               NM_KEYFILE_GROUP_NMMETA,
                               NM_KEYFILE_KEY_NMMETA_VOLATILE,
                               TRUE);
    }

    if (is_external) {
        g_key_file_set_boolean(kf_file,
                               NM_KEYFILE_GROUP_NMMETA,
                               NM_KEYFILE_KEY_NMMETA_EXTERNAL,
                               TRUE);
    }

    if (shadowed_storage) {
        g_key_file_set_string(kf_file,
                              NM_KEYFILE_GROUP_NMMETA,
                              NM_KEYFILE_KEY_NMMETA_SHADOWED_STORAGE,
                              shadowed_storage);
    }

    if (shadowed_owned) {
        g_key_file_set_boolean(kf_file,
                               NM_KEYFILE_GROUP_NMMETA,
                               NM_KEYFILE_KEY_NMMETA_SHADOWED_OWNED,
                               TRUE);
    }

    kf_content_buf = g_key_file_to_data(kf_file, &kf_content_len, error);
    if (!kf_content_buf)
        return FALSE;

    if (!g_file_test(keyfile_dir, G_FILE_TEST_IS_DIR))
        (void) g_mkdir_with_parents(keyfile_dir, 0755);

    for (i_path = -2; i_path < 10000; i_path++) {
        gs_free char *path_candidate = NULL;
        gboolean      is_existing_path;

        if (i_path == -2) {
            if (!existing_path || rename)
                continue;
            path_candidate = g_strdup(existing_path);
        } else if (i_path == -1) {
            gs_free char *filename_escaped = NULL;

            filename_escaped = nm_keyfile_utils_create_filename(id, with_extension);
            path_candidate   = g_build_filename(keyfile_dir, filename_escaped, NULL);
        } else {
            gs_free char *filename_escaped = NULL;
            gs_free char *filename         = NULL;

            if (i_path == 0)
                filename = g_strdup_printf("%s-%s", id, nm_connection_get_uuid(connection));
            else
                filename =
                    g_strdup_printf("%s-%s-%d", id, nm_connection_get_uuid(connection), i_path);

            filename_escaped = nm_keyfile_utils_create_filename(filename, with_extension);

            path_candidate = g_strdup_printf("%s/%s", keyfile_dir, filename_escaped);
        }

        is_existing_path = existing_path && nm_streq(existing_path, path_candidate);

        if (is_existing_path && rename)
            continue;

        if (allow_filename_cb && !allow_filename_cb(path_candidate, allow_filename_user_data))
            continue;

        if (!is_existing_path) {
            if (g_file_test(path_candidate, G_FILE_TEST_EXISTS))
                continue;
        }

        path = g_steal_pointer(&path_candidate);
        break;
    }

    if (!path) {
        /* this really should not happen, we tried hard to find an unused name... bail out. */
        g_set_error(error,
                    NM_SETTINGS_ERROR,
                    NM_SETTINGS_ERROR_FAILED,
                    "could not find suitable keyfile file name (%s already used)",
                    path);
        return FALSE;
    }

    if (out_reread || out_reread_same) {
        gs_free_error GError *reread_error = NULL;

        reread =
            nms_keyfile_reader_from_keyfile(kf_file, path, NULL, profile_dir, FALSE, &reread_error);

        if (!reread || !nm_connection_normalize(reread, NULL, NULL, &reread_error)) {
            nm_log_err(
                LOGD_SETTINGS,
                "BUG: the profile cannot be stored in keyfile format without becoming unusable: %s",
                reread_error->message);
            g_set_error(error,
                        NM_SETTINGS_ERROR,
                        NM_SETTINGS_ERROR_FAILED,
                        "keyfile writer produces an invalid connection: %s",
                        reread_error->message);
            nm_assert_not_reached();
            return FALSE;
        }

        if (out_reread_same) {
            reread_same =
                !!nm_connection_compare(reread, connection, NM_SETTING_COMPARE_FLAG_EXACT);

            nm_assert(reread_same
                      == nm_connection_compare(connection, reread, NM_SETTING_COMPARE_FLAG_EXACT));
            nm_assert(reread_same == ({
                          gs_unref_hashtable GHashTable *_settings = NULL;

                          (nm_connection_diff(reread,
                                              connection,
                                              NM_SETTING_COMPARE_FLAG_EXACT,
                                              &_settings)
                           && !_settings);
                      }));
        }
    }

    nm_utils_file_set_contents(path, kf_content_buf, kf_content_len, 0600, NULL, &local_err);
    if (local_err) {
        g_set_error(error,
                    NM_SETTINGS_ERROR,
                    NM_SETTINGS_ERROR_FAILED,
                    "error writing to file '%s': %s",
                    path,
                    local_err->message);
        return FALSE;
    }

    if (chown(path, owner_uid, owner_grp) < 0) {
        errsv = errno;
        g_set_error(error,
                    NM_SETTINGS_ERROR,
                    NM_SETTINGS_ERROR_FAILED,
                    "error chowning '%s': %s (%d)",
                    path,
                    nm_strerror_native(errsv),
                    errsv);
        unlink(path);
        return FALSE;
    }

    /* In case of updating the connection and changing the file path,
     * we need to remove the old one, not to end up with two connections.
     */
    if (existing_path && !existing_path_read_only && !nm_streq(path, existing_path))
        unlink(existing_path);

    NM_SET_OUT(out_reread, g_steal_pointer(&reread));
    NM_SET_OUT(out_reread_same, reread_same);
    NM_SET_OUT(out_path, g_steal_pointer(&path));

    return TRUE;
}

gboolean
nms_keyfile_writer_connection(NMConnection *                  connection,
                              gboolean                        is_nm_generated,
                              gboolean                        is_volatile,
                              gboolean                        is_external,
                              const char *                    shadowed_storage,
                              gboolean                        shadowed_owned,
                              const char *                    keyfile_dir,
                              const char *                    profile_dir,
                              const char *                    existing_path,
                              gboolean                        existing_path_read_only,
                              gboolean                        force_rename,
                              NMSKeyfileWriterAllowFilenameCb allow_filename_cb,
                              gpointer                        allow_filename_user_data,
                              char **                         out_path,
                              NMConnection **                 out_reread,
                              gboolean *                      out_reread_same,
                              GError **                       error)
{
    return _internal_write_connection(connection,
                                      is_nm_generated,
                                      is_volatile,
                                      is_external,
                                      shadowed_storage,
                                      shadowed_owned,
                                      keyfile_dir,
                                      profile_dir,
                                      TRUE,
                                      0,
                                      0,
                                      existing_path,
                                      existing_path_read_only,
                                      force_rename,
                                      allow_filename_cb,
                                      allow_filename_user_data,
                                      out_path,
                                      out_reread,
                                      out_reread_same,
                                      error);
}

gboolean
nms_keyfile_writer_test_connection(NMConnection * connection,
                                   const char *   keyfile_dir,
                                   uid_t          owner_uid,
                                   pid_t          owner_grp,
                                   char **        out_path,
                                   NMConnection **out_reread,
                                   gboolean *     out_reread_same,
                                   GError **      error)
{
    return _internal_write_connection(connection,
                                      FALSE,
                                      FALSE,
                                      FALSE,
                                      NULL,
                                      FALSE,
                                      keyfile_dir,
                                      keyfile_dir,
                                      FALSE,
                                      owner_uid,
                                      owner_grp,
                                      NULL,
                                      FALSE,
                                      FALSE,
                                      NULL,
                                      NULL,
                                      out_path,
                                      out_reread,
                                      out_reread_same,
                                      error);
}