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
|
/*
Authors:
Jan Cholasta <jcholast@redhat.com>
Copyright (C) 2012 Red Hat
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 <arpa/inet.h>
#include "db/sysdb.h"
#include "util/util.h"
#include "util/crypto/sss_crypto.h"
#include "util/sss_ssh.h"
errno_t
sss_ssh_make_ent(TALLOC_CTX *mem_ctx,
struct ldb_message *msg,
struct sss_ssh_ent **result)
{
TALLOC_CTX *tmp_ctx;
struct sss_ssh_ent *res = NULL;
errno_t ret;
const char *name;
struct ldb_message_element *el;
unsigned int i;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
name = ldb_msg_find_attr_as_string(msg, SYSDB_NAME, NULL);
if (!name) {
ret = EINVAL;
DEBUG(SSSDBG_CRIT_FAILURE, ("Host is missing name attribute\n"));
goto done;
}
res = talloc_zero(tmp_ctx, struct sss_ssh_ent);
if (!res) {
ret = ENOMEM;
goto done;
}
res->name = talloc_strdup(res, name);
if (!res->name) {
ret = ENOMEM;
goto done;
}
el = ldb_msg_find_element(msg, SYSDB_SSH_PUBKEY);
if (el) {
res->num_pubkeys = el->num_values;
res->pubkeys = talloc_array(res, struct sss_ssh_pubkey,
res->num_pubkeys);
if (!res->pubkeys) {
ret = ENOMEM;
goto done;
}
for (i = 0; i < el->num_values; i++) {
res->pubkeys[i].data = sss_base64_decode(res->pubkeys,
(char *)el->values[i].data, &res->pubkeys[i].data_len);
if (!res->pubkeys[i].data) {
ret = ENOMEM;
goto done;
}
}
}
el = ldb_msg_find_element(msg, SYSDB_NAME_ALIAS);
if (el) {
res->num_aliases = el->num_values;
res->aliases = talloc_array(res, char *, res->num_aliases);
if (!res->aliases) {
ret = ENOMEM;
goto done;
}
for (i = 0; i < el->num_values; i++) {
res->aliases[i] = talloc_strdup(res->aliases,
(char *)el->values[i].data);
if (!res->aliases[i]) {
ret = ENOMEM;
goto done;
}
}
}
*result = talloc_steal(mem_ctx, res);
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t
sss_ssh_get_pubkey_algorithm(TALLOC_CTX *mem_ctx,
struct sss_ssh_pubkey *pubkey,
char **result)
{
size_t c = 0;
uint32_t algo_len;
char *algo;
if (pubkey->data_len < 5) {
return EINVAL;
}
SAFEALIGN_COPY_UINT32(&algo_len, pubkey->data, &c);
algo_len = ntohl(algo_len);
if (algo_len < 1 || algo_len > 64 || algo_len > pubkey->data_len - 4) {
/* the maximum length of 64 is defined in RFC 4250 */
return EINVAL;
}
algo = talloc_zero_array(mem_ctx, char, algo_len+1);
if (!algo) {
return ENOMEM;
}
memcpy(algo, pubkey->data+c, algo_len);
*result = algo;
return EOK;
}
errno_t
sss_ssh_format_pubkey(TALLOC_CTX *mem_ctx,
struct sss_ssh_ent *ent,
struct sss_ssh_pubkey *pubkey,
enum sss_ssh_pubkey_format format,
const char *comment,
char **result)
{
TALLOC_CTX *tmp_ctx;
errno_t ret;
char *blob;
char *algo;
char *out = NULL;
if (!comment) {
comment = ent->name;
}
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
blob = sss_base64_encode(tmp_ctx, pubkey->data, pubkey->data_len);
if (!blob) {
ret = ENOMEM;
goto done;
}
switch (format) {
case SSS_SSH_FORMAT_RAW:
/* base64-encoded key blob */
out = talloc_steal(mem_ctx, blob);
break;
case SSS_SSH_FORMAT_OPENSSH:
/* OpenSSH authorized_keys/known_hosts format */
ret = sss_ssh_get_pubkey_algorithm(tmp_ctx, pubkey, &algo);
if (ret != EOK) {
goto done;
}
out = talloc_asprintf(mem_ctx, "%s %s %s", algo, blob, comment);
if (!out) {
ret = ENOMEM;
goto done;
}
break;
}
*result = out;
ret = EOK;
done:
talloc_free(tmp_ctx);
return ret;
}
|