No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / appl / kx / krb5.c
blob475b71aa339c8175d986eb15c13fefbbe91b552b
1 /*
2 * Copyright (c) 1995 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "kx.h"
36 __RCSID("$Heimdal: krb5.c 17450 2006-05-05 11:11:43Z lha $"
37 "$NetBSD$");
39 #ifdef KRB5
41 struct krb5_kx_context {
42 krb5_context context;
43 krb5_keyblock *keyblock;
44 krb5_crypto crypto;
45 krb5_principal client;
46 krb5_log_facility *log;
50 typedef struct krb5_kx_context krb5_kx_context;
52 #define K5DATA(kc) ((krb5_kx_context*)kc->data)
53 #define CONTEXT(kc) (K5DATA(kc)->context)
56 * Destroy the krb5 context in `c'.
59 static void
60 krb5_destroy (kx_context *kc)
62 if (K5DATA(kc)->keyblock)
63 krb5_free_keyblock (CONTEXT(kc), K5DATA(kc)->keyblock);
64 if (K5DATA(kc)->crypto)
65 krb5_crypto_destroy (CONTEXT(kc), K5DATA(kc)->crypto);
66 if (K5DATA(kc)->client)
67 krb5_free_principal (CONTEXT(kc), K5DATA(kc)->client);
68 if (CONTEXT(kc))
69 krb5_free_context (CONTEXT(kc));
70 memset (kc->data, 0, sizeof(krb5_kx_context));
71 free (kc->data);
75 * Read the authentication information from `s' and return 0 if
76 * succesful, else -1.
79 static int
80 krb5_authenticate (kx_context *kc, int s)
82 krb5_auth_context auth_context = NULL;
83 krb5_error_code ret;
84 krb5_principal server;
85 const char *host = kc->host;
87 ret = krb5_sname_to_principal (CONTEXT(kc),
88 host, "host", KRB5_NT_SRV_HST, &server);
89 if (ret) {
90 krb5_warn (CONTEXT(kc), ret, "krb5_sname_to_principal: %s", host);
91 return 1;
94 ret = krb5_sendauth (CONTEXT(kc),
95 &auth_context,
96 &s,
97 KX_VERSION,
98 NULL,
99 server,
100 AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY,
101 NULL,
102 NULL,
103 NULL,
104 NULL,
105 NULL,
106 NULL);
107 if (ret) {
108 if(ret != KRB5_SENDAUTH_BADRESPONSE)
109 krb5_warn (CONTEXT(kc), ret, "krb5_sendauth: %s", host);
110 return 1;
113 ret = krb5_auth_con_getkey (CONTEXT(kc), auth_context,
114 &K5DATA(kc)->keyblock);
115 if (ret) {
116 krb5_warn (CONTEXT(kc), ret, "krb5_auth_con_getkey: %s", host);
117 krb5_auth_con_free (CONTEXT(kc), auth_context);
118 return 1;
121 ret = krb5_crypto_init (CONTEXT(kc), K5DATA(kc)->keyblock,
122 0, &K5DATA(kc)->crypto);
123 if (ret) {
124 krb5_warn (CONTEXT(kc), ret, "krb5_crypto_init");
125 krb5_auth_con_free (CONTEXT(kc), auth_context);
126 return 1;
128 return 0;
132 * Read an encapsulated krb5 packet from `fd' into `buf' (of size
133 * `len'). Return the number of bytes read or 0 on EOF or -1 on
134 * error.
137 static ssize_t
138 krb5_read (kx_context *kc,
139 int fd, void *buf, size_t len)
141 size_t data_len, outer_len;
142 krb5_error_code ret;
143 unsigned char tmp[4];
144 krb5_data data;
145 int l;
147 l = krb5_net_read (CONTEXT(kc), &fd, tmp, 4);
148 if (l == 0)
149 return l;
150 if (l != 4)
151 return -1;
152 data_len = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
153 outer_len = krb5_get_wrapped_length (CONTEXT(kc),
154 K5DATA(kc)->crypto, data_len);
155 if (outer_len > len)
156 return -1;
157 if (krb5_net_read (CONTEXT(kc), &fd, buf, outer_len) != outer_len)
158 return -1;
160 ret = krb5_decrypt (CONTEXT(kc), K5DATA(kc)->crypto,
161 KRB5_KU_OTHER_ENCRYPTED,
162 buf, outer_len, &data);
163 if (ret) {
164 krb5_warn (CONTEXT(kc), ret, "krb5_decrypt");
165 return -1;
167 if (data_len > data.length) {
168 krb5_data_free (&data);
169 return -1;
171 memmove (buf, data.data, data_len);
172 krb5_data_free (&data);
173 return data_len;
177 * Write an encapsulated krb5 packet on `fd' with the data in `buf,
178 * len'. Return len or -1 on error.
181 static ssize_t
182 krb5_write(kx_context *kc,
183 int fd, const void *buf, size_t len)
185 krb5_data data;
186 krb5_error_code ret;
187 unsigned char tmp[4];
188 size_t outlen;
190 ret = krb5_encrypt (CONTEXT(kc), K5DATA(kc)->crypto,
191 KRB5_KU_OTHER_ENCRYPTED,
192 buf, len, &data);
193 if (ret){
194 krb5_warn (CONTEXT(kc), ret, "krb5_write");
195 return -1;
198 outlen = data.length;
199 tmp[0] = (len >> 24) & 0xFF;
200 tmp[1] = (len >> 16) & 0xFF;
201 tmp[2] = (len >> 8) & 0xFF;
202 tmp[3] = (len >> 0) & 0xFF;
204 if (krb5_net_write (CONTEXT(kc), &fd, tmp, 4) != 4 ||
205 krb5_net_write (CONTEXT(kc), &fd, data.data, outlen) != outlen) {
206 krb5_data_free (&data);
207 return -1;
209 krb5_data_free (&data);
210 return len;
214 * Copy from the unix socket `from_fd' encrypting to `to_fd'.
215 * Return 0, -1 or len.
218 static int
219 copy_out (kx_context *kc, int from_fd, int to_fd)
221 char buf[32768];
222 ssize_t len;
224 len = read (from_fd, buf, sizeof(buf));
225 if (len == 0)
226 return 0;
227 if (len < 0) {
228 krb5_warn (CONTEXT(kc), errno, "read");
229 return len;
231 return krb5_write (kc, to_fd, buf, len);
235 * Copy from the socket `from_fd' decrypting to `to_fd'.
236 * Return 0, -1 or len.
239 static int
240 copy_in (kx_context *kc, int from_fd, int to_fd)
242 char buf[33000]; /* XXX */
244 ssize_t len;
246 len = krb5_read (kc, from_fd, buf, sizeof(buf));
247 if (len == 0)
248 return 0;
249 if (len < 0) {
250 krb5_warn (CONTEXT(kc), errno, "krb5_read");
251 return len;
254 return krb5_net_write (CONTEXT(kc), &to_fd, buf, len);
258 * Copy data between `fd1' and `fd2', encrypting in one direction and
259 * decrypting in the other.
262 static int
263 krb5_copy_encrypted (kx_context *kc, int fd1, int fd2)
265 for (;;) {
266 fd_set fdset;
267 int ret;
269 if (fd1 >= FD_SETSIZE || fd2 >= FD_SETSIZE) {
270 krb5_warnx (CONTEXT(kc), "fd too large");
271 return 1;
274 FD_ZERO(&fdset);
275 FD_SET(fd1, &fdset);
276 FD_SET(fd2, &fdset);
278 ret = select (max(fd1, fd2)+1, &fdset, NULL, NULL, NULL);
279 if (ret < 0 && errno != EINTR) {
280 krb5_warn (CONTEXT(kc), errno, "select");
281 return 1;
283 if (FD_ISSET(fd1, &fdset)) {
284 ret = copy_out (kc, fd1, fd2);
285 if (ret <= 0)
286 return ret;
288 if (FD_ISSET(fd2, &fdset)) {
289 ret = copy_in (kc, fd2, fd1);
290 if (ret <= 0)
291 return ret;
297 * Return 0 if the user authenticated on `kc' is allowed to login as
298 * `user'.
301 static int
302 krb5_userok (kx_context *kc, char *user)
304 krb5_error_code ret;
305 char *tmp;
307 ret = krb5_unparse_name (CONTEXT(kc), K5DATA(kc)->client, &tmp);
308 if (ret)
309 krb5_err (CONTEXT(kc), 1, ret, "krb5_unparse_name");
310 kc->user = tmp;
312 return !krb5_kuserok (CONTEXT(kc), K5DATA(kc)->client, user);
316 * Create an instance of an krb5 context.
319 void
320 krb5_make_context (kx_context *kc)
322 krb5_kx_context *c;
323 krb5_error_code ret;
325 kc->authenticate = krb5_authenticate;
326 kc->userok = krb5_userok;
327 kc->read = krb5_read;
328 kc->write = krb5_write;
329 kc->copy_encrypted = krb5_copy_encrypted;
330 kc->destroy = krb5_destroy;
331 kc->user = NULL;
332 kc->data = malloc(sizeof(krb5_kx_context));
334 if (kc->data == NULL) {
335 syslog (LOG_ERR, "failed to malloc %lu bytes",
336 (unsigned long)sizeof(krb5_kx_context));
337 exit(1);
339 memset (kc->data, 0, sizeof(krb5_kx_context));
340 c = (krb5_kx_context *)kc->data;
341 ret = krb5_init_context (&c->context);
342 if (ret) {
343 syslog (LOG_ERR, "failed initialise krb5 context");
344 exit(1);
349 * Receive authentication information on `sock' (first four bytes
350 * in `buf').
354 recv_v5_auth (kx_context *kc, int sock, u_char *buf)
356 uint32_t len;
357 krb5_error_code ret;
358 krb5_principal server;
359 krb5_auth_context auth_context = NULL;
360 krb5_ticket *ticket;
362 if (memcmp (buf, "\x00\x00\x00\x13", 4) != 0)
363 return 1;
364 len = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | (buf[3]);
365 if (net_read(sock, buf, len) != len) {
366 syslog (LOG_ERR, "read: %m");
367 exit (1);
369 if (len != sizeof(KRB5_SENDAUTH_VERSION)
370 || memcmp (buf, KRB5_SENDAUTH_VERSION, len) != 0) {
371 syslog (LOG_ERR, "bad sendauth version: %.8s", buf);
372 exit (1);
375 krb5_make_context (kc);
376 krb5_openlog(CONTEXT(kc), "kxd", &K5DATA(kc)->log);
377 krb5_set_warn_dest(CONTEXT(kc), K5DATA(kc)->log);
379 ret = krb5_sock_to_principal (CONTEXT(kc), sock, "host",
380 KRB5_NT_SRV_HST, &server);
381 if (ret) {
382 syslog (LOG_ERR, "krb5_sock_to_principal: %s",
383 krb5_get_err_text (CONTEXT(kc), ret));
384 exit (1);
387 ret = krb5_recvauth (CONTEXT(kc),
388 &auth_context,
389 &sock,
390 KX_VERSION,
391 server,
392 KRB5_RECVAUTH_IGNORE_VERSION,
393 NULL,
394 &ticket);
395 krb5_free_principal (CONTEXT(kc), server);
396 if (ret) {
397 syslog (LOG_ERR, "krb5_sock_to_principal: %s",
398 krb5_get_err_text (CONTEXT(kc), ret));
399 exit (1);
402 ret = krb5_auth_con_getkey (CONTEXT(kc), auth_context, &K5DATA(kc)->keyblock);
403 if (ret) {
404 syslog (LOG_ERR, "krb5_auth_con_getkey: %s",
405 krb5_get_err_text (CONTEXT(kc), ret));
406 exit (1);
409 ret = krb5_crypto_init (CONTEXT(kc), K5DATA(kc)->keyblock, 0, &K5DATA(kc)->crypto);
410 if (ret) {
411 syslog (LOG_ERR, "krb5_crypto_init: %s",
412 krb5_get_err_text (CONTEXT(kc), ret));
413 exit (1);
416 K5DATA(kc)->client = ticket->client;
417 ticket->client = NULL;
418 krb5_free_ticket (CONTEXT(kc), ticket);
420 return 0;
423 #endif /* KRB5 */