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