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