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