File: cookie-support.c

package info (click to toggle)
snapd 2.73-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 81,460 kB
  • sloc: sh: 16,736; ansic: 16,652; python: 11,215; makefile: 1,966; exp: 190; awk: 58; xml: 22
file content (67 lines) | stat: -rw-r--r-- 2,100 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (C) 2017 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "cookie-support.h"

#include "../libsnap-confine-private/cleanup-funcs.h"
#include "../libsnap-confine-private/string-utils.h"
#include "../libsnap-confine-private/utils.h"

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define SC_COOKIE_DIR "/var/lib/snapd/cookie"

/**
 * Effective value of SC_COOKIE_DIR
 **/
static const char *sc_cookie_dir = SC_COOKIE_DIR;

char *sc_cookie_get_from_snapd(const char *snap_name, struct sc_error **errorp) {
    char context_path[PATH_MAX] = {0};
    struct sc_error *err = NULL;
    char *context = NULL;

    sc_must_snprintf(context_path, sizeof(context_path), "%s/snap.%s", sc_cookie_dir, snap_name);
    int fd SC_CLEANUP(sc_cleanup_close) = -1;
    fd = open(context_path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
    if (fd < 0) {
        err = sc_error_init_from_errno(errno, "warning: cannot open cookie file %s", context_path);
        goto out;
    }
    // large enough buffer for opaque cookie string
    char context_val[255] = {0};
    ssize_t n = read(fd, context_val, sizeof(context_val) - 1);
    if (n < 0) {
        err = sc_error_init_from_errno(errno, "cannot read cookie file %s", context_path);
        goto out;
    }
    context = strndup(context_val, n);
    if (context == NULL) {
        die("cannot duplicate snap cookie value");
    }

out:
    sc_error_forward(errorp, err);
    return context;
}