2 * Copyright (c) 1997 - 2006 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 #include <krb5-v4compat.h>
44 return ((x
<< 24) & 0xff000000) |
45 ((x
<< 8) & 0xff0000) |
52 _kdc_maybe_version4(unsigned char *buf
, int len
)
54 return len
> 0 && *buf
== 4;
58 make_err_reply(krb5_context context
, krb5_data
*reply
,
59 int code
, const char *msg
)
61 _krb5_krb_cr_err_reply(context
, "", "", "",
62 kdc_time
, code
, msg
, reply
);
65 struct valid_princ_ctx
{
66 krb5_kdc_configuration
*config
;
71 valid_princ(krb5_context context
,
75 struct valid_princ_ctx
*ctx
= funcctx
;
80 ret
= krb5_unparse_name(context
, princ
, &s
);
83 ret
= _kdc_db_fetch(context
, ctx
->config
, princ
, ctx
->flags
, NULL
, &ent
);
85 kdc_log(context
, ctx
->config
, 7, "Lookup %s failed: %s", s
,
86 krb5_get_err_text (context
, ret
));
90 kdc_log(context
, ctx
->config
, 7, "Lookup %s succeeded", s
);
92 _kdc_free_ent(context
, ent
);
97 _kdc_db_fetch4(krb5_context context
,
98 krb5_kdc_configuration
*config
,
99 const char *name
, const char *instance
, const char *realm
,
105 struct valid_princ_ctx ctx
;
110 ret
= krb5_425_conv_principal_ext2(context
, name
, instance
, realm
,
111 valid_princ
, &ctx
, 0, &p
);
114 ret
= _kdc_db_fetch(context
, config
, p
, flags
, NULL
, ent
);
115 krb5_free_principal(context
, p
);
119 #define RCHECK(X, L) if(X){make_err_reply(context, reply, KFAILURE, "Packet too short"); goto L;}
122 * Process the v4 request in `buf, len' (received from `addr'
123 * (with string `from').
124 * Return an error code and a reply in `reply'.
128 _kdc_do_version4(krb5_context context
,
129 krb5_kdc_configuration
*config
,
134 struct sockaddr_in
*addr
)
138 hdb_entry_ex
*client
= NULL
, *server
= NULL
;
143 char *name
= NULL
, *inst
= NULL
, *realm
= NULL
;
144 char *sname
= NULL
, *sinst
= NULL
;
148 char client_name
[256];
149 char server_name
[256];
151 if(!config
->enable_v4
) {
152 kdc_log(context
, config
, 0,
153 "Rejected version 4 request from %s", from
);
154 make_err_reply(context
, reply
, KRB4ET_KDC_GEN_ERR
,
155 "Function not enabled");
159 sp
= krb5_storage_from_mem(buf
, len
);
160 RCHECK(krb5_ret_int8(sp
, &pvno
), out
);
162 kdc_log(context
, config
, 0,
163 "Protocol version mismatch (krb4) (%d)", pvno
);
164 make_err_reply(context
, reply
, KRB4ET_KDC_PKT_VER
, "protocol mismatch");
167 RCHECK(krb5_ret_int8(sp
, &msg_type
), out
);
171 case AUTH_MSG_KDC_REQUEST
: {
172 krb5_data ticket
, cipher
;
173 krb5_keyblock session
;
175 krb5_data_zero(&ticket
);
176 krb5_data_zero(&cipher
);
178 RCHECK(krb5_ret_stringz(sp
, &name
), out1
);
179 RCHECK(krb5_ret_stringz(sp
, &inst
), out1
);
180 RCHECK(krb5_ret_stringz(sp
, &realm
), out1
);
181 RCHECK(krb5_ret_int32(sp
, &req_time
), out1
);
183 req_time
= swap32(req_time
);
184 RCHECK(krb5_ret_uint8(sp
, &life
), out1
);
185 RCHECK(krb5_ret_stringz(sp
, &sname
), out1
);
186 RCHECK(krb5_ret_stringz(sp
, &sinst
), out1
);
187 snprintf (client_name
, sizeof(client_name
),
188 "%s.%s@%s", name
, inst
, realm
);
189 snprintf (server_name
, sizeof(server_name
),
190 "%s.%s@%s", sname
, sinst
, config
->v4_realm
);
192 kdc_log(context
, config
, 0, "AS-REQ (krb4) %s from %s for %s",
193 client_name
, from
, server_name
);
195 ret
= _kdc_db_fetch4(context
, config
, name
, inst
, realm
,
196 HDB_F_GET_CLIENT
, &client
);
198 kdc_log(context
, config
, 0, "Client not found in database: %s: %s",
199 client_name
, krb5_get_err_text(context
, ret
));
200 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
201 "principal unknown");
204 ret
= _kdc_db_fetch4(context
, config
, sname
, sinst
, config
->v4_realm
,
205 HDB_F_GET_SERVER
, &server
);
207 kdc_log(context
, config
, 0, "Server not found in database: %s: %s",
208 server_name
, krb5_get_err_text(context
, ret
));
209 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
210 "principal unknown");
214 ret
= _kdc_check_flags (context
, config
,
219 /* good error code? */
220 make_err_reply(context
, reply
, KRB4ET_KDC_NAME_EXP
,
221 "operation not allowed");
225 if (config
->enable_v4_per_principal
&&
226 client
->entry
.flags
.allow_kerberos4
== 0)
228 kdc_log(context
, config
, 0,
229 "Per principal Kerberos 4 flag not turned on for %s",
231 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
232 "allow kerberos4 flag required");
237 * There's no way to do pre-authentication in v4 and thus no
238 * good error code to return if preauthentication is required.
241 if (config
->require_preauth
242 || client
->entry
.flags
.require_preauth
243 || server
->entry
.flags
.require_preauth
) {
244 kdc_log(context
, config
, 0,
245 "Pre-authentication required for v4-request: "
247 client_name
, server_name
);
248 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
253 ret
= _kdc_get_des_key(context
, client
, FALSE
, FALSE
, &ckey
);
255 kdc_log(context
, config
, 0, "no suitable DES key for client");
256 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
257 "no suitable DES key for client");
262 /* this is not necessary with the new code in libkrb */
263 /* find a properly salted key */
264 while(ckey
->salt
== NULL
|| ckey
->salt
->salt
.length
!= 0)
265 ret
= hdb_next_keytype2key(context
, &client
->entry
, KEYTYPE_DES
, &ckey
);
267 kdc_log(context
, config
, 0, "No version-4 salted key in database -- %s.%s@%s",
269 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
270 "No version-4 salted key in database");
275 ret
= _kdc_get_des_key(context
, server
, TRUE
, FALSE
, &skey
);
277 kdc_log(context
, config
, 0, "no suitable DES key for server");
278 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
279 "no suitable DES key for server");
283 max_life
= _krb5_krb_life_to_time(0, life
);
284 if(client
->entry
.max_life
)
285 max_life
= min(max_life
, *client
->entry
.max_life
);
286 if(server
->entry
.max_life
)
287 max_life
= min(max_life
, *server
->entry
.max_life
);
289 life
= krb_time_to_life(kdc_time
, kdc_time
+ max_life
);
291 ret
= krb5_generate_random_keyblock(context
,
295 make_err_reply(context
, reply
, KFAILURE
,
296 "Not enough random i KDC");
300 ret
= _krb5_krb_create_ticket(context
,
305 addr
->sin_addr
.s_addr
,
314 krb5_free_keyblock_contents(context
, &session
);
315 make_err_reply(context
, reply
, KFAILURE
,
316 "failed to create v4 ticket");
320 ret
= _krb5_krb_create_ciph(context
,
326 server
->entry
.kvno
% 255,
331 krb5_free_keyblock_contents(context
, &session
);
332 krb5_data_free(&ticket
);
334 make_err_reply(context
, reply
, KFAILURE
,
335 "Failed to create v4 cipher");
339 ret
= _krb5_krb_create_auth_reply(context
,
345 client
->entry
.pw_end
? *client
->entry
.pw_end
: 0,
346 client
->entry
.kvno
% 256,
349 krb5_data_free(&cipher
);
354 case AUTH_MSG_APPL_REQUEST
: {
355 struct _krb5_krb_auth_data ad
;
362 krb5_principal tgt_princ
= NULL
;
363 hdb_entry_ex
*tgt
= NULL
;
365 time_t max_end
, actual_end
, issue_time
;
367 memset(&ad
, 0, sizeof(ad
));
368 krb5_data_zero(&auth
);
370 RCHECK(krb5_ret_int8(sp
, &kvno
), out2
);
371 RCHECK(krb5_ret_stringz(sp
, &realm
), out2
);
373 ret
= krb5_425_conv_principal(context
, "krbtgt", realm
,
377 kdc_log(context
, config
, 0,
378 "Converting krbtgt principal (krb4): %s",
379 krb5_get_err_text(context
, ret
));
380 make_err_reply(context
, reply
, KFAILURE
,
381 "Failed to convert v4 principal (krbtgt)");
385 ret
= _kdc_db_fetch(context
, config
, tgt_princ
,
386 HDB_F_GET_KRBTGT
, NULL
, &tgt
);
389 s
= kdc_log_msg(context
, config
, 0, "Ticket-granting ticket not "
390 "found in database (krb4): krbtgt.%s@%s: %s",
391 realm
, config
->v4_realm
,
392 krb5_get_err_text(context
, ret
));
393 make_err_reply(context
, reply
, KFAILURE
, s
);
398 if(tgt
->entry
.kvno
% 256 != kvno
){
399 kdc_log(context
, config
, 0,
400 "tgs-req (krb4) with old kvno %d (current %d) for "
401 "krbtgt.%s@%s", kvno
, tgt
->entry
.kvno
% 256,
402 realm
, config
->v4_realm
);
403 make_err_reply(context
, reply
, KRB4ET_KDC_AUTH_EXP
,
404 "old krbtgt kvno used");
408 ret
= _kdc_get_des_key(context
, tgt
, TRUE
, FALSE
, &tkey
);
410 kdc_log(context
, config
, 0,
411 "no suitable DES key for krbtgt (krb4)");
412 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
413 "no suitable DES key for krbtgt");
417 RCHECK(krb5_ret_int8(sp
, &ticket_len
), out2
);
418 RCHECK(krb5_ret_int8(sp
, &req_len
), out2
);
420 pos
= krb5_storage_seek(sp
, ticket_len
+ req_len
, SEEK_CUR
);
425 if (config
->check_ticket_addresses
)
426 address
= addr
->sin_addr
.s_addr
;
430 ret
= _krb5_krb_rd_req(context
, &auth
, "krbtgt", realm
,
432 address
, &tkey
->key
, &ad
);
434 kdc_log(context
, config
, 0, "krb_rd_req: %d", ret
);
435 make_err_reply(context
, reply
, ret
, "failed to parse request");
439 RCHECK(krb5_ret_int32(sp
, &req_time
), out2
);
441 req_time
= swap32(req_time
);
442 RCHECK(krb5_ret_uint8(sp
, &life
), out2
);
443 RCHECK(krb5_ret_stringz(sp
, &sname
), out2
);
444 RCHECK(krb5_ret_stringz(sp
, &sinst
), out2
);
445 snprintf (server_name
, sizeof(server_name
),
447 sname
, sinst
, config
->v4_realm
);
448 snprintf (client_name
, sizeof(client_name
),
450 ad
.pname
, ad
.pinst
, ad
.prealm
);
452 kdc_log(context
, config
, 0, "TGS-REQ (krb4) %s from %s for %s",
453 client_name
, from
, server_name
);
455 if(strcmp(ad
.prealm
, realm
)){
456 kdc_log(context
, config
, 0,
457 "Can't hop realms (krb4) %s -> %s", realm
, ad
.prealm
);
458 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
463 if (!config
->enable_v4_cross_realm
&& strcmp(realm
, config
->v4_realm
) != 0) {
464 kdc_log(context
, config
, 0,
465 "krb4 Cross-realm %s -> %s disabled",
466 realm
, config
->v4_realm
);
467 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
472 if(strcmp(sname
, "changepw") == 0){
473 kdc_log(context
, config
, 0,
474 "Bad request for changepw ticket (krb4)");
475 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
476 "Can't authorize password change based on TGT");
480 ret
= _kdc_db_fetch4(context
, config
, ad
.pname
, ad
.pinst
, ad
.prealm
,
481 HDB_F_GET_CLIENT
, &client
);
482 if(ret
&& ret
!= HDB_ERR_NOENTRY
) {
484 s
= kdc_log_msg(context
, config
, 0,
485 "Client not found in database: (krb4) %s: %s",
486 client_name
, krb5_get_err_text(context
, ret
));
487 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
491 if (client
== NULL
&& strcmp(ad
.prealm
, config
->v4_realm
) == 0) {
493 s
= kdc_log_msg(context
, config
, 0,
494 "Local client not found in database: (krb4) "
496 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
501 ret
= _kdc_db_fetch4(context
, config
, sname
, sinst
, config
->v4_realm
,
502 HDB_F_GET_SERVER
, &server
);
505 s
= kdc_log_msg(context
, config
, 0,
506 "Server not found in database (krb4): %s: %s",
507 server_name
, krb5_get_err_text(context
, ret
));
508 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
513 ret
= _kdc_check_flags (context
, config
,
518 make_err_reply(context
, reply
, KRB4ET_KDC_NAME_EXP
,
519 "operation not allowed");
523 ret
= _kdc_get_des_key(context
, server
, TRUE
, FALSE
, &skey
);
525 kdc_log(context
, config
, 0,
526 "no suitable DES key for server (krb4)");
527 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
528 "no suitable DES key for server");
532 max_end
= _krb5_krb_life_to_time(ad
.time_sec
, ad
.life
);
533 max_end
= min(max_end
, _krb5_krb_life_to_time(kdc_time
, life
));
534 if(server
->entry
.max_life
)
535 max_end
= min(max_end
, kdc_time
+ *server
->entry
.max_life
);
536 if(client
&& client
->entry
.max_life
)
537 max_end
= min(max_end
, kdc_time
+ *client
->entry
.max_life
);
538 life
= min(life
, krb_time_to_life(kdc_time
, max_end
));
540 issue_time
= kdc_time
;
541 actual_end
= _krb5_krb_life_to_time(issue_time
, life
);
542 while (actual_end
> max_end
&& life
> 1) {
543 /* move them into the next earlier lifetime bracket */
545 actual_end
= _krb5_krb_life_to_time(issue_time
, life
);
547 if (actual_end
> max_end
) {
548 /* if life <= 1 and it's still too long, backdate the ticket */
549 issue_time
-= actual_end
- max_end
;
553 krb5_data ticket
, cipher
;
554 krb5_keyblock session
;
556 krb5_data_zero(&ticket
);
557 krb5_data_zero(&cipher
);
559 ret
= krb5_generate_random_keyblock(context
,
563 make_err_reply(context
, reply
, KFAILURE
,
564 "Not enough random i KDC");
568 ret
= _krb5_krb_create_ticket(context
,
573 addr
->sin_addr
.s_addr
,
582 krb5_free_keyblock_contents(context
, &session
);
583 make_err_reply(context
, reply
, KFAILURE
,
584 "failed to create v4 ticket");
588 ret
= _krb5_krb_create_ciph(context
,
594 server
->entry
.kvno
% 255,
599 krb5_free_keyblock_contents(context
, &session
);
601 make_err_reply(context
, reply
, KFAILURE
,
602 "failed to create v4 cipher");
606 ret
= _krb5_krb_create_auth_reply(context
,
616 krb5_data_free(&cipher
);
619 _krb5_krb_free_auth_data(context
, &ad
);
621 krb5_free_principal(context
, tgt_princ
);
623 _kdc_free_ent(context
, tgt
);
626 case AUTH_MSG_ERR_REPLY
:
629 kdc_log(context
, config
, 0, "Unknown message type (krb4): %d from %s",
632 make_err_reply(context
, reply
, KFAILURE
, "Unknown message type");
646 _kdc_free_ent(context
, client
);
648 _kdc_free_ent(context
, server
);
649 krb5_storage_free(sp
);
654 _kdc_encode_v4_ticket(krb5_context context
,
655 krb5_kdc_configuration
*config
,
656 void *buf
, size_t len
, const EncTicketPart
*et
,
657 const PrincipalName
*service
, size_t *size
)
661 char name
[40], inst
[40], realm
[40];
662 char sname
[40], sinst
[40];
665 krb5_principal princ
;
666 _krb5_principalname2krb5_principal(context
,
670 ret
= krb5_524_conv_principal(context
,
675 krb5_free_principal(context
, princ
);
679 _krb5_principalname2krb5_principal(context
,
684 ret
= krb5_524_conv_principal(context
,
689 krb5_free_principal(context
, princ
);
694 sp
= krb5_storage_emem();
696 krb5_store_int8(sp
, 0); /* flags */
697 krb5_store_stringz(sp
, name
);
698 krb5_store_stringz(sp
, inst
);
699 krb5_store_stringz(sp
, realm
);
701 unsigned char tmp
[4] = { 0, 0, 0, 0 };
704 for(i
= 0; i
< et
->caddr
->len
; i
++)
705 if(et
->caddr
->val
[i
].addr_type
== AF_INET
&&
706 et
->caddr
->val
[i
].address
.length
== 4){
707 memcpy(tmp
, et
->caddr
->val
[i
].address
.data
, 4);
711 krb5_storage_write(sp
, tmp
, sizeof(tmp
));
714 if((et
->key
.keytype
!= ETYPE_DES_CBC_MD5
&&
715 et
->key
.keytype
!= ETYPE_DES_CBC_MD4
&&
716 et
->key
.keytype
!= ETYPE_DES_CBC_CRC
) ||
717 et
->key
.keyvalue
.length
!= 8)
719 krb5_storage_write(sp
, et
->key
.keyvalue
.data
, 8);
722 time_t start
= et
->starttime
? *et
->starttime
: et
->authtime
;
723 krb5_store_int8(sp
, krb_time_to_life(start
, et
->endtime
));
724 krb5_store_int32(sp
, start
);
727 krb5_store_stringz(sp
, sname
);
728 krb5_store_stringz(sp
, sinst
);
732 krb5_storage_to_data(sp
, &data
);
733 krb5_storage_free(sp
);
734 *size
= (data
.length
+ 7) & ~7; /* pad to 8 bytes */
737 memset((unsigned char*)buf
- *size
+ 1, 0, *size
);
738 memcpy((unsigned char*)buf
- *size
+ 1, data
.data
, data
.length
);
739 krb5_data_free(&data
);
745 _kdc_get_des_key(krb5_context context
,
746 hdb_entry_ex
*principal
, krb5_boolean is_server
,
747 krb5_boolean prefer_afs_key
, Key
**ret_key
)
749 Key
*v5_key
= NULL
, *v4_key
= NULL
, *afs_key
= NULL
, *server_key
= NULL
;
751 krb5_enctype etypes
[] = { ETYPE_DES_CBC_MD5
,
756 i
< sizeof(etypes
)/sizeof(etypes
[0])
757 && (v5_key
== NULL
|| v4_key
== NULL
||
758 afs_key
== NULL
|| server_key
== NULL
);
761 while(hdb_next_enctype2key(context
, &principal
->entry
, etypes
[i
], &key
) == 0) {
762 if(key
->salt
== NULL
) {
765 } else if(key
->salt
->type
== hdb_pw_salt
&&
766 key
->salt
->salt
.length
== 0) {
769 } else if(key
->salt
->type
== hdb_afs3_salt
) {
772 } else if(server_key
== NULL
)
784 else if(is_server
&& server_key
)
785 *ret_key
= server_key
;
787 return KRB4ET_KDC_NULL_KEY
;
795 else if(is_server
&& server_key
)
796 *ret_key
= server_key
;
798 return KRB4ET_KDC_NULL_KEY
;
801 if((*ret_key
)->key
.keyvalue
.length
== 0)
802 return KRB4ET_KDC_NULL_KEY
;