summaryrefslogtreecommitdiffstats
path: root/src/sss_client/ssh/sss_ssh.c
blob: dbdbbeee8faadd46ba9fbf534192d18f097976e7 (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
/*
    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 <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <talloc.h>

#include <popt.h>
#include <locale.h>
#include <libintl.h>

#include "config.h"
#include "util/crypto/sss_crypto.h"
#include "sss_client/sss_cli.h"
#include "sss_client/ssh/sss_ssh.h"

/* FIXME - split from tools_util to create a common function */
void usage(poptContext pc, const char *error)
{
    poptPrintUsage(pc, stderr, 0);
    if (error) fprintf(stderr, "%s", error);
}

/* FIXME - split from tools_util to create a common function */
int set_locale(void)
{
    char *c;

    c = setlocale(LC_ALL, "");
    if (c == NULL) {
        return EIO;
    }

    errno = 0;
    c = bindtextdomain(PACKAGE, LOCALEDIR);
    if (c == NULL) {
        return errno;
    }

    errno = 0;
    c = textdomain(PACKAGE);
    if (c == NULL) {
        return errno;
    }

    return EOK;
}

errno_t
sss_ssh_get_pubkeys(TALLOC_CTX *mem_ctx,
                    enum sss_cli_command command,
                    const char *name,
                    struct sss_ssh_pubkey **pubkeys,
                    size_t *pubkeys_len)
{
    TALLOC_CTX *tmp_ctx;
    errno_t ret = EOK;
    uint32_t name_len;
    size_t req_len;
    uint8_t *req = NULL;
    size_t c = 0;
    struct sss_cli_req_data rd;
    int req_ret, req_errno;
    uint8_t *rep = NULL;
    size_t rep_len;
    uint32_t count, reserved, len, i;
    struct sss_ssh_pubkey *result = NULL;

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        return ENOMEM;
    }

    /* build request */
    name_len = strlen(name)+1;
    req_len = 2*sizeof(uint32_t) + name_len;

    req = talloc_array(tmp_ctx, uint8_t, req_len);
    if (!req) {
        ret = ENOMEM;
        goto done;
    }

    SAFEALIGN_SET_UINT32(req+c, 0, &c);
    SAFEALIGN_SET_UINT32(req+c, name_len, &c);
    safealign_memcpy(req+c, name, name_len, &c);

    /* send request */
    rd.data = req;
    rd.len = req_len;

    req_ret = sss_ssh_make_request(command, &rd, &rep, &rep_len, &req_errno);
    if (req_ret != SSS_STATUS_SUCCESS) {
        ret = EFAULT;
        goto done;
    }
    if (req_errno != EOK) {
        ret = req_errno;
        goto done;
    }

    /* parse reply */
    c = 0;
    if (rep_len-c < 2*sizeof(uint32_t)) {
        ret = EINVAL;
        goto done;
    }

    SAFEALIGN_COPY_UINT32(&count, rep+c, &c);

    SAFEALIGN_COPY_UINT32(&reserved, rep+c, &c);
    if (reserved != 0) {
        ret = EINVAL;
        goto done;
    }

    if (count > 0) {
        result = talloc_zero_array(tmp_ctx, struct sss_ssh_pubkey, count);
        if (!result) {
            ret = ENOMEM;
            goto done;
        }
    }

    for (i = 0; i < count; i++) {
        if (rep_len-c < 2*sizeof(uint32_t)) {
            ret = EINVAL;
            goto done;
        }

        SAFEALIGN_COPY_UINT32(&result[i].flags, rep+c, &c);
        if (result[i].flags != 0) {
            ret = EINVAL;
            goto done;
        }

        SAFEALIGN_COPY_UINT32(&len, rep+c, &c);

        if (rep_len-c < len + sizeof(uint32_t)) {
            ret = EINVAL;
            goto done;
        }

        result[i].name = talloc_array(result, char, len);
        if (!result[i].name) {
            ret = ENOMEM;
            goto done;
        }

        safealign_memcpy(result[i].name, rep+c, len, &c);
        if (strnlen(result[i].name, len) != len-1) {
            ret = EINVAL;
            goto done;
        }

        SAFEALIGN_COPY_UINT32(&len, rep+c, &c);

        if (rep_len-c < len) {
            ret = EINVAL;
            goto done;
        }

        result[i].key = talloc_array(result, uint8_t, len);
        if (!result[i].key) {
            ret = ENOMEM;
            goto done;
        }

        safealign_memcpy(result[i].key, rep+c, len, &c);
        result[i].key_len = len;
    }

    *pubkeys = result ? talloc_steal(mem_ctx, result) : NULL;
    *pubkeys_len = count;

done:
    talloc_free(tmp_ctx);

    return ret;
}

char *
sss_ssh_get_pubkey_algorithm(TALLOC_CTX *mem_ctx,
                             struct sss_ssh_pubkey *pubkey)
{
    size_t c = 0;
    uint32_t algo_len;
    char *algo;

    SAFEALIGN_COPY_UINT32(&algo_len, pubkey->key, &c);
    algo_len = ntohl(algo_len);

    algo = talloc_zero_array(mem_ctx, char, algo_len+1);
    if (!algo) {
        return NULL;
    }

    memcpy(algo, pubkey->key+c, algo_len);

    return algo;
}

errno_t
sss_ssh_format_pubkey(TALLOC_CTX *mem_ctx,
                      struct sss_ssh_pubkey *pubkey,
                      enum sss_ssh_pubkey_format format,
                      char **result)
{
    TALLOC_CTX *tmp_ctx;
    errno_t ret = EOK;
    char *pk;
    char *algo;
    char *out;

    if (!pubkey) {
        return EINVAL;
    }

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) {
        return ENOMEM;
    }

    pk = sss_base64_encode(tmp_ctx, pubkey->key, pubkey->key_len);
    if (!pk) {
        ret = ENOMEM;
        goto done;
    }

    switch (format) {
    case SSS_SSH_FORMAT_RAW:
        /* base64-encoded key blob */

        out = talloc_steal(mem_ctx, pk);

        break;

    case SSS_SSH_FORMAT_OPENSSH:
        /* OpenSSH authorized_keys/known_hosts format */

        algo = sss_ssh_get_pubkey_algorithm(tmp_ctx, pubkey);
        if (!algo) {
            ret = ENOMEM;
            goto done;
        }

        out = talloc_asprintf(tmp_ctx, "%s %s %s",
                              algo, pk, pubkey->name);
        if (!out) {
            ret = ENOMEM;
            goto done;
        }

        talloc_steal(mem_ctx, out);

        break;
    }

    *result = out;

done:
    talloc_free(tmp_ctx);

    return ret;
}