c8c90869ef4f1ccb9ea4c53dd6c3f69346dcc931
[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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "system/kerberos.h"
26 #include "auth/kerberos/kerberos.h"
27 #include "system/network.h"
28 #include "system/select.h"
29 #include "system/filesys.h"
30 #include "lib/socket/socket.h"
31 #include "lib/events/events.h"
32 /*
33   context structure for operations on cldap packets
34 */
35 struct smb_krb5_socket {
36         struct socket_context *sock;
37
38         /* the fd event */
39         struct fd_event *fde;
40
41         BOOL timeout;
42         NTSTATUS status;
43         DATA_BLOB request, reply, partial;
44
45         size_t partial_read;
46
47         krb5_krbhst_info *hi;
48 };
49
50 static int smb_krb5_context_destroy_1(void *ptr) 
51 {
52         struct smb_krb5_context *ctx = ptr;
53         krb5_free_context(ctx->krb5_context); 
54         return 0;
55 }
56
57 static int smb_krb5_context_destroy_2(void *ptr) 
58 {
59         struct smb_krb5_context *ctx = ptr;
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(ptr);
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(3, ("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         NTSTATUS status;
85         DATA_BLOB blob;
86         size_t nread, dsize;
87
88         switch (smb_krb5->hi->proto) {
89         case KRB5_KRBHST_UDP:
90                 status = socket_pending(smb_krb5->sock, &dsize);
91                 if (!NT_STATUS_IS_OK(status)) {
92                         talloc_free(tmp_ctx);
93                         return;
94                 }
95                 
96                 blob = data_blob_talloc(tmp_ctx, NULL, dsize);
97                 if (blob.data == NULL) {
98                         talloc_free(tmp_ctx);
99                         return;
100                 }
101                 
102                 status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread, 0);
103                 if (!NT_STATUS_IS_OK(status)) {
104                         talloc_free(tmp_ctx);
105                         return;
106                 }
107                 blob.length = nread;
108                 
109                 DEBUG(2,("Received smb_krb5 packet of length %d\n", 
110                          (int)blob.length));
111                 
112                 talloc_steal(smb_krb5, blob.data);
113                 smb_krb5->reply = blob;
114                 talloc_free(tmp_ctx);
115                 break;
116         case KRB5_KRBHST_TCP:
117                 if (smb_krb5->partial.length == 0) {
118                         smb_krb5->partial = data_blob_talloc(smb_krb5, NULL, 4);
119                         if (!smb_krb5->partial.data) {
120                                 smb_krb5->status = NT_STATUS_NO_MEMORY;
121                                 return;
122                         }
123                         
124                         smb_krb5->partial_read = 0;
125                 }
126                 
127                 /* read in the packet length */
128                 if (smb_krb5->partial_read < 4) {
129                         uint32_t packet_length;
130                         
131                         status = socket_recv(smb_krb5->sock, 
132                                              smb_krb5->partial.data + smb_krb5->partial_read,
133                                              4 - smb_krb5->partial_read,
134                                              &nread, 0);
135                         if (NT_STATUS_IS_ERR(status))  {
136                                 smb_krb5->status = status;
137                                 return;
138                         }
139                         if (!NT_STATUS_IS_OK(status)) {
140                                 return;
141                         }
142                         
143                         smb_krb5->partial_read += nread;
144                         if (smb_krb5->partial_read != 4) {
145                                 return;
146                         }
147                         
148                         packet_length = RIVAL(smb_krb5->partial.data, 0);
149                         
150                         smb_krb5->partial.data = talloc_realloc(smb_krb5, smb_krb5->partial.data, 
151                                                                 uint8_t, packet_length + 4);
152                         if (!smb_krb5->partial.data)  {
153                                 smb_krb5->status = NT_STATUS_NO_MEMORY;
154                                 return;
155                         }
156                         
157                         smb_krb5->partial.length = packet_length + 4;
158                 }
159                 
160                 /* read in the body */
161                 status = socket_recv(smb_krb5->sock, 
162                                      smb_krb5->partial.data + smb_krb5->partial_read,
163                                      smb_krb5->partial.length - smb_krb5->partial_read,
164                                      &nread, 0);
165                 if (NT_STATUS_IS_ERR(status))  {
166                         smb_krb5->status = status;
167                         return;
168                 }
169                 if (!NT_STATUS_IS_OK(status)) return;
170                 
171                 smb_krb5->partial_read += nread;
172
173                 if (smb_krb5->partial_read != smb_krb5->partial.length) return;
174
175                 smb_krb5->reply = data_blob_talloc(smb_krb5, smb_krb5->partial.data + 4, smb_krb5->partial.length - 4);
176                 break;
177         case KRB5_KRBHST_HTTP:
178                 return;
179         }
180 }
181
182 /*
183   handle request timeouts
184 */
185 static void smb_krb5_request_timeout(struct event_context *event_ctx, 
186                                   struct timed_event *te, struct timeval t,
187                                   void *private)
188 {
189         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
190         DEBUG(5,("Timed out smb_krb5 packet\n"));
191         smb_krb5->timeout = True;
192 }
193
194 /*
195   handle send events on a smb_krb5 socket
196 */
197 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
198 {
199         NTSTATUS status;
200
201         size_t len;
202         
203         len = smb_krb5->request.length;
204         status = socket_send(smb_krb5->sock, &smb_krb5->request, &len, 0);
205
206         if (!NT_STATUS_IS_OK(status)) return;
207         
208         EVENT_FD_READABLE(smb_krb5->fde);
209
210         EVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
211         return;
212 }
213
214
215 /*
216   handle fd events on a smb_krb5_socket
217 */
218 static void smb_krb5_socket_handler(struct event_context *ev, struct fd_event *fde,
219                                  uint16_t flags, void *private)
220 {
221         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private, struct smb_krb5_socket);
222         if (flags & EVENT_FD_WRITE) {
223                 smb_krb5_socket_send(smb_krb5);
224         } 
225         if (flags & EVENT_FD_READ) {
226                 smb_krb5_socket_recv(smb_krb5);
227         }
228 }
229
230
231 static krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
232                                                    void *data,
233                                                    krb5_krbhst_info *hi,
234                                                    const krb5_data *send_buf,
235                                                    krb5_data *recv_buf)
236 {
237         krb5_error_code ret;
238         NTSTATUS status;
239         char *remote_addr;
240         const char *name;
241         struct addrinfo *ai, *a;
242         struct smb_krb5_socket *smb_krb5;
243         int port;
244
245         struct event_context *ev = talloc_get_type(data, struct event_context);
246
247         DATA_BLOB send_blob = data_blob_const(send_buf->data, send_buf->length);
248
249         ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
250         if (ret) {
251                 return ret;
252         }
253
254         for (a = ai; a; a = ai->ai_next) {
255                 smb_krb5 = talloc(NULL, struct smb_krb5_socket);
256                 if (!smb_krb5) {
257                         return ENOMEM;
258                 }
259                 smb_krb5->hi = hi;
260                 
261                 switch (a->ai_family) {
262                 case PF_INET:
263                         name = "ipv4";
264                         break;
265                 case PF_INET6:
266                         name = "ipv6";
267                         break;
268                 default:
269                         talloc_free(smb_krb5);
270                         return EINVAL;
271                 }
272                 
273                 status = NT_STATUS_INVALID_PARAMETER;
274                 switch (hi->proto) {
275                 case KRB5_KRBHST_UDP:
276                         if (lp_parm_bool(-1, "krb5", "udp", True)) {
277                                 status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
278                                 break;
279                         }
280                 case KRB5_KRBHST_TCP:
281                         if (lp_parm_bool(-1, "krb5", "tcp", True)) {
282                                 status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
283                                 break;
284                         }
285                 case KRB5_KRBHST_HTTP:
286                         talloc_free(smb_krb5);
287                         return EINVAL;
288                 }
289                 if (!NT_STATUS_IS_OK(status)) {
290                         talloc_free(smb_krb5);
291                         continue;
292                 }
293
294                 talloc_steal(smb_krb5, smb_krb5->sock);
295                 
296                 switch (a->ai_family) {
297                 case PF_INET:
298                         remote_addr = talloc_strdup(smb_krb5, inet_ntoa(((struct sockaddr_in *)a->ai_addr)->sin_addr));
299                         port = ntohs(((struct sockaddr_in *)a->ai_addr)->sin_port);
300                         break;
301                 case PF_INET6:
302                 {
303                         char addr[128];
304                         const char *ret_addr;
305                         ret_addr = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)a->ai_addr)->sin6_addr, addr, sizeof(addr));
306                         if (ret_addr == NULL) {
307                                 talloc_free(smb_krb5);
308                                 return EINVAL;
309                         }
310         
311                         remote_addr = talloc_strdup(smb_krb5, ret_addr);
312                         port = ntohs(((struct sockaddr_in6 *)a->ai_addr)->sin6_port);
313                         break;
314                 }
315                 default:
316                         talloc_free(smb_krb5);
317                         return EINVAL;
318                 }
319                 
320                 status = socket_connect_ev(smb_krb5->sock, NULL, 0, remote_addr, port, 0, ev); 
321                 if (!NT_STATUS_IS_OK(status)) {
322                         talloc_free(smb_krb5);
323                         continue;
324                 }
325                 talloc_free(remote_addr);
326
327                 smb_krb5->fde = event_add_fd(ev, smb_krb5, 
328                                              socket_get_fd(smb_krb5->sock), 0,
329                                              smb_krb5_socket_handler, smb_krb5);
330
331                 event_add_timed(ev, smb_krb5, 
332                                 timeval_current_ofs(context->kdc_timeout, 0),
333                                 smb_krb5_request_timeout, smb_krb5);
334
335                 EVENT_FD_WRITEABLE(smb_krb5->fde);
336                 
337                 switch (hi->proto) {
338                 case KRB5_KRBHST_UDP:
339                         smb_krb5->request = send_blob;
340                         break;
341                 case KRB5_KRBHST_TCP:
342                         smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
343                         RSIVAL(smb_krb5->request.data, 0, send_blob.length);
344                         memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
345                         break;
346                 case KRB5_KRBHST_HTTP:
347                         talloc_free(smb_krb5);
348                         return EINVAL;
349                 }
350                 smb_krb5->timeout = False;
351                 smb_krb5->status = NT_STATUS_OK;
352                 smb_krb5->reply = data_blob(NULL, 0);
353                 smb_krb5->partial = data_blob(NULL, 0);
354
355                 while (!smb_krb5->timeout && (NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
356                         if (event_loop_once(ev) != 0) {
357                                 talloc_free(smb_krb5);
358                                 return EINVAL;
359                         }
360                 }
361                 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
362                         DEBUG(2,("Error reading smb_krb5 reply packet: %s\n", nt_errstr(smb_krb5->status)));
363                         talloc_free(smb_krb5);
364                         continue;
365                 }
366
367                 if (smb_krb5->timeout) {
368                         talloc_free(smb_krb5);
369                         continue;
370                 }
371
372                 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
373                 if (ret) {
374                         talloc_free(smb_krb5);
375                         return ret;
376                 }
377                 talloc_free(smb_krb5);
378                 
379                 break;
380         }
381         if (a) {
382                 return 0;
383         }
384         return KRB5_KDC_UNREACH;
385 }
386
387 /* NO internal data, so nothing to free */
388 static void smb_krb5_send_and_recv_close_func(krb5_context context, void *data) 
389 {
390         return;
391 }
392
393
394  krb5_error_code smb_krb5_init_context(void *parent_ctx, 
395                                        struct smb_krb5_context **smb_krb5_context) 
396 {
397         krb5_error_code ret;
398         TALLOC_CTX *tmp_ctx;
399         struct event_context *ev;
400         
401         initialize_krb5_error_table();
402         
403         tmp_ctx = talloc_new(parent_ctx);
404         *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
405
406         if (!*smb_krb5_context || !tmp_ctx) {
407                 talloc_free(*smb_krb5_context);
408                 talloc_free(tmp_ctx);
409                 return ENOMEM;
410         }
411
412         ret = krb5_init_context(&(*smb_krb5_context)->krb5_context);
413         if (ret) {
414                 DEBUG(1,("krb5_init_context failed (%s)\n", 
415                          error_message(ret)));
416                 return ret;
417         }
418
419         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_1);
420
421         if (lp_realm() && *lp_realm()) {
422                 char *upper_realm = strupper_talloc(tmp_ctx, lp_realm());
423                 if (!upper_realm) {
424                         DEBUG(1,("gensec_krb5_start: could not uppercase realm: %s\n", lp_realm()));
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         ev = event_context_find(*smb_krb5_context);
460         /* Set use of our socket lib */
461         ret = krb5_set_send_recv_func((*smb_krb5_context)->krb5_context, 
462                                       smb_krb5_send_and_recv_func, 
463                                       smb_krb5_send_and_recv_close_func, ev);
464         if (ret) {
465                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n", 
466                          smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
467                 talloc_free(tmp_ctx);
468                 return ret;
469         }
470
471         talloc_steal(parent_ctx, *smb_krb5_context);
472         talloc_free(tmp_ctx);
473
474         /* Set options in kerberos */
475
476         (*smb_krb5_context)->krb5_context->fdns = FALSE;
477
478         return 0;
479 }
480
481  void smb_krb5_free_context(struct smb_krb5_context *smb_krb5_context) 
482 {
483         talloc_free(smb_krb5_context);
484 }