2 Unix SMB/CIFS implementation.
3 Wrapper for krb5_init_context
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2004
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "system/kerberos.h"
25 #include "heimdal/lib/krb5/krb5_locl.h"
26 #include "auth/kerberos/kerberos.h"
27 #include "lib/socket/socket.h"
28 #include "lib/stream/packet.h"
29 #include "system/network.h"
30 #include "lib/events/events.h"
34 context structure for operations on cldap packets
36 struct smb_krb5_socket {
37 struct socket_context *sock;
43 DATA_BLOB request, reply;
45 struct packet_context *packet;
52 static int smb_krb5_context_destroy_1(struct smb_krb5_context *ctx)
54 krb5_free_context(ctx->krb5_context);
58 static int smb_krb5_context_destroy_2(struct smb_krb5_context *ctx)
60 /* Otherwise krb5_free_context will try and close what we have already free()ed */
61 krb5_set_warn_dest(ctx->krb5_context, NULL);
62 krb5_closelog(ctx->krb5_context, ctx->logf);
63 smb_krb5_context_destroy_1(ctx);
67 /* We never close down the DEBUG system, and no need to unreference the use */
68 static void smb_krb5_debug_close(void *private) {
72 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private)
74 DEBUG(2, ("Kerberos: %s\n", msg));
78 handle recv events on a smb_krb5 socket
80 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
82 TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
86 smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
87 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
92 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
93 if (blob.data == NULL && dsize != 0) {
94 smb_krb5->status = NT_STATUS_NO_MEMORY;
99 smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
100 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
101 talloc_free(tmp_ctx);
107 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
108 talloc_free(tmp_ctx);
112 DEBUG(2,("Received smb_krb5 packet of length %d\n",
115 talloc_steal(smb_krb5, blob.data);
116 smb_krb5->reply = blob;
117 talloc_free(tmp_ctx);
120 static NTSTATUS smb_krb5_full_packet(void *private, DATA_BLOB data)
122 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
123 talloc_steal(smb_krb5, data.data);
124 smb_krb5->reply = data;
125 smb_krb5->reply.length -= 4;
126 smb_krb5->reply.data += 4;
131 handle request timeouts
133 static void smb_krb5_request_timeout(struct event_context *event_ctx,
134 struct timed_event *te, struct timeval t,
137 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
138 DEBUG(5,("Timed out smb_krb5 packet\n"));
139 smb_krb5->status = NT_STATUS_IO_TIMEOUT;
142 static void smb_krb5_error_handler(void *private, NTSTATUS status)
144 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
145 smb_krb5->status = status;
149 handle send events on a smb_krb5 socket
151 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
157 len = smb_krb5->request.length;
158 status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
160 if (!NT_STATUS_IS_OK(status)) return;
162 EVENT_FD_READABLE(smb_krb5->fde);
164 EVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
170 handle fd events on a smb_krb5_socket
172 static void smb_krb5_socket_handler(struct event_context *ev, struct fd_event *fde,
173 uint16_t flags, void *private)
175 struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
176 switch (smb_krb5->hi->proto) {
177 case KRB5_KRBHST_UDP:
178 if (flags & EVENT_FD_READ) {
179 smb_krb5_socket_recv(smb_krb5);
182 if (flags & EVENT_FD_WRITE) {
183 smb_krb5_socket_send(smb_krb5);
188 case KRB5_KRBHST_TCP:
189 if (flags & EVENT_FD_READ) {
190 packet_recv(smb_krb5->packet);
193 if (flags & EVENT_FD_WRITE) {
194 packet_queue_run(smb_krb5->packet);
199 case KRB5_KRBHST_HTTP:
206 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
208 krb5_krbhst_info *hi,
209 const krb5_data *send_buf,
214 struct socket_address *remote_addr;
216 struct addrinfo *ai, *a;
217 struct smb_krb5_socket *smb_krb5;
219 struct event_context *ev = talloc_get_type(data, struct event_context);
221 DATA_BLOB send_blob = data_blob_const(send_buf->data, send_buf->length);
223 ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
228 for (a = ai; a; a = ai->ai_next) {
229 smb_krb5 = talloc(NULL, struct smb_krb5_socket);
235 switch (a->ai_family) {
245 talloc_free(smb_krb5);
249 status = NT_STATUS_INVALID_PARAMETER;
251 case KRB5_KRBHST_UDP:
252 if (lp_parm_bool(-1, "krb5", "udp", True)) {
253 status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
256 case KRB5_KRBHST_TCP:
257 if (lp_parm_bool(-1, "krb5", "tcp", True)) {
258 status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
261 case KRB5_KRBHST_HTTP:
262 talloc_free(smb_krb5);
265 if (!NT_STATUS_IS_OK(status)) {
266 talloc_free(smb_krb5);
270 talloc_steal(smb_krb5, smb_krb5->sock);
272 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
274 talloc_free(smb_krb5);
278 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
279 if (!NT_STATUS_IS_OK(status)) {
280 talloc_free(smb_krb5);
283 talloc_free(remote_addr);
285 /* Setup the FDE, start listening for read events
286 * from the start (otherwise we may miss a socket
287 * drop) and mark as AUTOCLOSE along with the fde */
289 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
290 smb_krb5->fde = event_add_fd(ev, smb_krb5->sock,
291 socket_get_fd(smb_krb5->sock),
292 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
293 smb_krb5_socket_handler, smb_krb5);
294 /* its now the job of the event layer to close the socket */
295 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
297 event_add_timed(ev, smb_krb5,
298 timeval_current_ofs(context->kdc_timeout, 0),
299 smb_krb5_request_timeout, smb_krb5);
302 smb_krb5->status = NT_STATUS_OK;
303 smb_krb5->reply = data_blob(NULL, 0);
306 case KRB5_KRBHST_UDP:
307 EVENT_FD_WRITEABLE(smb_krb5->fde);
308 smb_krb5->request = send_blob;
310 case KRB5_KRBHST_TCP:
312 smb_krb5->packet = packet_init(smb_krb5);
313 if (smb_krb5->packet == NULL) {
314 talloc_free(smb_krb5);
317 packet_set_private(smb_krb5->packet, smb_krb5);
318 packet_set_socket(smb_krb5->packet, smb_krb5->sock);
319 packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
320 packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
321 packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
322 packet_set_event_context(smb_krb5->packet, ev);
323 packet_set_fde(smb_krb5->packet, smb_krb5->fde);
325 smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
326 RSIVAL(smb_krb5->request.data, 0, send_blob.length);
327 memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
328 packet_send(smb_krb5->packet, smb_krb5->request);
330 case KRB5_KRBHST_HTTP:
331 talloc_free(smb_krb5);
334 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
335 if (event_loop_once(ev) != 0) {
336 talloc_free(smb_krb5);
340 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
341 talloc_free(smb_krb5);
345 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
346 DEBUG(2,("Error reading smb_krb5 reply packet: %s\n", nt_errstr(smb_krb5->status)));
347 talloc_free(smb_krb5);
351 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
353 talloc_free(smb_krb5);
356 talloc_free(smb_krb5);
363 return KRB5_KDC_UNREACH;
366 krb5_error_code smb_krb5_init_context(void *parent_ctx,
367 struct event_context *ev,
368 struct smb_krb5_context **smb_krb5_context)
373 const char *config_file;
375 initialize_krb5_error_table();
377 tmp_ctx = talloc_new(parent_ctx);
378 *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
380 if (!*smb_krb5_context || !tmp_ctx) {
381 talloc_free(tmp_ctx);
385 ret = krb5_init_context(&(*smb_krb5_context)->krb5_context);
387 DEBUG(1,("krb5_init_context failed (%s)\n",
388 error_message(ret)));
389 talloc_free(tmp_ctx);
393 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_1);
395 config_file = config_path(tmp_ctx, "krb5.conf");
397 talloc_free(tmp_ctx);
401 /* Use our local krb5.conf file by default */
402 ret = krb5_prepend_config_files_default(config_file, &config_files);
404 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
405 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
406 talloc_free(tmp_ctx);
410 ret = krb5_set_config_files((*smb_krb5_context)->krb5_context,
412 krb5_free_config_files(config_files);
414 DEBUG(1,("krb5_set_config_files failed (%s)\n",
415 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
416 talloc_free(tmp_ctx);
420 if (lp_realm() && *lp_realm()) {
421 char *upper_realm = strupper_talloc(tmp_ctx, lp_realm());
423 DEBUG(1,("gensec_krb5_start: could not uppercase realm: %s\n", lp_realm()));
424 talloc_free(tmp_ctx);
427 ret = krb5_set_default_realm((*smb_krb5_context)->krb5_context, upper_realm);
429 DEBUG(1,("krb5_set_default_realm failed (%s)\n",
430 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
431 talloc_free(tmp_ctx);
436 /* TODO: Should we have a different name here? */
437 ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
440 DEBUG(1,("krb5_initlog failed (%s)\n",
441 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
442 talloc_free(tmp_ctx);
446 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_2);
448 ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
449 smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
451 DEBUG(1,("krb5_addlog_func failed (%s)\n",
452 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
453 talloc_free(tmp_ctx);
456 krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
458 /* Set use of our socket lib */
459 ret = krb5_set_send_to_kdc_func((*smb_krb5_context)->krb5_context,
460 smb_krb5_send_and_recv_func,
463 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
464 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
465 talloc_free(tmp_ctx);
469 talloc_steal(parent_ctx, *smb_krb5_context);
470 talloc_free(tmp_ctx);
472 /* Set options in kerberos */
474 krb5_set_dns_canonicalize_hostname((*smb_krb5_context)->krb5_context,
475 lp_parm_bool(-1, "krb5", "set_dns_canonicalize", false));