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