forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrdkafka_sasl.c
354 lines (293 loc) · 11.1 KB
/
rdkafka_sasl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* librdkafka - The Apache Kafka C/C++ library
*
* Copyright (c) 2015 Magnus Edenhill
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "rdkafka_int.h"
#include "rdkafka_transport.h"
#include "rdkafka_transport_int.h"
#include "rdkafka_sasl.h"
#include "rdkafka_sasl_int.h"
/**
* Send auth message with framing.
* This is a blocking call.
*/
int rd_kafka_sasl_send (rd_kafka_transport_t *rktrans,
const void *payload, int len,
char *errstr, size_t errstr_size) {
rd_buf_t buf;
rd_slice_t slice;
int32_t hdr;
rd_rkb_dbg(rktrans->rktrans_rkb, SECURITY, "SASL",
"Send SASL frame to broker (%d bytes)", len);
rd_buf_init(&buf, 1+1, sizeof(hdr));
hdr = htobe32(len);
rd_buf_write(&buf, &hdr, sizeof(hdr));
if (payload)
rd_buf_push(&buf, payload, len, NULL);
rd_slice_init_full(&slice, &buf);
/* Simulate blocking behaviour on non-blocking socket..
* FIXME: This isn't optimal but is highly unlikely to stall since
* the socket buffer will most likely not be exceeded. */
do {
int r;
r = (int)rd_kafka_transport_send(rktrans, &slice,
errstr, errstr_size);
if (r == -1) {
rd_rkb_dbg(rktrans->rktrans_rkb, SECURITY, "SASL",
"SASL send failed: %s", errstr);
rd_buf_destroy(&buf);
return -1;
}
if (rd_slice_remains(&slice) == 0)
break;
/* Avoid busy-looping */
rd_usleep(10*1000, NULL);
} while (1);
rd_buf_destroy(&buf);
return 0;
}
/**
* @brief Authentication succesful
*
* Transition to next connect state.
*/
void rd_kafka_sasl_auth_done (rd_kafka_transport_t *rktrans) {
/* Authenticated */
rd_kafka_broker_connect_up(rktrans->rktrans_rkb);
}
int rd_kafka_sasl_io_event (rd_kafka_transport_t *rktrans, int events,
char *errstr, size_t errstr_size) {
rd_kafka_buf_t *rkbuf;
int r;
const void *buf;
size_t len;
if (!(events & POLLIN))
return 0;
r = rd_kafka_transport_framed_recv(rktrans, &rkbuf,
errstr, errstr_size);
if (r == -1) {
if (!strcmp(errstr, "Disconnected"))
rd_snprintf(errstr, errstr_size,
"Disconnected: check client %s credentials "
"and broker logs",
rktrans->rktrans_rkb->rkb_rk->rk_conf.
sasl.mechanisms);
return -1;
} else if (r == 0) /* not fully received yet */
return 0;
rd_rkb_dbg(rktrans->rktrans_rkb, SECURITY, "SASL",
"Received SASL frame from broker (%"PRIusz" bytes)",
rkbuf ? rkbuf->rkbuf_totlen : 0);
if (rkbuf) {
rd_slice_init_full(&rkbuf->rkbuf_reader, &rkbuf->rkbuf_buf);
/* Seek past framing header */
rd_slice_seek(&rkbuf->rkbuf_reader, 4);
len = rd_slice_remains(&rkbuf->rkbuf_reader);
buf = rd_slice_ensure_contig(&rkbuf->rkbuf_reader, len);
} else {
buf = NULL;
len = 0;
}
r = rktrans->rktrans_rkb->rkb_rk->
rk_conf.sasl.provider->recv(rktrans, buf, len,
errstr, errstr_size);
rd_kafka_buf_destroy(rkbuf);
return r;
}
/**
* @brief Close SASL session (from transport code)
* @remark May be called on non-SASL transports (no-op)
*/
void rd_kafka_sasl_close (rd_kafka_transport_t *rktrans) {
const struct rd_kafka_sasl_provider *provider =
rktrans->rktrans_rkb->rkb_rk->rk_conf.
sasl.provider;
if (provider && provider->close)
provider->close(rktrans);
}
/**
* Initialize and start SASL authentication.
*
* Returns 0 on successful init and -1 on error.
*
* Locality: broker thread
*/
int rd_kafka_sasl_client_new (rd_kafka_transport_t *rktrans,
char *errstr, size_t errstr_size) {
int r;
rd_kafka_broker_t *rkb = rktrans->rktrans_rkb;
rd_kafka_t *rk = rkb->rkb_rk;
char *hostname, *t;
const struct rd_kafka_sasl_provider *provider =
rk->rk_conf.sasl.provider;
/* Verify broker support:
* - RD_KAFKA_FEATURE_SASL_GSSAPI - GSSAPI supported
* - RD_KAFKA_FEATURE_SASL_HANDSHAKE - GSSAPI, PLAIN and possibly
* other mechanisms supported. */
if (!strcmp(rk->rk_conf.sasl.mechanisms, "GSSAPI")) {
if (!(rkb->rkb_features & RD_KAFKA_FEATURE_SASL_GSSAPI)) {
rd_snprintf(errstr, errstr_size,
"SASL GSSAPI authentication not supported "
"by broker");
return -1;
}
} else if (!(rkb->rkb_features & RD_KAFKA_FEATURE_SASL_HANDSHAKE)) {
rd_snprintf(errstr, errstr_size,
"SASL Handshake not supported by broker "
"(required by mechanism %s)%s",
rk->rk_conf.sasl.mechanisms,
rk->rk_conf.api_version_request ? "" :
": try api.version.request=true");
return -1;
}
rd_strdupa(&hostname, rktrans->rktrans_rkb->rkb_nodename);
if ((t = strchr(hostname, ':')))
*t = '\0'; /* remove ":port" */
rd_rkb_dbg(rkb, SECURITY, "SASL",
"Initializing SASL client: service name %s, "
"hostname %s, mechanisms %s, provider %s",
rk->rk_conf.sasl.service_name, hostname,
rk->rk_conf.sasl.mechanisms,
provider->name);
r = provider->client_new(rktrans, hostname, errstr, errstr_size);
if (r != -1)
rd_kafka_transport_poll_set(rktrans, POLLIN);
return r;
}
/**
* Per handle SASL term.
*
* Locality: broker thread
*/
void rd_kafka_sasl_broker_term (rd_kafka_broker_t *rkb) {
const struct rd_kafka_sasl_provider *provider =
rkb->rkb_rk->rk_conf.sasl.provider;
if (provider->broker_term)
provider->broker_term(rkb);
}
/**
* Broker SASL init.
*
* Locality: broker thread
*/
void rd_kafka_sasl_broker_init (rd_kafka_broker_t *rkb) {
const struct rd_kafka_sasl_provider *provider =
rkb->rkb_rk->rk_conf.sasl.provider;
if (provider->broker_init)
provider->broker_init(rkb);
}
/**
* @brief Select SASL provider for configured mechanism (singularis)
* @returns 0 on success or -1 on failure.
*/
int rd_kafka_sasl_select_provider (rd_kafka_t *rk,
char *errstr, size_t errstr_size) {
const struct rd_kafka_sasl_provider *provider = NULL;
if (!strcmp(rk->rk_conf.sasl.mechanisms, "GSSAPI")) {
/* GSSAPI / Kerberos */
#ifdef _MSC_VER
provider = &rd_kafka_sasl_win32_provider;
#elif WITH_SASL_CYRUS
provider = &rd_kafka_sasl_cyrus_provider;
#endif
} else if (!strcmp(rk->rk_conf.sasl.mechanisms, "PLAIN")) {
/* SASL PLAIN */
provider = &rd_kafka_sasl_plain_provider;
} else if (!strncmp(rk->rk_conf.sasl.mechanisms, "SCRAM-SHA-",
strlen("SCRAM-SHA-"))) {
/* SASL SCRAM */
#if WITH_SASL_SCRAM
provider = &rd_kafka_sasl_scram_provider;
#endif
} else if (!strcmp(rk->rk_conf.sasl.mechanisms, "OAUTHBEARER")) {
/* SASL OAUTHBEARER */
#if WITH_SASL_OAUTHBEARER
provider = &rd_kafka_sasl_oauthbearer_provider;
rk->rk_oauthbearer = rd_calloc(1, sizeof(*rk->rk_oauthbearer));
rd_list_init(&rk->rk_oauthbearer->extensions, 0,
(void (*)(void *))rd_strtup_destroy);
#endif
} else {
/* Unsupported mechanism */
rd_snprintf(errstr, errstr_size,
"Unsupported SASL mechanism: %s",
rk->rk_conf.sasl.mechanisms);
return -1;
}
if (!provider) {
rd_snprintf(errstr, errstr_size,
"No provider for SASL mechanism %s"
": recompile librdkafka with "
#ifndef _MSC_VER
"libsasl2 or "
#endif
"openssl support. "
"Current build options:"
" PLAIN"
#ifdef _MSC_VER
" WindowsSSPI(GSSAPI)"
#endif
#if WITH_SASL_CYRUS
" SASL_CYRUS"
#endif
#if WITH_SASL_SCRAM
" SASL_SCRAM"
#endif
#if WITH_SASL_OAUTHBEARER
" OAUTHBEARER"
#endif
,
rk->rk_conf.sasl.mechanisms);
return -1;
}
rd_kafka_dbg(rk, SECURITY, "SASL",
"Selected provider %s for SASL mechanism %s",
provider->name, rk->rk_conf.sasl.mechanisms);
/* Validate SASL config */
if (provider->conf_validate &&
provider->conf_validate(rk, errstr, errstr_size) == -1)
return -1;
rk->rk_conf.sasl.provider = provider;
return 0;
}
/**
* Global SASL termination.
*/
void rd_kafka_sasl_global_term (void) {
#if WITH_SASL_CYRUS
rd_kafka_sasl_cyrus_global_term();
#endif
}
/**
* Global SASL init, called once per runtime.
*/
int rd_kafka_sasl_global_init (void) {
#if WITH_SASL_CYRUS
return rd_kafka_sasl_cyrus_global_init();
#else
return 0;
#endif
}