s4-kerberos Mention the remote address we fail to contact the KDC on
[idra/samba.git] / source4 / auth / kerberos / krb5_init_context.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrapper for krb5_init_context
4
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher 2004
8
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.
13
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.
18
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/>.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include <tevent.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 "param/param.h"
31 #include "libcli/resolve/resolve.h"
32 #include "../lib/tsocket/tsocket.h"
33
34 /*
35   context structure for operations on cldap packets
36 */
37 struct smb_krb5_socket {
38         struct socket_context *sock;
39
40         /* the fd event */
41         struct tevent_fd *fde;
42
43         NTSTATUS status;
44         DATA_BLOB request, reply;
45
46         struct packet_context *packet;
47
48         size_t partial_read;
49
50         krb5_krbhst_info *hi;
51 };
52
53 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
54 {
55         /* Otherwise krb5_free_context will try and close what we have already free()ed */
56         krb5_set_warn_dest(ctx->krb5_context, NULL);
57         krb5_closelog(ctx->krb5_context, ctx->logf);
58         krb5_free_context(ctx->krb5_context);
59         return 0;
60 }
61
62 /* We never close down the DEBUG system, and no need to unreference the use */
63 static void smb_krb5_debug_close(void *private_data) {
64         return;
65 }
66
67 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private_data)
68 {
69         DEBUG(3, ("Kerberos: %s\n", msg));
70 }
71
72 /*
73   handle recv events on a smb_krb5 socket
74 */
75 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
76 {
77         TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
78         DATA_BLOB blob;
79         size_t nread, dsize;
80
81         smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
82         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
83                 talloc_free(tmp_ctx);
84                 return;
85         }
86
87         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
88         if (blob.data == NULL && dsize != 0) {
89                 smb_krb5->status = NT_STATUS_NO_MEMORY;
90                 talloc_free(tmp_ctx);
91                 return;
92         }
93
94         smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
95         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
96                 talloc_free(tmp_ctx);
97                 return;
98         }
99         blob.length = nread;
100
101         if (nread == 0) {
102                 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
103                 talloc_free(tmp_ctx);
104                 return;
105         }
106
107         DEBUG(2,("Received smb_krb5 packet of length %d\n",
108                  (int)blob.length));
109
110         talloc_steal(smb_krb5, blob.data);
111         smb_krb5->reply = blob;
112         talloc_free(tmp_ctx);
113 }
114
115 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
116 {
117         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
118         talloc_steal(smb_krb5, data.data);
119         smb_krb5->reply = data;
120         smb_krb5->reply.length -= 4;
121         smb_krb5->reply.data += 4;
122         return NT_STATUS_OK;
123 }
124
125 /*
126   handle request timeouts
127 */
128 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
129                                   struct tevent_timer *te, struct timeval t,
130                                   void *private_data)
131 {
132         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
133         DEBUG(5,("Timed out smb_krb5 packet\n"));
134         smb_krb5->status = NT_STATUS_IO_TIMEOUT;
135 }
136
137 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
138 {
139         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
140         smb_krb5->status = status;
141 }
142
143 /*
144   handle send events on a smb_krb5 socket
145 */
146 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
147 {
148         NTSTATUS status;
149
150         size_t len;
151
152         len = smb_krb5->request.length;
153         status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
154
155         if (!NT_STATUS_IS_OK(status)) return;
156
157         TEVENT_FD_READABLE(smb_krb5->fde);
158
159         TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
160         return;
161 }
162
163
164 /*
165   handle fd events on a smb_krb5_socket
166 */
167 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
168                                  uint16_t flags, void *private_data)
169 {
170         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
171         switch (smb_krb5->hi->proto) {
172         case KRB5_KRBHST_UDP:
173                 if (flags & TEVENT_FD_READ) {
174                         smb_krb5_socket_recv(smb_krb5);
175                         return;
176                 }
177                 if (flags & TEVENT_FD_WRITE) {
178                         smb_krb5_socket_send(smb_krb5);
179                         return;
180                 }
181                 /* not reached */
182                 return;
183         case KRB5_KRBHST_TCP:
184                 if (flags & TEVENT_FD_READ) {
185                         packet_recv(smb_krb5->packet);
186                         return;
187                 }
188                 if (flags & TEVENT_FD_WRITE) {
189                         packet_queue_run(smb_krb5->packet);
190                         return;
191                 }
192                 /* not reached */
193                 return;
194         case KRB5_KRBHST_HTTP:
195                 /* can't happen */
196                 break;
197         }
198 }
199
200
201 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
202                                             void *data,
203                                             krb5_krbhst_info *hi,
204                                             time_t timeout,
205                                             const krb5_data *send_buf,
206                                             krb5_data *recv_buf)
207 {
208         krb5_error_code ret;
209         NTSTATUS status;
210         struct socket_address *remote_addr;
211         const char *name;
212         struct addrinfo *ai, *a;
213         struct smb_krb5_socket *smb_krb5;
214
215         DATA_BLOB send_blob;
216
217         struct tevent_context *ev;
218         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
219         if (!tmp_ctx) {
220                 return ENOMEM;
221         }
222
223         if (!data) {
224                 /* If no event context was available, then create one for this loop */
225                 ev = tevent_context_init(tmp_ctx);
226                 if (!ev) {
227                         talloc_free(tmp_ctx);
228                         return ENOMEM;
229                 }
230         } else {
231                 ev = talloc_get_type_abort(data, struct tevent_context);
232         }
233
234         send_blob = data_blob_const(send_buf->data, send_buf->length);
235
236         ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
237         if (ret) {
238                 talloc_free(tmp_ctx);
239                 return ret;
240         }
241
242         for (a = ai; a; a = ai->ai_next) {
243                 smb_krb5 = talloc(tmp_ctx, struct smb_krb5_socket);
244                 if (!smb_krb5) {
245                         talloc_free(tmp_ctx);
246                         return ENOMEM;
247                 }
248                 smb_krb5->hi = hi;
249
250                 switch (a->ai_family) {
251                 case PF_INET:
252                         name = "ipv4";
253                         break;
254 #ifdef HAVE_IPV6
255                 case PF_INET6:
256                         name = "ipv6";
257                         break;
258 #endif
259                 default:
260                         talloc_free(tmp_ctx);
261                         return EINVAL;
262                 }
263
264                 status = NT_STATUS_INVALID_PARAMETER;
265                 switch (hi->proto) {
266                 case KRB5_KRBHST_UDP:
267                         status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
268                         break;
269                 case KRB5_KRBHST_TCP:
270                         status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
271                         break;
272                 case KRB5_KRBHST_HTTP:
273                         talloc_free(tmp_ctx);
274                         return EINVAL;
275                 }
276                 if (!NT_STATUS_IS_OK(status)) {
277                         talloc_free(smb_krb5);
278                         continue;
279                 }
280
281                 talloc_steal(smb_krb5, smb_krb5->sock);
282
283                 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
284                 if (!remote_addr) {
285                         talloc_free(smb_krb5);
286                         continue;
287                 }
288
289                 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
290                 if (!NT_STATUS_IS_OK(status)) {
291                         talloc_free(smb_krb5);
292                         continue;
293                 }
294                 talloc_free(remote_addr);
295
296                 /* Setup the FDE, start listening for read events
297                  * from the start (otherwise we may miss a socket
298                  * drop) and mark as AUTOCLOSE along with the fde */
299
300                 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
301                 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
302                                               socket_get_fd(smb_krb5->sock),
303                                               TEVENT_FD_READ,
304                                               smb_krb5_socket_handler, smb_krb5);
305                 /* its now the job of the event layer to close the socket */
306                 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
307                 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
308
309                 tevent_add_timer(ev, smb_krb5,
310                                  timeval_current_ofs(timeout, 0),
311                                  smb_krb5_request_timeout, smb_krb5);
312
313                 smb_krb5->status = NT_STATUS_OK;
314                 smb_krb5->reply = data_blob(NULL, 0);
315
316                 switch (hi->proto) {
317                 case KRB5_KRBHST_UDP:
318                         TEVENT_FD_WRITEABLE(smb_krb5->fde);
319                         smb_krb5->request = send_blob;
320                         break;
321                 case KRB5_KRBHST_TCP:
322
323                         smb_krb5->packet = packet_init(smb_krb5);
324                         if (smb_krb5->packet == NULL) {
325                                 talloc_free(smb_krb5);
326                                 return ENOMEM;
327                         }
328                         packet_set_private(smb_krb5->packet, smb_krb5);
329                         packet_set_socket(smb_krb5->packet, smb_krb5->sock);
330                         packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
331                         packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
332                         packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
333                         packet_set_event_context(smb_krb5->packet, ev);
334                         packet_set_fde(smb_krb5->packet, smb_krb5->fde);
335
336                         smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
337                         RSIVAL(smb_krb5->request.data, 0, send_blob.length);
338                         memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
339                         packet_send(smb_krb5->packet, smb_krb5->request);
340                         break;
341                 case KRB5_KRBHST_HTTP:
342                         talloc_free(tmp_ctx);
343                         return EINVAL;
344                 }
345                 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
346                         if (tevent_loop_once(ev) != 0) {
347                                 talloc_free(tmp_ctx);
348                                 return EINVAL;
349                         }
350
351                         /* After each and every event loop, reset the
352                          * send_to_kdc pointers to what they were when
353                          * we entered this loop.  That way, if a
354                          * nested event has invalidated them, we put
355                          * it back before we return to the heimdal
356                          * code */
357                         ret = krb5_set_send_to_kdc_func(context,
358                                                         smb_krb5_send_and_recv_func,
359                                                         data);
360                         if (ret != 0) {
361                                 talloc_free(tmp_ctx);
362                                 return ret;
363                         }
364                 }
365                 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
366                         talloc_free(smb_krb5);
367                         continue;
368                 }
369
370                 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
371                         struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
372                         const char *addr_string = NULL;
373                         if (addr) {
374                                 addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
375                         } else {
376                                 addr_string = NULL;
377                         }
378                         DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
379                                  addr_string));
380                         talloc_free(smb_krb5);
381                         continue;
382                 }
383
384                 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
385                 if (ret) {
386                         talloc_free(tmp_ctx);
387                         return ret;
388                 }
389                 talloc_free(smb_krb5);
390
391                 break;
392         }
393         talloc_free(tmp_ctx);
394         if (a) {
395                 return 0;
396         }
397         return KRB5_KDC_UNREACH;
398 }
399
400 krb5_error_code
401 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
402                             struct loadparm_context *lp_ctx,
403                             krb5_context *_krb5_context)
404 {
405         krb5_error_code ret;
406         char **config_files;
407         const char *config_file, *realm;
408         krb5_context krb5_ctx;
409
410         initialize_krb5_error_table();
411
412         ret = krb5_init_context(&krb5_ctx);
413         if (ret) {
414                 DEBUG(1,("krb5_init_context failed (%s)\n",
415                          error_message(ret)));
416                 return ret;
417         }
418
419         config_file = config_path(tmp_ctx, lp_ctx, "krb5.conf");
420         if (!config_file) {
421                 krb5_free_context(krb5_ctx);
422                 return ENOMEM;
423         }
424
425         /* Use our local krb5.conf file by default */
426         ret = krb5_prepend_config_files_default(config_file == NULL?"":config_file, &config_files);
427         if (ret) {
428                 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
429                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
430                 krb5_free_context(krb5_ctx);
431                 return ret;
432         }
433
434         ret = krb5_set_config_files(krb5_ctx, config_files);
435         krb5_free_config_files(config_files);
436         if (ret) {
437                 DEBUG(1,("krb5_set_config_files failed (%s)\n",
438                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
439                 krb5_free_context(krb5_ctx);
440                 return ret;
441         }
442
443         realm = lpcfg_realm(lp_ctx);
444         if (realm != NULL) {
445                 ret = krb5_set_default_realm(krb5_ctx, realm);
446                 if (ret) {
447                         DEBUG(1,("krb5_set_default_realm failed (%s)\n",
448                                  smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
449                         krb5_free_context(krb5_ctx);
450                         return ret;
451                 }
452         }
453
454         *_krb5_context = krb5_ctx;
455         return 0;
456 }
457
458 krb5_error_code smb_krb5_init_context(void *parent_ctx,
459                                       struct tevent_context *ev,
460                                       struct loadparm_context *lp_ctx,
461                                       struct smb_krb5_context **smb_krb5_context)
462 {
463         krb5_error_code ret;
464         TALLOC_CTX *tmp_ctx;
465
466         initialize_krb5_error_table();
467
468         tmp_ctx = talloc_new(parent_ctx);
469         *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
470
471         if (!*smb_krb5_context || !tmp_ctx) {
472                 talloc_free(tmp_ctx);
473                 return ENOMEM;
474         }
475
476         ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx,
477                                           &(*smb_krb5_context)->krb5_context);
478         if (ret) {
479                 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
480                          error_message(ret)));
481                 talloc_free(tmp_ctx);
482                 return ret;
483         }
484
485         /* TODO: Should we have a different name here? */
486         ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
487
488         if (ret) {
489                 DEBUG(1,("krb5_initlog failed (%s)\n",
490                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
491                 krb5_free_context((*smb_krb5_context)->krb5_context);
492                 talloc_free(tmp_ctx);
493                 return ret;
494         }
495
496         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
497
498         ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
499                                smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
500         if (ret) {
501                 DEBUG(1,("krb5_addlog_func failed (%s)\n",
502                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
503                 talloc_free(tmp_ctx);
504                 return ret;
505         }
506         krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
507
508         /* Set use of our socket lib */
509         if (ev) {
510                 struct tevent_context *previous_ev;
511                 ret = smb_krb5_context_set_event_ctx(*smb_krb5_context,
512                                                      ev, &previous_ev);
513                 if (ret) {
514                         talloc_free(tmp_ctx);
515                         return ret;
516                 }
517         }
518
519         talloc_steal(parent_ctx, *smb_krb5_context);
520         talloc_free(tmp_ctx);
521
522         /* Set options in kerberos */
523
524         krb5_set_dns_canonicalize_hostname((*smb_krb5_context)->krb5_context,
525                                            lpcfg_parm_bool(lp_ctx, NULL, "krb5", "set_dns_canonicalize", false));
526
527         return 0;
528 }
529
530 krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
531                                                struct tevent_context *ev,
532                                                struct tevent_context **previous_ev)
533 {
534         int ret;
535         if (!ev) {
536                 return EINVAL;
537         }
538
539         *previous_ev = smb_krb5_context->current_ev;
540
541         smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
542         if (!smb_krb5_context->current_ev) {
543                 return ENOMEM;
544         }
545
546         /* Set use of our socket lib */
547         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
548                                         smb_krb5_send_and_recv_func,
549                                         ev);
550         if (ret) {
551                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
552                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
553                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
554                 talloc_free(tmp_ctx);
555                 talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
556                 smb_krb5_context->current_ev = NULL;
557                 return ret;
558         }
559         return 0;
560 }
561
562 krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
563                                                   struct tevent_context *previous_ev,
564                                                   struct tevent_context *ev)
565 {
566         int ret;
567         talloc_unlink(smb_krb5_context, ev);
568         /* If there was a mismatch with things happening on a stack, then don't wipe things */
569         smb_krb5_context->current_ev = previous_ev;
570         /* Set use of our socket lib */
571         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
572                                         smb_krb5_send_and_recv_func,
573                                         previous_ev);
574         if (ret) {
575                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
576                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
577                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
578                 talloc_free(tmp_ctx);
579                 return ret;
580         }
581         return 0;
582 }