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
34 #define KRB5_DEPRECATED /* uses v4 functions that will die */
40 #include <krb5-v4compat.h>
46 return ((x
<< 24) & 0xff000000) |
47 ((x
<< 8) & 0xff0000) |
54 _kdc_maybe_version4(unsigned char *buf
, int len
)
56 return len
> 0 && *buf
== 4;
60 make_err_reply(krb5_context context
, krb5_data
*reply
,
61 int code
, const char *msg
)
63 _krb5_krb_cr_err_reply(context
, "", "", "",
64 kdc_time
, code
, msg
, reply
);
67 struct valid_princ_ctx
{
68 krb5_kdc_configuration
*config
;
73 valid_princ(krb5_context context
,
77 struct valid_princ_ctx
*ctx
= funcctx
;
82 ret
= krb5_unparse_name(context
, princ
, &s
);
85 ret
= _kdc_db_fetch(context
, ctx
->config
, princ
, ctx
->flags
, NULL
, &ent
);
87 kdc_log(context
, ctx
->config
, 7, "Lookup %s failed: %s", s
,
88 krb5_get_err_text (context
, ret
));
92 kdc_log(context
, ctx
->config
, 7, "Lookup %s succeeded", s
);
94 _kdc_free_ent(context
, ent
);
99 _kdc_db_fetch4(krb5_context context
,
100 krb5_kdc_configuration
*config
,
101 const char *name
, const char *instance
, const char *realm
,
107 struct valid_princ_ctx ctx
;
112 ret
= krb5_425_conv_principal_ext2(context
, name
, instance
, realm
,
113 valid_princ
, &ctx
, 0, &p
);
116 ret
= _kdc_db_fetch(context
, config
, p
, flags
, NULL
, ent
);
117 krb5_free_principal(context
, p
);
121 #define RCHECK(X, L) if(X){make_err_reply(context, reply, KFAILURE, "Packet too short"); goto L;}
124 * Process the v4 request in `buf, len' (received from `addr'
125 * (with string `from').
126 * Return an error code and a reply in `reply'.
130 _kdc_do_version4(krb5_context context
,
131 krb5_kdc_configuration
*config
,
136 struct sockaddr_in
*addr
)
139 krb5_error_code ret
= EINVAL
;
140 hdb_entry_ex
*client
= NULL
, *server
= NULL
;
145 char *name
= NULL
, *inst
= NULL
, *realm
= NULL
;
146 char *sname
= NULL
, *sinst
= NULL
;
150 char client_name
[256];
151 char server_name
[256];
153 if(!config
->enable_v4
) {
154 kdc_log(context
, config
, 0,
155 "Rejected version 4 request from %s", from
);
156 make_err_reply(context
, reply
, KRB4ET_KDC_GEN_ERR
,
157 "Function not enabled");
161 sp
= krb5_storage_from_mem(buf
, len
);
162 RCHECK(krb5_ret_int8(sp
, &pvno
), out
);
164 kdc_log(context
, config
, 0,
165 "Protocol version mismatch (krb4) (%d)", pvno
);
166 make_err_reply(context
, reply
, KRB4ET_KDC_PKT_VER
, "protocol mismatch");
167 ret
= KRB4ET_KDC_PKT_VER
;
170 RCHECK(krb5_ret_int8(sp
, &msg_type
), out
);
174 case AUTH_MSG_KDC_REQUEST
: {
175 krb5_data ticket
, cipher
;
176 krb5_keyblock session
;
178 krb5_data_zero(&ticket
);
179 krb5_data_zero(&cipher
);
181 RCHECK(krb5_ret_stringz(sp
, &name
), out1
);
182 RCHECK(krb5_ret_stringz(sp
, &inst
), out1
);
183 RCHECK(krb5_ret_stringz(sp
, &realm
), out1
);
184 RCHECK(krb5_ret_int32(sp
, &req_time
), out1
);
186 req_time
= swap32(req_time
);
187 RCHECK(krb5_ret_uint8(sp
, &life
), out1
);
188 RCHECK(krb5_ret_stringz(sp
, &sname
), out1
);
189 RCHECK(krb5_ret_stringz(sp
, &sinst
), out1
);
190 snprintf (client_name
, sizeof(client_name
),
191 "%s.%s@%s", name
, inst
, realm
);
192 snprintf (server_name
, sizeof(server_name
),
193 "%s.%s@%s", sname
, sinst
, config
->v4_realm
);
195 kdc_log(context
, config
, 0, "AS-REQ (krb4) %s from %s for %s",
196 client_name
, from
, server_name
);
198 ret
= _kdc_db_fetch4(context
, config
, name
, inst
, realm
,
199 HDB_F_GET_CLIENT
, &client
);
201 kdc_log(context
, config
, 0, "Client not found in database: %s: %s",
202 client_name
, krb5_get_err_text(context
, ret
));
203 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
204 "principal unknown");
207 ret
= _kdc_db_fetch4(context
, config
, sname
, sinst
, config
->v4_realm
,
208 HDB_F_GET_SERVER
, &server
);
210 kdc_log(context
, config
, 0, "Server not found in database: %s: %s",
211 server_name
, krb5_get_err_text(context
, ret
));
212 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
213 "principal unknown");
217 ret
= kdc_check_flags (context
, config
,
222 /* good error code? */
223 make_err_reply(context
, reply
, KRB4ET_KDC_NAME_EXP
,
224 "operation not allowed");
228 if (config
->enable_v4_per_principal
&&
229 client
->entry
.flags
.allow_kerberos4
== 0)
231 kdc_log(context
, config
, 0,
232 "Per principal Kerberos 4 flag not turned on for %s",
234 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
235 "allow kerberos4 flag required");
240 * There's no way to do pre-authentication in v4 and thus no
241 * good error code to return if preauthentication is required.
244 if (config
->require_preauth
245 || client
->entry
.flags
.require_preauth
246 || server
->entry
.flags
.require_preauth
) {
247 kdc_log(context
, config
, 0,
248 "Pre-authentication required for v4-request: "
250 client_name
, server_name
);
251 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
256 ret
= _kdc_get_des_key(context
, client
, FALSE
, FALSE
, &ckey
);
258 kdc_log(context
, config
, 0, "no suitable DES key for client");
259 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
260 "no suitable DES key for client");
264 ret
= _kdc_get_des_key(context
, server
, TRUE
, FALSE
, &skey
);
266 kdc_log(context
, config
, 0, "no suitable DES key for server");
267 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
268 "no suitable DES key for server");
272 max_life
= _krb5_krb_life_to_time(0, life
);
273 if(client
->entry
.max_life
)
274 max_life
= min(max_life
, *client
->entry
.max_life
);
275 if(server
->entry
.max_life
)
276 max_life
= min(max_life
, *server
->entry
.max_life
);
278 life
= krb_time_to_life(kdc_time
, kdc_time
+ max_life
);
280 ret
= krb5_generate_random_keyblock(context
,
284 make_err_reply(context
, reply
, KFAILURE
,
285 "Not enough random i KDC");
289 ret
= _krb5_krb_create_ticket(context
,
294 addr
->sin_addr
.s_addr
,
303 krb5_free_keyblock_contents(context
, &session
);
304 make_err_reply(context
, reply
, KFAILURE
,
305 "failed to create v4 ticket");
309 ret
= _krb5_krb_create_ciph(context
,
315 server
->entry
.kvno
% 255,
320 krb5_free_keyblock_contents(context
, &session
);
321 krb5_data_free(&ticket
);
323 make_err_reply(context
, reply
, KFAILURE
,
324 "Failed to create v4 cipher");
328 ret
= _krb5_krb_create_auth_reply(context
,
334 client
->entry
.pw_end
? *client
->entry
.pw_end
: 0,
335 client
->entry
.kvno
% 256,
338 krb5_data_free(&cipher
);
343 case AUTH_MSG_APPL_REQUEST
: {
344 struct _krb5_krb_auth_data ad
;
351 krb5_principal tgt_princ
= NULL
;
352 hdb_entry_ex
*tgt
= NULL
;
354 time_t max_end
, actual_end
, issue_time
;
356 memset(&ad
, 0, sizeof(ad
));
357 krb5_data_zero(&auth
);
359 RCHECK(krb5_ret_int8(sp
, &kvno
), out2
);
360 RCHECK(krb5_ret_stringz(sp
, &realm
), out2
);
362 ret
= krb5_425_conv_principal(context
, "krbtgt", realm
,
366 kdc_log(context
, config
, 0,
367 "Converting krbtgt principal (krb4): %s",
368 krb5_get_err_text(context
, ret
));
369 make_err_reply(context
, reply
, KFAILURE
,
370 "Failed to convert v4 principal (krbtgt)");
374 ret
= _kdc_db_fetch(context
, config
, tgt_princ
,
375 HDB_F_GET_KRBTGT
, NULL
, &tgt
);
378 s
= kdc_log_msg(context
, config
, 0, "Ticket-granting ticket not "
379 "found in database (krb4): krbtgt.%s@%s: %s",
380 realm
, config
->v4_realm
,
381 krb5_get_err_text(context
, ret
));
382 make_err_reply(context
, reply
, KFAILURE
, s
);
387 if(tgt
->entry
.kvno
% 256 != kvno
){
388 kdc_log(context
, config
, 0,
389 "tgs-req (krb4) with old kvno %d (current %d) for "
390 "krbtgt.%s@%s", kvno
, tgt
->entry
.kvno
% 256,
391 realm
, config
->v4_realm
);
392 make_err_reply(context
, reply
, KRB4ET_KDC_AUTH_EXP
,
393 "old krbtgt kvno used");
397 ret
= _kdc_get_des_key(context
, tgt
, TRUE
, FALSE
, &tkey
);
399 kdc_log(context
, config
, 0,
400 "no suitable DES key for krbtgt (krb4)");
401 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
402 "no suitable DES key for krbtgt");
406 RCHECK(krb5_ret_int8(sp
, &ticket_len
), out2
);
407 RCHECK(krb5_ret_int8(sp
, &req_len
), out2
);
409 pos
= krb5_storage_seek(sp
, ticket_len
+ req_len
, SEEK_CUR
);
414 if (config
->check_ticket_addresses
)
415 address
= addr
->sin_addr
.s_addr
;
419 ret
= _krb5_krb_rd_req(context
, &auth
, "krbtgt", realm
,
421 address
, &tkey
->key
, &ad
);
423 kdc_log(context
, config
, 0, "krb_rd_req: %d", ret
);
424 make_err_reply(context
, reply
, ret
, "failed to parse request");
428 RCHECK(krb5_ret_int32(sp
, &req_time
), out2
);
430 req_time
= swap32(req_time
);
431 RCHECK(krb5_ret_uint8(sp
, &life
), out2
);
432 RCHECK(krb5_ret_stringz(sp
, &sname
), out2
);
433 RCHECK(krb5_ret_stringz(sp
, &sinst
), out2
);
434 snprintf (server_name
, sizeof(server_name
),
436 sname
, sinst
, config
->v4_realm
);
437 snprintf (client_name
, sizeof(client_name
),
439 ad
.pname
, ad
.pinst
, ad
.prealm
);
441 kdc_log(context
, config
, 0, "TGS-REQ (krb4) %s from %s for %s",
442 client_name
, from
, server_name
);
444 if(strcmp(ad
.prealm
, realm
)){
445 kdc_log(context
, config
, 0,
446 "Can't hop realms (krb4) %s -> %s", realm
, ad
.prealm
);
447 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
452 if (!config
->enable_v4_cross_realm
&& strcmp(realm
, config
->v4_realm
) != 0) {
453 kdc_log(context
, config
, 0,
454 "krb4 Cross-realm %s -> %s disabled",
455 realm
, config
->v4_realm
);
456 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
461 if(strcmp(sname
, "changepw") == 0){
462 kdc_log(context
, config
, 0,
463 "Bad request for changepw ticket (krb4)");
464 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
,
465 "Can't authorize password change based on TGT");
469 ret
= _kdc_db_fetch4(context
, config
, ad
.pname
, ad
.pinst
, ad
.prealm
,
470 HDB_F_GET_CLIENT
, &client
);
471 if(ret
&& ret
!= HDB_ERR_NOENTRY
) {
473 s
= kdc_log_msg(context
, config
, 0,
474 "Client not found in database: (krb4) %s: %s",
475 client_name
, krb5_get_err_text(context
, ret
));
476 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
480 if (client
== NULL
&& strcmp(ad
.prealm
, config
->v4_realm
) == 0) {
482 s
= kdc_log_msg(context
, config
, 0,
483 "Local client not found in database: (krb4) "
485 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
490 ret
= _kdc_db_fetch4(context
, config
, sname
, sinst
, config
->v4_realm
,
491 HDB_F_GET_SERVER
, &server
);
494 s
= kdc_log_msg(context
, config
, 0,
495 "Server not found in database (krb4): %s: %s",
496 server_name
, krb5_get_err_text(context
, ret
));
497 make_err_reply(context
, reply
, KRB4ET_KDC_PR_UNKNOWN
, s
);
502 ret
= kdc_check_flags (context
, config
,
507 make_err_reply(context
, reply
, KRB4ET_KDC_NAME_EXP
,
508 "operation not allowed");
512 ret
= _kdc_get_des_key(context
, server
, TRUE
, FALSE
, &skey
);
514 kdc_log(context
, config
, 0,
515 "no suitable DES key for server (krb4)");
516 make_err_reply(context
, reply
, KRB4ET_KDC_NULL_KEY
,
517 "no suitable DES key for server");
521 max_end
= _krb5_krb_life_to_time(ad
.time_sec
, ad
.life
);
522 max_end
= min(max_end
, _krb5_krb_life_to_time(kdc_time
, life
));
523 if(server
->entry
.max_life
)
524 max_end
= min(max_end
, kdc_time
+ *server
->entry
.max_life
);
525 if(client
&& client
->entry
.max_life
)
526 max_end
= min(max_end
, kdc_time
+ *client
->entry
.max_life
);
527 life
= min(life
, krb_time_to_life(kdc_time
, max_end
));
529 issue_time
= kdc_time
;
530 actual_end
= _krb5_krb_life_to_time(issue_time
, life
);
531 while (actual_end
> max_end
&& life
> 1) {
532 /* move them into the next earlier lifetime bracket */
534 actual_end
= _krb5_krb_life_to_time(issue_time
, life
);
536 if (actual_end
> max_end
) {
537 /* if life <= 1 and it's still too long, backdate the ticket */
538 issue_time
-= actual_end
- max_end
;
542 krb5_data ticket
, cipher
;
543 krb5_keyblock session
;
545 krb5_data_zero(&ticket
);
546 krb5_data_zero(&cipher
);
548 ret
= krb5_generate_random_keyblock(context
,
552 make_err_reply(context
, reply
, KFAILURE
,
553 "Not enough random i KDC");
557 ret
= _krb5_krb_create_ticket(context
,
562 addr
->sin_addr
.s_addr
,
571 krb5_free_keyblock_contents(context
, &session
);
572 make_err_reply(context
, reply
, KFAILURE
,
573 "failed to create v4 ticket");
577 ret
= _krb5_krb_create_ciph(context
,
583 server
->entry
.kvno
% 255,
588 krb5_free_keyblock_contents(context
, &session
);
590 make_err_reply(context
, reply
, KFAILURE
,
591 "failed to create v4 cipher");
595 ret
= _krb5_krb_create_auth_reply(context
,
605 krb5_data_free(&cipher
);
608 _krb5_krb_free_auth_data(context
, &ad
);
610 krb5_free_principal(context
, tgt_princ
);
612 _kdc_free_ent(context
, tgt
);
615 case AUTH_MSG_ERR_REPLY
:
619 kdc_log(context
, config
, 0, "Unknown message type (krb4): %d from %s",
622 make_err_reply(context
, reply
, KFAILURE
, "Unknown message type");
637 _kdc_free_ent(context
, client
);
639 _kdc_free_ent(context
, server
);
640 krb5_storage_free(sp
);
645 _kdc_encode_v4_ticket(krb5_context context
,
646 krb5_kdc_configuration
*config
,
647 void *buf
, size_t len
, const EncTicketPart
*et
,
648 const PrincipalName
*service
, size_t *size
)
652 char name
[40], inst
[40], realm
[40];
653 char sname
[40], sinst
[40];
656 krb5_principal princ
;
657 _krb5_principalname2krb5_principal(context
,
661 ret
= krb5_524_conv_principal(context
,
666 krb5_free_principal(context
, princ
);
670 _krb5_principalname2krb5_principal(context
,
675 ret
= krb5_524_conv_principal(context
,
680 krb5_free_principal(context
, princ
);
685 sp
= krb5_storage_emem();
687 krb5_store_int8(sp
, 0); /* flags */
688 krb5_store_stringz(sp
, name
);
689 krb5_store_stringz(sp
, inst
);
690 krb5_store_stringz(sp
, realm
);
692 unsigned char tmp
[4] = { 0, 0, 0, 0 };
695 for(i
= 0; i
< et
->caddr
->len
; i
++)
696 if(et
->caddr
->val
[i
].addr_type
== AF_INET
&&
697 et
->caddr
->val
[i
].address
.length
== 4){
698 memcpy(tmp
, et
->caddr
->val
[i
].address
.data
, 4);
702 krb5_storage_write(sp
, tmp
, sizeof(tmp
));
705 if((et
->key
.keytype
!= ETYPE_DES_CBC_MD5
&&
706 et
->key
.keytype
!= ETYPE_DES_CBC_MD4
&&
707 et
->key
.keytype
!= ETYPE_DES_CBC_CRC
) ||
708 et
->key
.keyvalue
.length
!= 8)
710 krb5_storage_write(sp
, et
->key
.keyvalue
.data
, 8);
713 time_t start
= et
->starttime
? *et
->starttime
: et
->authtime
;
714 krb5_store_int8(sp
, krb_time_to_life(start
, et
->endtime
));
715 krb5_store_int32(sp
, start
);
718 krb5_store_stringz(sp
, sname
);
719 krb5_store_stringz(sp
, sinst
);
723 krb5_storage_to_data(sp
, &data
);
724 krb5_storage_free(sp
);
725 *size
= (data
.length
+ 7) & ~7; /* pad to 8 bytes */
728 memset((unsigned char*)buf
- *size
+ 1, 0, *size
);
729 memcpy((unsigned char*)buf
- *size
+ 1, data
.data
, data
.length
);
730 krb5_data_free(&data
);
736 _kdc_get_des_key(krb5_context context
,
737 hdb_entry_ex
*principal
, krb5_boolean is_server
,
738 krb5_boolean prefer_afs_key
, Key
**ret_key
)
740 Key
*v5_key
= NULL
, *v4_key
= NULL
, *afs_key
= NULL
, *server_key
= NULL
;
742 krb5_enctype etypes
[] = { ETYPE_DES_CBC_MD5
,
747 i
< sizeof(etypes
)/sizeof(etypes
[0])
748 && (v5_key
== NULL
|| v4_key
== NULL
||
749 afs_key
== NULL
|| server_key
== NULL
);
752 while(hdb_next_enctype2key(context
, &principal
->entry
, etypes
[i
], &key
) == 0) {
753 if(key
->salt
== NULL
) {
756 } else if(key
->salt
->type
== hdb_pw_salt
&&
757 key
->salt
->salt
.length
== 0) {
760 } else if(key
->salt
->type
== hdb_afs3_salt
) {
763 } else if(server_key
== NULL
)
775 else if(is_server
&& server_key
)
776 *ret_key
= server_key
;
778 return KRB4ET_KDC_NULL_KEY
;
786 else if(is_server
&& server_key
)
787 *ret_key
= server_key
;
789 return KRB4ET_KDC_NULL_KEY
;
792 if((*ret_key
)->key
.keyvalue
.length
== 0)
793 return KRB4ET_KDC_NULL_KEY
;