summaryrefslogtreecommitdiffstats
path: root/src/responder/kcm/kcmsrv_ccache_key.c
blob: 59d60453c5d5e28ccda8f98c63125954640d0e8b (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
/*
   SSSD

   Copyright (C) Red Hat, 2020

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   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 "config.h"

#include <stdio.h>
#include <talloc.h>

#include "util/util.h"
#include "responder/kcm/kcmsrv_ccache_pvt.h"

/*
 * The secrets store is a key-value store at heart. We store the UUID
 * and the name in the key to allow easy lookups by either part.
 */
#define SEC_KEY_SEPARATOR   '-'

const char *sec_key_create(TALLOC_CTX *mem_ctx,
                           const char *name,
                           uuid_t uuid)
{
    char uuid_str[UUID_STR_SIZE];

    uuid_unparse(uuid, uuid_str);
    return talloc_asprintf(mem_ctx,
                           "%s%c%s", uuid_str, SEC_KEY_SEPARATOR, name);
}

static bool sec_key_valid(const char *sec_key)
{
    if (sec_key == NULL) {
        return false;
    }

    if (strlen(sec_key) < UUID_STR_SIZE + 1) {
        /* One char for separator (at UUID_STR_SIZE, because strlen doesn't
         * include the '\0', but UUID_STR_SIZE does) and at least one for
         * the name */
        DEBUG(SSSDBG_CRIT_FAILURE, "Key %s is too short\n", sec_key);
        return false;
    }

    if (sec_key[UUID_STR_SIZE - 1] != SEC_KEY_SEPARATOR) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Key doesn't contain the separator\n");
        return false;
    }

    return true;
}

errno_t sec_key_parse(TALLOC_CTX *mem_ctx,
                      const char *sec_key,
                      const char **_name,
                      uuid_t uuid)
{
    char uuid_str[UUID_STR_SIZE];

    if (!sec_key_valid(sec_key)) {
        return EINVAL;
    }

    strncpy(uuid_str, sec_key, sizeof(uuid_str) - 1);
    if (sec_key[UUID_STR_SIZE - 1] != SEC_KEY_SEPARATOR) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Key doesn't contain the separator\n");
        return EINVAL;
    }
    uuid_str[UUID_STR_SIZE - 1] = '\0';

    *_name = talloc_strdup(mem_ctx, sec_key + UUID_STR_SIZE);
    if (*_name == NULL) {
        return ENOMEM;
    }
    uuid_parse(uuid_str, uuid);

    return EOK;
}

errno_t sec_key_get_uuid(const char *sec_key,
                         uuid_t uuid)
{
    char uuid_str[UUID_STR_SIZE];

    if (!sec_key_valid(sec_key)) {
        return EINVAL;
    }

    strncpy(uuid_str, sec_key, UUID_STR_SIZE - 1);
    uuid_str[UUID_STR_SIZE - 1] = '\0';
    uuid_parse(uuid_str, uuid);
    return EOK;
}

const char *sec_key_get_name(const char *sec_key)
{
    if (!sec_key_valid(sec_key)) {
        return NULL;
    }

    return sec_key + UUID_STR_SIZE;
}

bool sec_key_match_name(const char *sec_key,
                        const char *name)
{
    if (!sec_key_valid(sec_key) || name == NULL) {
        return false;
    }

    return strcmp(sec_key + UUID_STR_SIZE, name) == 0;
}

bool sec_key_match_uuid(const char *sec_key,
                        uuid_t uuid)
{
    errno_t ret;
    uuid_t key_uuid;

    /* Clear uuid value to avoid cppcheck warning. */
    uuid_clear(key_uuid);

    ret = sec_key_get_uuid(sec_key, key_uuid);
    if (ret != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, "Cannot convert key to UUID\n");
        return false;
    }

    return uuid_compare(key_uuid, uuid) == 0;
}