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