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 1262
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 1262
        VSHA256_Init(&ctx);
59 1262
        VSHA256_Update(&ctx, challenge, 32);
60 1262
        VSHA256_Update(&ctx, "\n", 1);
61
        do {
62 324334
                i = read(S_fd, buf, 1);
63 324334
                if (i == 1)
64 323072
                        VSHA256_Update(&ctx, buf, i);
65 324334
        } while (i > 0);
66 1262
        VSHA256_Update(&ctx, challenge, 32);
67 1262
        VSHA256_Update(&ctx, "\n", 1);
68 1262
        VSHA256_Final(buf, &ctx);
69 41646
        for (i = 0; i < VSHA256_LEN; i++)
70 40384
                assert(snprintf(response + 2 * i, 3, "%02x", buf[i]) == 2);
71 1262
}
72
73
int
74 14773
VCLI_WriteResult(int fd, unsigned status, const char *result)
75
{
76
        int i, l;
77
        struct iovec iov[3];
78 14773
        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 14773
        assert(status >= 100);
86 14773
        assert(status <= 999);          /*lint !e650 const out of range */
87
88 14773
        len = strlen(result);
89
90 14773
        i = snprintf(res, sizeof res,
91
            "%-3d %-8zd\n", status, len);
92 14773
        assert(i == CLI_LINE0_LEN);
93 14773
        assert(strtoul(res + 3, NULL, 10) == len);
94
95 14773
        iov[0].iov_base = res;
96 14773
        iov[0].iov_len = CLI_LINE0_LEN;
97
98 14773
        iov[1].iov_base = (void*)(uintptr_t)result;     /* TRUST ME */
99 14773
        iov[1].iov_len = len;
100
101 14773
        iov[2].iov_base = nl;
102 14773
        iov[2].iov_len = 1;
103
104 59092
        for (l = i = 0; i < 3; i++)
105 44319
                l += iov[i].iov_len;
106 14773
        i = writev(fd, iov, 3);
107 14773
        return (i != l);
108
}
109
110
static int
111 28303
read_tmo(int fd, char *ptr, unsigned len, double tmo)
112
{
113
        int i, j, to;
114
        struct pollfd pfd;
115
116 28303
        if (tmo > 0)
117 28303
                to = (int)(tmo * 1e3);
118
        else
119 0
                to = -1;
120 28303
        pfd.fd = fd;
121 28303
        pfd.events = POLLIN;
122 84908
        for (j = 0; len > 0; ) {
123 28303
                i = poll(&pfd, 1, to);
124 28303
                if (i < 0) {
125 0
                        errno = EINTR;
126 0
                        return (-1);
127
                }
128 28303
                if (i == 0) {
129 0
                        errno = ETIMEDOUT;
130 0
                        return (-1);
131
                }
132 28303
                i = read(fd, ptr, len);
133 28303
                if (i < 0)
134 0
                        return (i);
135 28303
                if (i == 0)
136 1
                        break;
137 28302
                len -= i;
138 28302
                ptr += i;
139 28302
                j += i;
140
        }
141 28303
        return (j);
142
}
143
144
int
145 14152
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 14152
        char *p = NULL;
151 14152
        const char *err = "CLI communication error (hdr)";
152
153 14152
        if (status == NULL)
154 0
                status = &s;
155 14152
        if (ptr != NULL)
156 14152
                *ptr = NULL;
157
        do {
158 14152
                i = read_tmo(fd, res, CLI_LINE0_LEN, tmo);
159 14152
                if (i != CLI_LINE0_LEN)
160 1
                        break;
161
162 14151
                if (res[3] != ' ')
163 0
                        break;
164
165 14151
                if (res[CLI_LINE0_LEN - 1] != '\n')
166 0
                        break;
167
168 14151
                res[CLI_LINE0_LEN - 1] = '\0';
169 14151
                j = sscanf(res, "%u %u\n", &u, &v);
170 14151
                if (j != 2)
171 0
                        break;
172
173 14151
                err = "CLI communication error (body)";
174
175 14151
                *status = u;
176 14151
                p = malloc(v + 1L);
177 14151
                if (p == NULL)
178 0
                        break;
179
180 14151
                i = read_tmo(fd, p, v + 1, tmo);
181 14151
                if (i < 0)
182 0
                        break;
183 14151
                if (i != v + 1)
184 0
                        break;
185 14151
                if (p[v] != '\n')
186 0
                        break;
187
188 14151
                p[v] = '\0';
189 14151
                if (ptr == NULL)
190 0
                        free(p);
191
                else
192 14151
                        *ptr = p;
193 14151
                return (0);
194
        } while (0);
195
196 1
        free(p);
197 1
        *status = CLIS_COMMS;
198 1
        if (ptr != NULL)
199 1
                *ptr = strdup(err);
200 1
        return (*status);
201
}