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
#include "vtim.h"
48
49
void
50 77800
VCLI_AuthResponse(int S_fd, const char *challenge,
51
    char response[CLI_AUTH_RESPONSE_LEN + 1])
52
{
53
        VSHA256_CTX ctx;
54
        uint8_t buf[VSHA256_LEN];
55
        int i;
56
57 77800
        assert(CLI_AUTH_RESPONSE_LEN == (VSHA256_LEN * 2));
58
59 77800
        VSHA256_Init(&ctx);
60 77800
        VSHA256_Update(&ctx, challenge, 32);
61 77800
        VSHA256_Update(&ctx, "\n", 1);
62 77800
        do {
63 19994600
                i = read(S_fd, buf, 1);
64 19994600
                if (i == 1)
65 19916800
                        VSHA256_Update(&ctx, buf, i);
66 19994600
        } while (i > 0);
67 77800
        VSHA256_Update(&ctx, challenge, 32);
68 77800
        VSHA256_Update(&ctx, "\n", 1);
69 77800
        VSHA256_Final(buf, &ctx);
70 2567400
        for (i = 0; i < VSHA256_LEN; i++)
71 2489600
                assert(snprintf(response + 2 * i, 3, "%02x", buf[i]) == 2);
72 77800
}
73
74
int
75 944338
VCLI_WriteResult(int fd, unsigned status, const char *result)
76
{
77
        int i, l;
78
        struct iovec iov[3];
79 944338
        char nl[2] = "\n";
80
        size_t len;
81
        char res[CLI_LINE0_LEN + 2];    /*
82
                                         * NUL + one more so we can catch
83
                                         * any misformats by snprintf
84
                                         */
85
86 944338
        assert(status >= 100);
87 944338
        assert(status <= 999);          /*lint !e650 const out of range */
88
89 944338
        len = strlen(result);
90
91 944338
        i = snprintf(res, sizeof res, "%-3d %-8zd\n", status, len);
92 944338
        assert(i == CLI_LINE0_LEN);
93 944338
        assert(strtoul(res + 3, NULL, 10) == len);
94
95 944338
        iov[0].iov_base = res;
96 944338
        iov[0].iov_len = CLI_LINE0_LEN;
97
98 944338
        iov[1].iov_base = (void*)(uintptr_t)result;     /* TRUST ME */
99 944338
        iov[1].iov_len = len;
100
101 944338
        iov[2].iov_base = nl;
102 944338
        iov[2].iov_len = 1;
103
104 3777352
        for (l = i = 0; i < 3; i++)
105 2833014
                l += iov[i].iov_len;
106 944338
        i = writev(fd, iov, 3);
107 944338
        return (i != l);
108
}
109
110
static int
111 1826134
read_tmo(int fd, char *ptr, unsigned len, double tmo)
112
{
113
        int i, j;
114
        struct pollfd pfd;
115
116 1826134
        pfd.fd = fd;
117 1826134
        pfd.events = POLLIN;
118 3651829
        for (j = 0; len > 0; ) {
119 1826175
                i = poll(&pfd, 1, VTIM_poll_tmo(tmo));
120 1826175
                if (i < 0) {
121 0
                        errno = EINTR;
122 0
                        return (-1);
123
                }
124 1826175
                if (i == 0) {
125 160
                        errno = ETIMEDOUT;
126 160
                        return (-1);
127
                }
128 1826015
                i = read(fd, ptr, len);
129 1826015
                if (i < 0)
130 0
                        return (i);
131 1826015
                if (i == 0)
132 320
                        break;
133 1825695
                len -= i;
134 1825695
                ptr += i;
135 1825695
                j += i;
136
        }
137 1825974
        return (j);
138 1826134
}
139
140
int
141 913307
VCLI_ReadResult(int fd, unsigned *status, char **ptr, double tmo)
142
{
143
        char res[CLI_LINE0_LEN];        /* For NUL */
144
        int i, j;
145
        unsigned u, v, s;
146 913307
        char *p = NULL;
147 913307
        const char *err = "CLI communication error (hdr)";
148
149 913307
        if (status == NULL)
150 200
                status = &s;
151 913307
        if (ptr != NULL)
152 875707
                *ptr = NULL;
153 913307
        do {
154 913307
                i = read_tmo(fd, res, CLI_LINE0_LEN, tmo);
155 913307
                if (i != CLI_LINE0_LEN)
156 480
                        break;
157
158 912827
                if (res[3] != ' ')
159 0
                        break;
160
161 912827
                if (res[CLI_LINE0_LEN - 1] != '\n')
162 0
                        break;
163
164 912827
                res[CLI_LINE0_LEN - 1] = '\0';
165 912827
                j = sscanf(res, "%u %u\n", &u, &v);
166 912827
                if (j != 2)
167 0
                        break;
168
169 912827
                err = "CLI communication error (body)";
170
171 912827
                *status = u;
172 912827
                p = malloc(v + 1L);
173 912827
                if (p == NULL)
174 0
                        break;
175
176 912827
                i = read_tmo(fd, p, v + 1, tmo);
177 912827
                if (i < 0)
178 0
                        break;
179 912827
                if (i != v + 1)
180 0
                        break;
181 912827
                if (p[v] != '\n')
182 0
                        break;
183
184 912827
                p[v] = '\0';
185 912827
                if (ptr == NULL)
186 37280
                        free(p);
187
                else
188 875547
                        *ptr = p;
189 912827
                return (0);
190
        } while (0);
191
192 480
        free(p);
193 480
        *status = CLIS_COMMS;
194 480
        if (ptr != NULL)
195 160
                *ptr = strdup(err);
196 480
        return (*status);
197 913307
}