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