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