2 * Copyright (c) 1995 - 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 __RCSID("$Heimdal: krb5.c 17450 2006-05-05 11:11:43Z lha $"
41 struct krb5_kx_context
{
43 krb5_keyblock
*keyblock
;
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'.
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
);
69 krb5_free_context (CONTEXT(kc
));
70 memset (kc
->data
, 0, sizeof(krb5_kx_context
));
75 * Read the authentication information from `s' and return 0 if
80 krb5_authenticate (kx_context
*kc
, int s
)
82 krb5_auth_context auth_context
= NULL
;
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
);
90 krb5_warn (CONTEXT(kc
), ret
, "krb5_sname_to_principal: %s", host
);
94 ret
= krb5_sendauth (CONTEXT(kc
),
100 AP_OPTS_MUTUAL_REQUIRED
| AP_OPTS_USE_SUBKEY
,
108 if(ret
!= KRB5_SENDAUTH_BADRESPONSE
)
109 krb5_warn (CONTEXT(kc
), ret
, "krb5_sendauth: %s", host
);
113 ret
= krb5_auth_con_getkey (CONTEXT(kc
), auth_context
,
114 &K5DATA(kc
)->keyblock
);
116 krb5_warn (CONTEXT(kc
), ret
, "krb5_auth_con_getkey: %s", host
);
117 krb5_auth_con_free (CONTEXT(kc
), auth_context
);
121 ret
= krb5_crypto_init (CONTEXT(kc
), K5DATA(kc
)->keyblock
,
122 0, &K5DATA(kc
)->crypto
);
124 krb5_warn (CONTEXT(kc
), ret
, "krb5_crypto_init");
125 krb5_auth_con_free (CONTEXT(kc
), auth_context
);
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
138 krb5_read (kx_context
*kc
,
139 int fd
, void *buf
, size_t len
)
141 size_t data_len
, outer_len
;
143 unsigned char tmp
[4];
147 l
= krb5_net_read (CONTEXT(kc
), &fd
, tmp
, 4);
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
);
157 if (krb5_net_read (CONTEXT(kc
), &fd
, buf
, outer_len
) != outer_len
)
160 ret
= krb5_decrypt (CONTEXT(kc
), K5DATA(kc
)->crypto
,
161 KRB5_KU_OTHER_ENCRYPTED
,
162 buf
, outer_len
, &data
);
164 krb5_warn (CONTEXT(kc
), ret
, "krb5_decrypt");
167 if (data_len
> data
.length
) {
168 krb5_data_free (&data
);
171 memmove (buf
, data
.data
, data_len
);
172 krb5_data_free (&data
);
177 * Write an encapsulated krb5 packet on `fd' with the data in `buf,
178 * len'. Return len or -1 on error.
182 krb5_write(kx_context
*kc
,
183 int fd
, const void *buf
, size_t len
)
187 unsigned char tmp
[4];
190 ret
= krb5_encrypt (CONTEXT(kc
), K5DATA(kc
)->crypto
,
191 KRB5_KU_OTHER_ENCRYPTED
,
194 krb5_warn (CONTEXT(kc
), ret
, "krb5_write");
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
);
209 krb5_data_free (&data
);
214 * Copy from the unix socket `from_fd' encrypting to `to_fd'.
215 * Return 0, -1 or len.
219 copy_out (kx_context
*kc
, int from_fd
, int to_fd
)
224 len
= read (from_fd
, buf
, sizeof(buf
));
228 krb5_warn (CONTEXT(kc
), errno
, "read");
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.
240 copy_in (kx_context
*kc
, int from_fd
, int to_fd
)
242 char buf
[33000]; /* XXX */
246 len
= krb5_read (kc
, from_fd
, buf
, sizeof(buf
));
250 krb5_warn (CONTEXT(kc
), errno
, "krb5_read");
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.
263 krb5_copy_encrypted (kx_context
*kc
, int fd1
, int fd2
)
269 if (fd1
>= FD_SETSIZE
|| fd2
>= FD_SETSIZE
) {
270 krb5_warnx (CONTEXT(kc
), "fd too large");
278 ret
= select (max(fd1
, fd2
)+1, &fdset
, NULL
, NULL
, NULL
);
279 if (ret
< 0 && errno
!= EINTR
) {
280 krb5_warn (CONTEXT(kc
), errno
, "select");
283 if (FD_ISSET(fd1
, &fdset
)) {
284 ret
= copy_out (kc
, fd1
, fd2
);
288 if (FD_ISSET(fd2
, &fdset
)) {
289 ret
= copy_in (kc
, fd2
, fd1
);
297 * Return 0 if the user authenticated on `kc' is allowed to login as
302 krb5_userok (kx_context
*kc
, char *user
)
307 ret
= krb5_unparse_name (CONTEXT(kc
), K5DATA(kc
)->client
, &tmp
);
309 krb5_err (CONTEXT(kc
), 1, ret
, "krb5_unparse_name");
312 return !krb5_kuserok (CONTEXT(kc
), K5DATA(kc
)->client
, user
);
316 * Create an instance of an krb5 context.
320 krb5_make_context (kx_context
*kc
)
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
;
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
));
339 memset (kc
->data
, 0, sizeof(krb5_kx_context
));
340 c
= (krb5_kx_context
*)kc
->data
;
341 ret
= krb5_init_context (&c
->context
);
343 syslog (LOG_ERR
, "failed initialise krb5 context");
349 * Receive authentication information on `sock' (first four bytes
354 recv_v5_auth (kx_context
*kc
, int sock
, u_char
*buf
)
358 krb5_principal server
;
359 krb5_auth_context auth_context
= NULL
;
362 if (memcmp (buf
, "\x00\x00\x00\x13", 4) != 0)
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");
369 if (len
!= sizeof(KRB5_SENDAUTH_VERSION
)
370 || memcmp (buf
, KRB5_SENDAUTH_VERSION
, len
) != 0) {
371 syslog (LOG_ERR
, "bad sendauth version: %.8s", buf
);
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
);
382 syslog (LOG_ERR
, "krb5_sock_to_principal: %s",
383 krb5_get_err_text (CONTEXT(kc
), ret
));
387 ret
= krb5_recvauth (CONTEXT(kc
),
392 KRB5_RECVAUTH_IGNORE_VERSION
,
395 krb5_free_principal (CONTEXT(kc
), server
);
397 syslog (LOG_ERR
, "krb5_sock_to_principal: %s",
398 krb5_get_err_text (CONTEXT(kc
), ret
));
402 ret
= krb5_auth_con_getkey (CONTEXT(kc
), auth_context
, &K5DATA(kc
)->keyblock
);
404 syslog (LOG_ERR
, "krb5_auth_con_getkey: %s",
405 krb5_get_err_text (CONTEXT(kc
), ret
));
409 ret
= krb5_crypto_init (CONTEXT(kc
), K5DATA(kc
)->keyblock
, 0, &K5DATA(kc
)->crypto
);
411 syslog (LOG_ERR
, "krb5_crypto_init: %s",
412 krb5_get_err_text (CONTEXT(kc
), ret
));
416 K5DATA(kc
)->client
= ticket
->client
;
417 ticket
->client
= NULL
;
418 krb5_free_ticket (CONTEXT(kc
), ticket
);