s4:kdc streamline context initialization
[ira/wip.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
33 /*
34   context structure for operations on cldap packets
35 */
36 struct smb_krb5_socket {
37         struct socket_context *sock;
38
39         /* the fd event */
40         struct tevent_fd *fde;
41
42         NTSTATUS status;
43         DATA_BLOB request, reply;
44
45         struct packet_context *packet;
46
47         size_t partial_read;
48
49         krb5_krbhst_info *hi;
50 };
51
52 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
53 {
54         /* Otherwise krb5_free_context will try and close what we have already free()ed */
55         krb5_set_warn_dest(ctx->krb5_context, NULL);
56         krb5_closelog(ctx->krb5_context, ctx->logf);
57         krb5_free_context(ctx->krb5_context);
58         return 0;
59 }
60
61 /* We never close down the DEBUG system, and no need to unreference the use */
62 static void smb_krb5_debug_close(void *private_data) {
63         return;
64 }
65
66 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private_data)
67 {
68         DEBUG(3, ("Kerberos: %s\n", msg));
69 }
70
71 /*
72   handle recv events on a smb_krb5 socket
73 */
74 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
75 {
76         TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
77         DATA_BLOB blob;
78         size_t nread, dsize;
79
80         smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
81         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
82                 talloc_free(tmp_ctx);
83                 return;
84         }
85
86         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
87         if (blob.data == NULL && dsize != 0) {
88                 smb_krb5->status = NT_STATUS_NO_MEMORY;
89                 talloc_free(tmp_ctx);
90                 return;
91         }
92
93         smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
94         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
95                 talloc_free(tmp_ctx);
96                 return;
97         }
98         blob.length = nread;
99
100         if (nread == 0) {
101                 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
102                 talloc_free(tmp_ctx);
103                 return;
104         }
105
106         DEBUG(2,("Received smb_krb5 packet of length %d\n",
107                  (int)blob.length));
108
109         talloc_steal(smb_krb5, blob.data);
110         smb_krb5->reply = blob;
111         talloc_free(tmp_ctx);
112 }
113
114 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
115 {
116         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
117         talloc_steal(smb_krb5, data.data);
118         smb_krb5->reply = data;
119         smb_krb5->reply.length -= 4;
120         smb_krb5->reply.data += 4;
121         return NT_STATUS_OK;
122 }
123
124 /*
125   handle request timeouts
126 */
127 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
128                                   struct tevent_timer *te, struct timeval t,
129                                   void *private_data)
130 {
131         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
132         DEBUG(5,("Timed out smb_krb5 packet\n"));
133         smb_krb5->status = NT_STATUS_IO_TIMEOUT;
134 }
135
136 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
137 {
138         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
139         smb_krb5->status = status;
140 }
141
142 /*
143   handle send events on a smb_krb5 socket
144 */
145 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
146 {
147         NTSTATUS status;
148
149         size_t len;
150
151         len = smb_krb5->request.length;
152         status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
153
154         if (!NT_STATUS_IS_OK(status)) return;
155
156         TEVENT_FD_READABLE(smb_krb5->fde);
157
158         TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
159         return;
160 }
161
162
163 /*
164   handle fd events on a smb_krb5_socket
165 */
166 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
167                                  uint16_t flags, void *private_data)
168 {
169         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
170         switch (smb_krb5->hi->proto) {
171         case KRB5_KRBHST_UDP:
172                 if (flags & TEVENT_FD_READ) {
173                         smb_krb5_socket_recv(smb_krb5);
174                         return;
175                 }
176                 if (flags & TEVENT_FD_WRITE) {
177                         smb_krb5_socket_send(smb_krb5);
178                         return;
179                 }
180                 /* not reached */
181                 return;
182         case KRB5_KRBHST_TCP:
183                 if (flags & TEVENT_FD_READ) {
184                         packet_recv(smb_krb5->packet);
185                         return;
186                 }
187                 if (flags & TEVENT_FD_WRITE) {
188                         packet_queue_run(smb_krb5->packet);
189                         return;
190                 }
191                 /* not reached */
192                 return;
193         case KRB5_KRBHST_HTTP:
194                 /* can't happen */
195                 break;
196         }
197 }
198
199
200 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
201                                             void *data,
202                                             krb5_krbhst_info *hi,
203                                             time_t timeout,
204                                             const krb5_data *send_buf,
205                                             krb5_data *recv_buf)
206 {
207         krb5_error_code ret;
208         NTSTATUS status;
209         struct socket_address *remote_addr;
210         const char *name;
211         struct addrinfo *ai, *a;
212         struct smb_krb5_socket *smb_krb5;
213
214         struct tevent_context *ev = talloc_get_type(data, struct tevent_context);
215
216         DATA_BLOB send_blob = data_blob_const(send_buf->data, send_buf->length);
217
218         ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
219         if (ret) {
220                 return ret;
221         }
222
223         for (a = ai; a; a = ai->ai_next) {
224                 smb_krb5 = talloc(NULL, struct smb_krb5_socket);
225                 if (!smb_krb5) {
226                         return ENOMEM;
227                 }
228                 smb_krb5->hi = hi;
229
230                 switch (a->ai_family) {
231                 case PF_INET:
232                         name = "ipv4";
233                         break;
234 #ifdef HAVE_IPV6
235                 case PF_INET6:
236                         name = "ipv6";
237                         break;
238 #endif
239                 default:
240                         talloc_free(smb_krb5);
241                         return EINVAL;
242                 }
243
244                 status = NT_STATUS_INVALID_PARAMETER;
245                 switch (hi->proto) {
246                 case KRB5_KRBHST_UDP:
247                         status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
248                         break;
249                 case KRB5_KRBHST_TCP:
250                         status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
251                         break;
252                 case KRB5_KRBHST_HTTP:
253                         talloc_free(smb_krb5);
254                         return EINVAL;
255                 }
256                 if (!NT_STATUS_IS_OK(status)) {
257                         talloc_free(smb_krb5);
258                         continue;
259                 }
260
261                 talloc_steal(smb_krb5, smb_krb5->sock);
262
263                 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
264                 if (!remote_addr) {
265                         talloc_free(smb_krb5);
266                         continue;
267                 }
268
269                 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
270                 if (!NT_STATUS_IS_OK(status)) {
271                         talloc_free(smb_krb5);
272                         continue;
273                 }
274                 talloc_free(remote_addr);
275
276                 /* Setup the FDE, start listening for read events
277                  * from the start (otherwise we may miss a socket
278                  * drop) and mark as AUTOCLOSE along with the fde */
279
280                 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
281                 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
282                                               socket_get_fd(smb_krb5->sock),
283                                               TEVENT_FD_READ,
284                                               smb_krb5_socket_handler, smb_krb5);
285                 /* its now the job of the event layer to close the socket */
286                 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
287                 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
288
289                 tevent_add_timer(ev, smb_krb5,
290                                  timeval_current_ofs(timeout, 0),
291                                  smb_krb5_request_timeout, smb_krb5);
292
293                 smb_krb5->status = NT_STATUS_OK;
294                 smb_krb5->reply = data_blob(NULL, 0);
295
296                 switch (hi->proto) {
297                 case KRB5_KRBHST_UDP:
298                         TEVENT_FD_WRITEABLE(smb_krb5->fde);
299                         smb_krb5->request = send_blob;
300                         break;
301                 case KRB5_KRBHST_TCP:
302
303                         smb_krb5->packet = packet_init(smb_krb5);
304                         if (smb_krb5->packet == NULL) {
305                                 talloc_free(smb_krb5);
306                                 return ENOMEM;
307                         }
308                         packet_set_private(smb_krb5->packet, smb_krb5);
309                         packet_set_socket(smb_krb5->packet, smb_krb5->sock);
310                         packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
311                         packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
312                         packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
313                         packet_set_event_context(smb_krb5->packet, ev);
314                         packet_set_fde(smb_krb5->packet, smb_krb5->fde);
315
316                         smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
317                         RSIVAL(smb_krb5->request.data, 0, send_blob.length);
318                         memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
319                         packet_send(smb_krb5->packet, smb_krb5->request);
320                         break;
321                 case KRB5_KRBHST_HTTP:
322                         talloc_free(smb_krb5);
323                         return EINVAL;
324                 }
325                 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
326                         if (tevent_loop_once(ev) != 0) {
327                                 talloc_free(smb_krb5);
328                                 return EINVAL;
329                         }
330                 }
331                 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
332                         talloc_free(smb_krb5);
333                         continue;
334                 }
335
336                 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
337                         DEBUG(2,("Error reading smb_krb5 reply packet: %s\n", nt_errstr(smb_krb5->status)));
338                         talloc_free(smb_krb5);
339                         continue;
340                 }
341
342                 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
343                 if (ret) {
344                         talloc_free(smb_krb5);
345                         return ret;
346                 }
347                 talloc_free(smb_krb5);
348
349                 break;
350         }
351         if (a) {
352                 return 0;
353         }
354         return KRB5_KDC_UNREACH;
355 }
356
357 krb5_error_code
358 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
359                             struct tevent_context *ev,
360                             struct loadparm_context *lp_ctx,
361                             krb5_context *_krb5_context)
362 {
363         krb5_error_code ret;
364         char **config_files;
365         const char *config_file, *realm;
366         krb5_context krb5_ctx;
367
368         initialize_krb5_error_table();
369
370         ret = krb5_init_context(&krb5_ctx);
371         if (ret) {
372                 DEBUG(1,("krb5_init_context failed (%s)\n",
373                          error_message(ret)));
374                 return ret;
375         }
376
377         config_file = config_path(tmp_ctx, lp_ctx, "krb5.conf");
378         if (!config_file) {
379                 krb5_free_context(krb5_ctx);
380                 return ENOMEM;
381         }
382
383         /* Use our local krb5.conf file by default */
384         ret = krb5_prepend_config_files_default(config_file == NULL?"":config_file, &config_files);
385         if (ret) {
386                 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
387                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
388                 krb5_free_context(krb5_ctx);
389                 return ret;
390         }
391
392         ret = krb5_set_config_files(krb5_ctx, config_files);
393         krb5_free_config_files(config_files);
394         if (ret) {
395                 DEBUG(1,("krb5_set_config_files failed (%s)\n",
396                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
397                 krb5_free_context(krb5_ctx);
398                 return ret;
399         }
400
401         realm = lp_realm(lp_ctx);
402         if (realm != NULL) {
403                 ret = krb5_set_default_realm(krb5_ctx, realm);
404                 if (ret) {
405                         DEBUG(1,("krb5_set_default_realm failed (%s)\n",
406                                  smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
407                         krb5_free_context(krb5_ctx);
408                         return ret;
409                 }
410         }
411
412         *_krb5_context = krb5_ctx;
413         return 0;
414 }
415
416 krb5_error_code smb_krb5_init_context(void *parent_ctx,
417                                       struct tevent_context *ev,
418                                       struct loadparm_context *lp_ctx,
419                                        struct smb_krb5_context **smb_krb5_context)
420 {
421         krb5_error_code ret;
422         TALLOC_CTX *tmp_ctx;
423
424         initialize_krb5_error_table();
425
426         tmp_ctx = talloc_new(parent_ctx);
427         *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
428
429         if (!*smb_krb5_context || !tmp_ctx) {
430                 talloc_free(tmp_ctx);
431                 return ENOMEM;
432         }
433
434         ret = smb_krb5_init_context_basic(tmp_ctx, ev, lp_ctx,
435                                           &(*smb_krb5_context)->krb5_context);
436         if (ret) {
437                 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
438                          error_message(ret)));
439                 talloc_free(tmp_ctx);
440                 return ret;
441         }
442
443         /* TODO: Should we have a different name here? */
444         ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
445
446         if (ret) {
447                 DEBUG(1,("krb5_initlog failed (%s)\n",
448                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
449                 krb5_free_context((*smb_krb5_context)->krb5_context);
450                 talloc_free(tmp_ctx);
451                 return ret;
452         }
453
454         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
455
456         ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
457                                smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
458         if (ret) {
459                 DEBUG(1,("krb5_addlog_func failed (%s)\n",
460                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
461                 talloc_free(tmp_ctx);
462                 return ret;
463         }
464         krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
465
466         /* Set use of our socket lib */
467         ret = krb5_set_send_to_kdc_func((*smb_krb5_context)->krb5_context,
468                                         smb_krb5_send_and_recv_func,
469                                         ev);
470         if (ret) {
471                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
472                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
473                 talloc_free(tmp_ctx);
474                 return ret;
475         }
476
477         talloc_steal(parent_ctx, *smb_krb5_context);
478         talloc_free(tmp_ctx);
479
480         /* Set options in kerberos */
481
482         krb5_set_dns_canonicalize_hostname((*smb_krb5_context)->krb5_context,
483                                            lp_parm_bool(lp_ctx, NULL, "krb5", "set_dns_canonicalize", false));
484
485         return 0;
486 }
487