varnish-cache/lib/libvarnish/vcli_proto.c
0
/*-
1
 * Copyright (c) 2010-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <sys/types.h>
33
#include <sys/uio.h>
34
35
#include <poll.h>
36
#include <stdint.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#include "vdef.h"
43
44
#include "vas.h"        // XXX Flexelint "not used" - but req'ed for assert()
45
#include "vcli.h"
46
#include "vsha256.h"
47
48
void
49 47275
VCLI_AuthResponse(int S_fd, const char *challenge,
50
    char response[CLI_AUTH_RESPONSE_LEN + 1])
51
{
52
        VSHA256_CTX ctx;
53
        uint8_t buf[VSHA256_LEN];
54
        int i;
55
56 47275
        assert(CLI_AUTH_RESPONSE_LEN == (VSHA256_LEN * 2));
57
58 47275
        VSHA256_Init(&ctx);
59 47275
        VSHA256_Update(&ctx, challenge, 32);
60 47275
        VSHA256_Update(&ctx, "\n", 1);
61 47275
        do {
62 12149675
                i = read(S_fd, buf, 1);
63 12149675
                if (i == 1)
64 12102400
                        VSHA256_Update(&ctx, buf, i);
65 12149675
        } while (i > 0);
66 47275
        VSHA256_Update(&ctx, challenge, 32);
67 47275
        VSHA256_Update(&ctx, "\n", 1);
68 47275
        VSHA256_Final(buf, &ctx);
69 1560075
        for (i = 0; i < VSHA256_LEN; i++)
70 1512800
                assert(snprintf(response + 2 * i, 3, "%02x", buf[i]) == 2);
71 47275
}
72
73
int
74 571170
VCLI_WriteResult(int fd, unsigned status, const char *result)
75
{
76
        int i, l;
77
        struct iovec iov[3];
78 571170
        char nl[2] = "\n";
79
        size_t len;
80
        char res[CLI_LINE0_LEN + 2];    /*
81
                                         * NUL + one more so we can catch
82
                                         * any misformats by snprintf
83
                                         */
84
85 571170
        assert(status >= 100);
86 571170
        assert(status <= 999);          /*lint !e650 const out of range */
87
88 571170
        len = strlen(result);
89
90 571170
        i = snprintf(res, sizeof res, "%-3d %-8zd\n", status, len);
91 571170
        assert(i == CLI_LINE0_LEN);
92 571170
        assert(strtoul(res + 3, NULL, 10) == len);
93
94 571170
        iov[0].iov_base = res;
95 571170
        iov[0].iov_len = CLI_LINE0_LEN;
96
97 571170
        iov[1].iov_base = (void*)(uintptr_t)result;     /* TRUST ME */
98 571170
        iov[1].iov_len = len;
99
100 571170
        iov[2].iov_base = nl;
101 571170
        iov[2].iov_len = 1;
102
103 2284680
        for (l = i = 0; i < 3; i++)
104 1713510
                l += iov[i].iov_len;
105 571170
        i = writev(fd, iov, 3);
106 571170
        return (i != l);
107
}
108
109
static int
110 1104502
read_tmo(int fd, char *ptr, unsigned len, double tmo)
111
{
112
        int i, j, to;
113
        struct pollfd pfd;
114
115 1104502
        if (tmo > 0)
116 1104502
                to = (int)(tmo * 1e3);
117
        else
118 0
                to = -1;
119 1104502
        pfd.fd = fd;
120 1104502
        pfd.events = POLLIN;
121 2208729
        for (j = 0; len > 0; ) {
122 1104527
                i = poll(&pfd, 1, to);
123 1104527
                if (i < 0) {
124 0
                        errno = EINTR;
125 0
                        return (-1);
126
                }
127 1104527
                if (i == 0) {
128 100
                        errno = ETIMEDOUT;
129 100
                        return (-1);
130
                }
131 1104427
                i = read(fd, ptr, len);
132 1104427
                if (i < 0)
133 0
                        return (i);
134 1104427
                if (i == 0)
135 200
                        break;
136 1104227
                len -= i;
137 1104227
                ptr += i;
138 1104227
                j += i;
139
        }
140 1104402
        return (j);
141 1104502
}
142
143
int
144 552401
VCLI_ReadResult(int fd, unsigned *status, char **ptr, double tmo)
145
{
146
        char res[CLI_LINE0_LEN];        /* For NUL */
147
        int i, j;
148
        unsigned u, v, s;
149 552401
        char *p = NULL;
150 552401
        const char *err = "CLI communication error (hdr)";
151
152 552401
        if (status == NULL)
153 125
                status = &s;
154 552401
        if (ptr != NULL)
155 529626
                *ptr = NULL;
156 552401
        do {
157 552401
                i = read_tmo(fd, res, CLI_LINE0_LEN, tmo);
158 552401
                if (i != CLI_LINE0_LEN)
159 300
                        break;
160
161 552101
                if (res[3] != ' ')
162 0
                        break;
163
164 552101
                if (res[CLI_LINE0_LEN - 1] != '\n')
165 0
                        break;
166
167 552101
                res[CLI_LINE0_LEN - 1] = '\0';
168 552101
                j = sscanf(res, "%u %u\n", &u, &v);
169 552101
                if (j != 2)
170 0
                        break;
171
172 552101
                err = "CLI communication error (body)";
173
174 552101
                *status = u;
175 552101
                p = malloc(v + 1L);
176 552101
                if (p == NULL)
177 0
                        break;
178
179 552101
                i = read_tmo(fd, p, v + 1, tmo);
180 552101
                if (i < 0)
181 0
                        break;
182 552101
                if (i != v + 1)
183 0
                        break;
184 552101
                if (p[v] != '\n')
185 0
                        break;
186
187 552101
                p[v] = '\0';
188 552101
                if (ptr == NULL)
189 22575
                        free(p);
190
                else
191 529526
                        *ptr = p;
192 552101
                return (0);
193
        } while (0);
194
195 300
        free(p);
196 300
        *status = CLIS_COMMS;
197 300
        if (ptr != NULL)
198 100
                *ptr = strdup(err);
199 300
        return (*status);
200 552401
}