2 * Copyright (c) 1997 - 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 #include <krb5-v4compat.h>
38 RCSID("$Id: kerberos4.c,v 1.54 2005/06/30 01:51:43 lha Exp $");
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);
66 valid_princ(krb5_context context,
70 krb5_kdc_configuration *config = funcctx;
75 ret = krb5_unparse_name(context, princ, &s);
78 ret = _kdc_db_fetch(context, config, princ, HDB_ENT_TYPE_ANY, &ent);
80 kdc_log(context, config, 7, "Lookup %s failed: %s", s,
81 krb5_get_err_text (context, ret));
85 kdc_log(context, config, 7, "Lookup %s succeeded", s);
87 _kdc_free_ent(context, ent);
92 _kdc_db_fetch4(krb5_context context,
93 krb5_kdc_configuration *config,
94 const char *name, const char *instance, const char *realm,
95 enum hdb_ent_type ent_type,
101 ret = krb5_425_conv_principal_ext2(context, name, instance, realm,
102 valid_princ, config, 0, &p);
105 ret = _kdc_db_fetch(context, config, p, ent_type, ent);
106 krb5_free_principal(context, p);
110 #define RCHECK(X, L) if(X){make_err_reply(context, reply, KFAILURE, "Packet too short"); goto L;}
113 * Process the v4 request in `buf, len' (received from `addr'
114 * (with string `from').
115 * Return an error code and a reply in `reply'.
119 _kdc_do_version4(krb5_context context,
120 krb5_kdc_configuration *config,
125 struct sockaddr_in *addr)
129 hdb_entry *client = NULL, *server = NULL;
134 char *name = NULL, *inst = NULL, *realm = NULL;
135 char *sname = NULL, *sinst = NULL;
139 char client_name[256];
140 char server_name[256];
142 if(!config->enable_v4) {
143 kdc_log(context, config, 0,
144 "Rejected version 4 request from %s", from);
145 make_err_reply(context, reply, KDC_GEN_ERR, "function not enabled");
149 sp = krb5_storage_from_mem(buf, len);
150 RCHECK(krb5_ret_int8(sp, &pvno), out);
152 kdc_log(context, config, 0,
153 "Protocol version mismatch (krb4) (%d)", pvno);
154 make_err_reply(context, reply, KDC_PKT_VER, "protocol mismatch");
157 RCHECK(krb5_ret_int8(sp, &msg_type), out);
161 case AUTH_MSG_KDC_REQUEST: {
162 krb5_data ticket, cipher;
163 krb5_keyblock session;
165 krb5_data_zero(&ticket);
166 krb5_data_zero(&cipher);
168 RCHECK(krb5_ret_stringz(sp, &name), out1);
169 RCHECK(krb5_ret_stringz(sp, &inst), out1);
170 RCHECK(krb5_ret_stringz(sp, &realm), out1);
171 RCHECK(krb5_ret_int32(sp, &req_time), out1);
173 req_time = swap32(req_time);
174 RCHECK(krb5_ret_int8(sp, &life), out1);
175 RCHECK(krb5_ret_stringz(sp, &sname), out1);
176 RCHECK(krb5_ret_stringz(sp, &sinst), out1);
177 snprintf (client_name, sizeof(client_name),
178 "%s.%s@%s", name, inst, realm);
179 snprintf (server_name, sizeof(server_name),
180 "%s.%s@%s", sname, sinst, config->v4_realm);
182 kdc_log(context, config, 0, "AS-REQ (krb4) %s from %s for %s",
183 client_name, from, server_name);
185 ret = _kdc_db_fetch4(context, config, name, inst, realm, HDB_ENT_TYPE_CLIENT, &client);
187 kdc_log(context, config, 0, "Client not found in database: %s: %s",
188 client_name, krb5_get_err_text(context, ret));
189 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN,
190 "principal unknown");
193 ret = _kdc_db_fetch4(context, config, sname, sinst,
194 config->v4_realm, HDB_ENT_TYPE_SERVER, &server);
196 kdc_log(context, config, 0, "Server not found in database: %s: %s",
197 server_name, krb5_get_err_text(context, ret));
198 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN,
199 "principal unknown");
203 ret = _kdc_check_flags (context, config,
208 /* good error code? */
209 make_err_reply(context, reply, KERB_ERR_NAME_EXP,
210 "operation not allowed");
215 * There's no way to do pre-authentication in v4 and thus no
216 * good error code to return if preauthentication is required.
219 if (config->require_preauth
220 || client->flags.require_preauth
221 || server->flags.require_preauth) {
222 kdc_log(context, config, 0,
223 "Pre-authentication required for v4-request: "
225 client_name, server_name);
226 make_err_reply(context, reply, KERB_ERR_NULL_KEY,
231 ret = _kdc_get_des_key(context, client, FALSE, FALSE, &ckey);
233 kdc_log(context, config, 0, "no suitable DES key for client");
234 make_err_reply(context, reply, KDC_NULL_KEY,
235 "no suitable DES key for client");
240 /* this is not necessary with the new code in libkrb */
241 /* find a properly salted key */
242 while(ckey->salt == NULL || ckey->salt->salt.length != 0)
243 ret = hdb_next_keytype2key(context, client, KEYTYPE_DES, &ckey);
245 kdc_log(context, config, 0, "No version-4 salted key in database -- %s.%s@%s",
247 make_err_reply(context, reply, KDC_NULL_KEY,
248 "No version-4 salted key in database");
253 ret = _kdc_get_des_key(context, server, TRUE, FALSE, &skey);
255 kdc_log(context, config, 0, "no suitable DES key for server");
257 make_err_reply(context, reply, KDC_NULL_KEY,
258 "no suitable DES key for server");
262 max_life = _krb5_krb_life_to_time(0, life);
264 max_life = min(max_life, *client->max_life);
266 max_life = min(max_life, *server->max_life);
268 life = krb_time_to_life(kdc_time, kdc_time + max_life);
270 ret = krb5_generate_random_keyblock(context,
274 make_err_reply(context, reply, KFAILURE,
275 "Not enough random i KDC");
279 ret = _krb5_krb_create_ticket(context,
284 addr->sin_addr.s_addr,
293 krb5_free_keyblock_contents(context, &session);
294 make_err_reply(context, reply, KFAILURE,
295 "failed to create v4 ticket");
299 ret = _krb5_krb_create_ciph(context,
310 krb5_free_keyblock_contents(context, &session);
311 krb5_data_free(&ticket);
313 make_err_reply(context, reply, KFAILURE,
314 "Failed to create v4 cipher");
318 ret = _krb5_krb_create_auth_reply(context,
324 client->pw_end ? *client->pw_end : 0,
328 krb5_data_free(&cipher);
333 case AUTH_MSG_APPL_REQUEST: {
334 struct _krb5_krb_auth_data ad;
341 krb5_principal tgt_princ = NULL;
342 hdb_entry *tgt = NULL;
344 time_t max_end, actual_end, issue_time;
346 memset(&ad, 0, sizeof(ad));
347 krb5_data_zero(&auth);
349 RCHECK(krb5_ret_int8(sp, &kvno), out2);
350 RCHECK(krb5_ret_stringz(sp, &realm), out2);
352 ret = krb5_425_conv_principal(context, "krbtgt", realm,
356 kdc_log(context, config, 0,
357 "Converting krbtgt principal (krb4): %s",
358 krb5_get_err_text(context, ret));
359 make_err_reply(context, reply, KFAILURE,
360 "Failed to convert v4 principal (krbtgt)");
364 ret = _kdc_db_fetch(context, config, tgt_princ, HDB_ENT_TYPE_SERVER, &tgt);
367 s = kdc_log_msg(context, config, 0, "Ticket-granting ticket not "
368 "found in database (krb4): krbtgt.%s@%s: %s",
369 realm, config->v4_realm,
370 krb5_get_err_text(context, ret));
371 make_err_reply(context, reply, KFAILURE, s);
376 if(tgt->kvno % 256 != kvno){
377 kdc_log(context, config, 0,
378 "tgs-req (krb4) with old kvno %d (current %d) for "
379 "krbtgt.%s@%s", kvno, tgt->kvno % 256,
380 realm, config->v4_realm);
381 make_err_reply(context, reply, KDC_AUTH_EXP,
382 "old krbtgt kvno used");
386 ret = _kdc_get_des_key(context, tgt, TRUE, FALSE, &tkey);
388 kdc_log(context, config, 0,
389 "no suitable DES key for krbtgt (krb4)");
391 make_err_reply(context, reply, KDC_NULL_KEY,
392 "no suitable DES key for krbtgt");
396 RCHECK(krb5_ret_int8(sp, &ticket_len), out2);
397 RCHECK(krb5_ret_int8(sp, &req_len), out2);
399 pos = krb5_storage_seek(sp, ticket_len + req_len, SEEK_CUR);
404 if (config->check_ticket_addresses)
405 address = addr->sin_addr.s_addr;
409 ret = _krb5_krb_rd_req(context, &auth, "krbtgt", realm,
411 address, &tkey->key, &ad);
413 kdc_log(context, config, 0, "krb_rd_req: %d", ret);
414 make_err_reply(context, reply, ret, "failed to parse request");
418 RCHECK(krb5_ret_int32(sp, &req_time), out2);
420 req_time = swap32(req_time);
421 RCHECK(krb5_ret_int8(sp, &life), out2);
422 RCHECK(krb5_ret_stringz(sp, &sname), out2);
423 RCHECK(krb5_ret_stringz(sp, &sinst), out2);
424 snprintf (server_name, sizeof(server_name),
426 sname, sinst, config->v4_realm);
427 snprintf (client_name, sizeof(client_name),
429 ad.pname, ad.pinst, ad.prealm);
431 kdc_log(context, config, 0, "TGS-REQ (krb4) %s from %s for %s",
432 client_name, from, server_name);
434 if(strcmp(ad.prealm, realm)){
435 kdc_log(context, config, 0,
436 "Can't hop realms (krb4) %s -> %s", realm, ad.prealm);
437 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN,
442 if (!config->enable_v4_cross_realm && strcmp(realm, config->v4_realm) != 0) {
443 kdc_log(context, config, 0,
444 "krb4 Cross-realm %s -> %s disabled",
445 realm, config->v4_realm);
446 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN,
451 if(strcmp(sname, "changepw") == 0){
452 kdc_log(context, config, 0,
453 "Bad request for changepw ticket (krb4)");
454 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN,
455 "Can't authorize password change based on TGT");
459 ret = _kdc_db_fetch4(context, config, ad.pname, ad.pinst, ad.prealm, HDB_ENT_TYPE_CLIENT, &client);
460 if(ret && ret != HDB_ERR_NOENTRY) {
462 s = kdc_log_msg(context, config, 0,
463 "Client not found in database: (krb4) %s: %s",
464 client_name, krb5_get_err_text(context, ret));
465 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN, s);
469 if (client == NULL && strcmp(ad.prealm, config->v4_realm) == 0) {
471 s = kdc_log_msg(context, config, 0,
472 "Local client not found in database: (krb4) "
474 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN, s);
479 ret = _kdc_db_fetch4(context, config, sname, sinst, config->v4_realm,
480 HDB_ENT_TYPE_SERVER, &server);
483 s = kdc_log_msg(context, config, 0,
484 "Server not found in database (krb4): %s: %s",
485 server_name, krb5_get_err_text(context, ret));
486 make_err_reply(context, reply, KERB_ERR_PRINCIPAL_UNKNOWN, s);
491 ret = _kdc_check_flags (context, config,
496 /* good error code? */
497 make_err_reply(context, reply, KERB_ERR_NAME_EXP,
498 "operation not allowed");
502 ret = _kdc_get_des_key(context, server, TRUE, FALSE, &skey);
504 kdc_log(context, config, 0,
505 "no suitable DES key for server (krb4)");
507 make_err_reply(context, reply, KDC_NULL_KEY,
508 "no suitable DES key for server");
512 max_end = _krb5_krb_life_to_time(ad.time_sec, ad.life);
513 max_end = min(max_end, _krb5_krb_life_to_time(kdc_time, life));
515 max_end = min(max_end, kdc_time + *server->max_life);
516 if(client && client->max_life)
517 max_end = min(max_end, kdc_time + *client->max_life);
518 life = min(life, krb_time_to_life(kdc_time, max_end));
520 issue_time = kdc_time;
521 actual_end = _krb5_krb_life_to_time(issue_time, life);
522 while (actual_end > max_end && life > 1) {
523 /* move them into the next earlier lifetime bracket */
525 actual_end = _krb5_krb_life_to_time(issue_time, life);
527 if (actual_end > max_end) {
528 /* if life <= 1 and it's still too long, backdate the ticket */
529 issue_time -= actual_end - max_end;
533 krb5_data ticket, cipher;
534 krb5_keyblock session;
536 krb5_data_zero(&ticket);
537 krb5_data_zero(&cipher);
539 ret = krb5_generate_random_keyblock(context,
543 make_err_reply(context, reply, KFAILURE,
544 "Not enough random i KDC");
548 ret = _krb5_krb_create_ticket(context,
553 addr->sin_addr.s_addr,
562 krb5_free_keyblock_contents(context, &session);
563 make_err_reply(context, reply, KFAILURE,
564 "failed to create v4 ticket");
568 ret = _krb5_krb_create_ciph(context,
579 krb5_free_keyblock_contents(context, &session);
581 make_err_reply(context, reply, KFAILURE,
582 "failed to create v4 cipher");
586 ret = _krb5_krb_create_auth_reply(context,
596 krb5_data_free(&cipher);
599 _krb5_krb_free_auth_data(context, &ad);
601 krb5_free_principal(context, tgt_princ);
603 _kdc_free_ent(context, tgt);
606 case AUTH_MSG_ERR_REPLY:
609 kdc_log(context, config, 0, "Unknown message type (krb4): %d from %s",
612 make_err_reply(context, reply, KFAILURE, "Unknown message type");
626 _kdc_free_ent(context, client);
628 _kdc_free_ent(context, server);
629 krb5_storage_free(sp);
634 _kdc_encode_v4_ticket(krb5_context context,
635 krb5_kdc_configuration *config,
636 void *buf, size_t len, const EncTicketPart *et,
637 const PrincipalName *service, size_t *size)
641 char name[40], inst[40], realm[40];
642 char sname[40], sinst[40];
645 krb5_principal princ;
646 _krb5_principalname2krb5_principal(&princ,
649 ret = krb5_524_conv_principal(context,
654 krb5_free_principal(context, princ);
658 _krb5_principalname2krb5_principal(&princ,
662 ret = krb5_524_conv_principal(context,
667 krb5_free_principal(context, princ);
672 sp = krb5_storage_emem();
674 krb5_store_int8(sp, 0); /* flags */
675 krb5_store_stringz(sp, name);
676 krb5_store_stringz(sp, inst);
677 krb5_store_stringz(sp, realm);
679 unsigned char tmp[4] = { 0, 0, 0, 0 };
682 for(i = 0; i < et->caddr->len; i++)
683 if(et->caddr->val[i].addr_type == AF_INET &&
684 et->caddr->val[i].address.length == 4){
685 memcpy(tmp, et->caddr->val[i].address.data, 4);
689 krb5_storage_write(sp, tmp, sizeof(tmp));
692 if((et->key.keytype != ETYPE_DES_CBC_MD5 &&
693 et->key.keytype != ETYPE_DES_CBC_MD4 &&
694 et->key.keytype != ETYPE_DES_CBC_CRC) ||
695 et->key.keyvalue.length != 8)
697 krb5_storage_write(sp, et->key.keyvalue.data, 8);
700 time_t start = et->starttime ? *et->starttime : et->authtime;
701 krb5_store_int8(sp, krb_time_to_life(start, et->endtime));
702 krb5_store_int32(sp, start);
705 krb5_store_stringz(sp, sname);
706 krb5_store_stringz(sp, sinst);
710 krb5_storage_to_data(sp, &data);
711 krb5_storage_free(sp);
712 *size = (data.length + 7) & ~7; /* pad to 8 bytes */
715 memset((unsigned char*)buf - *size + 1, 0, *size);
716 memcpy((unsigned char*)buf - *size + 1, data.data, data.length);
717 krb5_data_free(&data);
723 _kdc_get_des_key(krb5_context context,
724 hdb_entry *principal, krb5_boolean is_server,
725 krb5_boolean prefer_afs_key, Key **ret_key)
727 Key *v5_key = NULL, *v4_key = NULL, *afs_key = NULL, *server_key = NULL;
729 krb5_enctype etypes[] = { ETYPE_DES_CBC_MD5,
734 i < sizeof(etypes)/sizeof(etypes[0])
735 && (v5_key == NULL || v4_key == NULL ||
736 afs_key == NULL || server_key == NULL);
739 while(hdb_next_enctype2key(context, principal, etypes[i], &key) == 0) {
740 if(key->salt == NULL) {
743 } else if(key->salt->type == hdb_pw_salt &&
744 key->salt->salt.length == 0) {
747 } else if(key->salt->type == hdb_afs3_salt) {
750 } else if(server_key == NULL)
762 else if(is_server && server_key)
763 *ret_key = server_key;
765 return KERB_ERR_NULL_KEY;
773 else if(is_server && server_key)
774 *ret_key = server_key;
776 return KERB_ERR_NULL_KEY;
779 if((*ret_key)->key.keyvalue.length == 0)
780 return KERB_ERR_NULL_KEY;