2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afs/param.h>
17 #include <stdio.h> /* sprintf */
19 #include <afs/kautils.h>
20 #include <afs/cm_config.h>
22 #include <afs/krb_prot.h>
26 #include "../permit_xprt.h"
28 int krb_add_host (struct sockaddr_in *server_list_p);
29 static void krb_set_port(long port);
31 static long ka_AddHostProc(void *rockp, struct sockaddr_in *addrp, char *namep)
33 return krb_add_host(addrp);
36 static char bogusReason[100];
38 static char *ka_MapKerberosError(int code)
42 return "password was incorrect";
43 case KERB_ERR_PRINCIPAL_UNKNOWN:
44 return "user doesn't exist";
45 case KERB_ERR_SERVICE_EXP:
46 return "server and client clocks are badly skewed";
48 return "Authentication Server was unavailable";
50 sprintf(bogusReason, "unknown authentication error %d",
56 static int krb_get_in_tkt_ext(
63 struct ktc_encryptionKey *key1,
64 struct ktc_encryptionKey *key2,
67 struct ktc_encryptionKey *outKeyp,
73 afs_int32 ka_UserAuthenticateGeneral(
80 afs_int32 *password_expiresP,
85 struct ktc_encryptionKey key1, key2;
88 struct ktc_encryptionKey sessionKey;
95 struct ktc_principal server;
96 struct ktc_principal client;
97 struct ktc_token token;
99 if (instance == NULL) instance = "";
100 if (lifetime == 0) lifetime = MAXKTCTICKETLIFETIME;
102 code = cm_SearchCellFile(realm, fullRealm, ka_AddHostProc, NULL);
105 *reasonP = "specified realm is unknown";
109 strcpy(upperRealm, fullRealm);
112 /* encrypt password, both ways */
113 ka_StringToKey(password, upperRealm, &key1);
114 des_string_to_key(password, &key2);
116 /* set port number */
117 sp = getservbyname("kerberos", "udp");
118 if (sp) krb_set_port(ntohs(sp->s_port));
121 code = krb_get_in_tkt_ext(name, instance, upperRealm, "afs", "",
122 lifetime, &key1, &key2, &ticket, &ticketLen, &sessionKey,
123 &kvno, &expirationTime, reasonP);
125 if (code && *reasonP == NULL)
126 *reasonP = ka_MapKerberosError(code);
131 strcpy(server.name, "afs");
132 strcpy(server.instance, "");
133 strcpy(server.cell, fullRealm);
135 /* Would like to use Vice ID's; using raw names for now. */
136 strcpy(client.name, name);
137 strcpy(client.instance, instance);
138 strcpy(client.cell, upperRealm);
140 token.startTime = 0; /* XXX */
141 token.endTime = expirationTime;
142 token.sessionKey = sessionKey;
143 token.kvno = (short) kvno;
144 token.ticketLen = ticketLen;
145 memcpy(token.ticket, ticket, ticketLen);
147 code = ktc_SetToken(&server, &token, &client,
148 (flags & KA_USERAUTH_AUTHENT_LOGON) ? AFS_SETTOK_LOGON : 0);
150 if (code == KTC_NOCM || code == KTC_NOCMRPC)
151 *reasonP = "AFS service may not have started";
152 else if (code == KTC_RPC)
153 *reasonP = "RPC failure in AFS gateway";
154 else if (code == KTC_NOCELL)
155 *reasonP = "unknown cell";
157 *reasonP = "unknown error";
166 * This code is descended from kerberos files krb_get_in_tkt.c and
167 * send_to_kdc.c, and one.c.
171 * definition of variable set to 1.
172 * used in krb_conf.h to determine host byte order.
174 static int krbONE = 1;
176 #define HOST_BYTE_ORDER (* (char *) &krbONE)
181 * Copyright 1986, 1987, 1988 by the Massachusetts Institute
184 * For copying and distribution information, please see the file
194 #include <sys/types.h>
195 #include <winsock2.h>
197 static int swap_bytes;
200 * The kaserver defines these error codes *privately*. So we redefine them
201 * here, with a slight name change to show that they really are kaserver
204 #define KERB_KA_ERR_BAD_MSG_TYPE 99
205 #define KERB_KA_ERR_BAD_LIFETIME 98
206 #define KERB_KA_ERR_NONNULL_REALM 97
207 #define KERB_KA_ERR_PKT_LENGTH 96
208 #define KERB_KA_ERR_TEXT_LENGTH 95
210 static void swap_u_int32
213 *u = *u >> 24 | (*u & 0x00ff0000) >> 8 | (*u & 0x0000ff00) << 8 | *u << 24;
216 static void swap_u_int16
219 *u = *u >> 8 | *u << 8;
222 int pkt_clen(KTEXT pkt);
223 KTEXT pkt_cipher(KTEXT packet);
226 * The following routine has been hacked to make it work for two different
227 * possible string-to-key algorithms. This is a minimal displacement
232 * krb_get_in_tkt() gets a ticket for a given principal to use a given
233 * service and stores the returned ticket and session key for future
236 * The "user", "instance", and "realm" arguments give the identity of
237 * the client who will use the ticket. The "service" and "sinstance"
238 * arguments give the identity of the server that the client wishes
239 * to use. (The realm of the server is the same as the Kerberos server
240 * to whom the request is sent.) The "life" argument indicates the
241 * desired lifetime of the ticket; the "key_proc" argument is a pointer
242 * to the routine used for getting the client's private key to decrypt
243 * the reply from Kerberos. The "decrypt_proc" argument is a pointer
244 * to the routine used to decrypt the reply from Kerberos; and "arg"
245 * is an argument to be passed on to the "key_proc" routine.
247 * If all goes well, krb_get_in_tkt() returns INTK_OK, otherwise it
248 * returns an error code: If an AUTH_MSG_ERR_REPLY packet is returned
249 * by Kerberos, then the error code it contains is returned. Other
250 * error codes returned by this routine include INTK_PROT to indicate
251 * wrong protocol version, INTK_BADPW to indicate bad password (if
252 * decrypted ticket didn't make sense), INTK_ERR if the ticket was for
253 * the wrong server or the ticket store couldn't be initialized.
255 * The format of the message sent to Kerberos is as follows:
257 * Size Variable Field
258 * ---- -------- -----
260 * 1 byte KRB_PROT_VERSION protocol version number
261 * 1 byte AUTH_MSG_KDC_REQUEST | message type
262 * HOST_BYTE_ORDER local byte order in lsb
263 * string user client's name
264 * string instance client's instance
265 * string realm client's realm
266 * 4 bytes tlocal.tv_sec timestamp in seconds
267 * 1 byte life desired lifetime
268 * string service service's name
269 * string sinstance service's instance
273 * Check_response is a support routine for krb_get_in_tkt.
275 * Check the response with the supplied key. If the key is apparently
276 * wrong, return INTK_BADPW, otherwise zero.
278 static check_response
284 struct ktc_encryptionKey *key)
288 char s_service[SNAME_SZ];
289 char s_instance[INST_SZ];
290 char s_realm[REALM_SZ];
295 /* copy information from return packet into "cip" */
296 cip->length = pkt_clen(rpkt);
297 memcpy((char *)(cip->dat), (char *) pkt_cipher(rpkt), cip->length);
300 key_sched((char *) key, key_s);
301 pcbc_encrypt((C_Block *)cip->dat, (C_Block *)cip->dat,
302 (long) cip->length, key_s, (des_cblock *) key, 0);
304 /* Skip session key */
305 ptr = (char *) cip->dat + 8;
307 /* Check and extract server's name */
308 if ((strlen(ptr) + (ptr - (char *) cip->dat)) > cip->length)
311 (void) strncpy(s_service, ptr, sizeof(s_service)-1);
312 s_service[sizeof(s_service)-1] = '\0';
313 ptr += strlen(s_service) + 1;
315 /* Check and extract server's instance */
316 if ((strlen(ptr) + (ptr - (char *) cip->dat)) > cip->length)
319 (void) strncpy(s_instance,ptr, sizeof(s_instance)-1);
320 s_instance[sizeof(s_instance)-1] = '\0';
321 ptr += strlen(s_instance) + 1;
323 /* Check and extract server's realm */
324 if ((strlen(ptr) + (ptr - (char *) cip->dat)) > cip->length)
327 (void) strncpy(s_realm,ptr, sizeof(s_realm));
328 s_realm[sizeof(s_realm)-1] = '\0';
329 ptr += strlen(s_realm) + 1;
331 /* Ignore ticket lifetime, server key version */
334 /* Extract and check ticket length */
335 ticket_len = (unsigned char) *ptr++;
337 if ((ticket_len < 0) ||
338 ((ticket_len + (ptr - (char *) cip->dat)) > (int) cip->length))
341 /* Check returned server name, instance, and realm fields */
343 * 7/23/98 - Deleting realm check. This allows cell name to differ
347 if (strcmp(s_service, service) || strcmp(s_instance, instance) ||
348 strcmp(s_realm, realm)) {
350 if (strcmp(s_service, service) || strcmp(s_instance, instance)) {
352 /* not what we asked for: assume decryption failed */
360 * The old kaserver (pre 3.4) returned zero error codes sometimes, leaving
361 * the kaserver error code in a string in the text of the error message.
362 * The new one does the same, but returns KDC_GEN_ERR rather than zero.
363 * We try to extract the actual error code.
365 static char bogus_kaerror[100];
366 static int kaserver_map_error_code
371 if (code == 0 || code == KDC_GEN_ERR) {
373 if (sscanf(etext, "code =%u: ", &mapcode) == 1) {
375 strcpy(bogus_kaerror, etext);
376 *reasonP = bogus_kaerror;
387 static int krb_get_in_tkt_ext(user, instance, realm, service, sinstance, life,
388 key1, key2, ticketpp, ticketLenp, outKeyp, kvnop, expp, reasonp)
395 struct ktc_encryptionKey *key1, *key2;
398 struct ktc_encryptionKey *outKeyp;
404 KTEXT pkt = &pkt_st; /* Packet to KDC */
406 KTEXT rpkt = &rpkt_st; /* Returned packet */
408 KTEXT cip = &cip_st; /* Returned Ciphertext */
410 KTEXT tkt = &tkt_st; /* Current ticket */
411 C_Block ses; /* Session key for tkt */
412 int kvno; /* Kvno for session key */
413 unsigned char *v = pkt->dat; /* Prot vers no */
414 unsigned char *t = (pkt->dat+1); /* Prot msg type */
416 char s_name[SNAME_SZ];
417 char s_instance[INST_SZ];
425 unsigned long t_local;
427 afs_uint32 rep_err_code;
431 /* BUILD REQUEST PACKET */
433 /* Set up the fixed part of the packet */
434 *v = (unsigned char) KRB_PROT_VERSION;
435 *t = (unsigned char) AUTH_MSG_KDC_REQUEST;
436 *t |= HOST_BYTE_ORDER;
438 /* Now for the variable info */
439 (void) strcpy((char *)(pkt->dat+2),user); /* aname */
440 pkt->length = 3 + strlen(user);
441 (void) strcpy((char *)(pkt->dat+pkt->length),
442 instance); /* instance */
443 pkt->length += 1 + strlen(instance);
444 (void) strcpy((char *)(pkt->dat+pkt->length),realm); /* realm */
445 pkt->length += 1 + strlen(realm);
448 (void) gettimeofday(&t_local,(struct timezone *) 0);
450 t_local = time((void *) 0);
453 memcpy((char *)(pkt->dat+pkt->length), (char *)&(t_local), 4);
457 kerberos_life = DEFAULT_TKT_LIFE;
459 kerberos_life = time_to_life(0, life);
460 if (kerberos_life == 0) {
461 kerberos_life = DEFAULT_TKT_LIFE;
465 *(pkt->dat+(pkt->length)++) = kerberos_life;
466 (void) strcpy((char *)(pkt->dat+pkt->length),service);
467 pkt->length += 1 + strlen(service);
468 (void) strcpy((char *)(pkt->dat+pkt->length),sinstance);
470 pkt->length += 1 + strlen(sinstance);
474 /* SEND THE REQUEST AND RECEIVE THE RETURN PACKET */
476 if (kerror = send_to_kdc(pkt, rpkt)) {
480 /* check packet version of the returned packet */
481 if (pkt_version(rpkt) != KRB_PROT_VERSION)
484 /* Check byte order */
485 msg_byte_order = pkt_msg_type(rpkt) & 1;
487 if (msg_byte_order != HOST_BYTE_ORDER) {
491 switch (pkt_msg_type(rpkt) & ~1) {
492 case AUTH_MSG_KDC_REPLY:
494 case AUTH_MSG_ERR_REPLY:
495 memcpy((char *) &rep_err_code, pkt_err_code(rpkt), 4);
496 if (swap_bytes) swap_u_int32(&rep_err_code);
497 /* kaservers return bogus error codes in different ways, so map it
498 from the error text if this is the case */
499 return kaserver_map_error_code(rep_err_code, pkt_err_text(rpkt), reasonp);
505 /* get the principal's expiration date */
506 memcpy((char *) &exp_date, pkt_x_date(rpkt), sizeof(exp_date));
507 if (swap_bytes) swap_u_int32(&exp_date);
509 /* Extract length. This will be re-extracted in check_response, below */
510 cip->length = pkt_clen(rpkt);
512 /* Length of zero seems to correspond to no principal (with kaserver) */
513 if (cip->length== 0) {
514 return (KERB_ERR_PRINCIPAL_UNKNOWN);
517 if ((cip->length < 0) || (cip->length > sizeof(cip->dat))) {
518 return(INTK_ERR); /* no appropriate error code
519 currently defined for INTK_ */
523 * Check the response against both possible keys, and use the one
526 if (check_response(rpkt, cip, service, sinstance, realm, key1) &&
527 check_response(rpkt, cip, service, sinstance, realm, key2)) {
532 * EXTRACT INFORMATION FROM RETURN PACKET
534 * Some of the fields, below are already checked for integrity by
537 ptr = (char *) cip->dat;
539 /* extract session key */
540 memcpy((char *)ses, ptr, 8);
543 /* extract server's name */
544 (void) strncpy(s_name,ptr, sizeof(s_name)-1);
545 s_name[sizeof(s_name)-1] = '\0';
546 ptr += strlen(s_name) + 1;
548 /* extract server's instance */
549 (void) strncpy(s_instance,ptr, sizeof(s_instance)-1);
550 s_instance[sizeof(s_instance)-1] = '\0';
551 ptr += strlen(s_instance) + 1;
553 /* extract server's realm */
554 (void) strncpy(rlm,ptr, sizeof(rlm));
555 rlm[sizeof(rlm)-1] = '\0';
556 ptr += strlen(rlm) + 1;
558 /* extract ticket lifetime, server key version, ticket length */
559 /* be sure to avoid sign extension on lifetime! */
560 lifetime = (unsigned char) ptr[0];
561 kvno = (unsigned char) ptr[1];
562 tkt->length = (unsigned char) ptr[2];
565 /* extract ticket itself */
566 memcpy((char *)(tkt->dat), ptr, tkt->length);
569 /* check KDC time stamp */
570 memcpy((char *)&kdc_time, ptr, 4); /* Time (coarse) */
571 if (swap_bytes) swap_u_int32(&kdc_time);
575 t_local = time((void *) 0);
576 if (abs((int)(t_local - kdc_time)) > CLOCK_SKEW) {
577 return(RD_AP_TIME); /* XXX should probably be better
581 /* copy out results; if *ticketpp is non-null, the caller has already
582 * allocated the buffer for us.
584 memcpy(outKeyp, ses, sizeof(struct ktc_encryptionKey));
585 if (*ticketpp == NULL) {
586 *ticketpp = malloc(tkt->length);
588 else if (tkt->length > (unsigned long) *ticketLenp) return -1;
589 *ticketLenp = tkt->length;
590 memcpy(*ticketpp, tkt->dat, tkt->length);
592 if (expp) *expp = life_to_time(kdc_time, (char) lifetime);
594 return(INTK_OK); /* this is zero */
599 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
601 * For copying and distribution information, please see the file
605 #define S_AD_SZ sizeof(struct sockaddr_in)
607 static int krb_debug;
609 /* CLIENT_KRB_TIMEOUT indicates the time to wait before
610 * retrying a server. It's defined in "krb.h".
612 static struct timeval timeout = { CLIENT_KRB_TIMEOUT, 0};
613 static char *prog = "dm";
617 * This file contains two routines, send_to_kdc() and send_recv().
618 * send_recv() is a static routine used by send_to_kdc().
622 * send_to_kdc() sends a message to the Kerberos authentication
623 * server(s) in the given realm and returns the reply message.
624 * The "pkt" argument points to the message to be sent to Kerberos;
625 * the "rpkt" argument will be filled in with Kerberos' reply.
626 * The "realm" argument indicates the realm of the Kerberos server(s)
627 * to transact with. If the realm is null, the local realm is used.
629 * If more than one Kerberos server is known for a given realm,
630 * different servers will be queried until one of them replies.
631 * Several attempts (retries) are made for each server before
632 * giving up entirely.
634 * If an answer was received from a Kerberos host, KSUCCESS is
635 * returned. The following errors can be returned:
637 * SKDC_CANT - can't get local realm
638 * - can't find "kerberos" in /etc/services database
639 * - can't open socket
640 * - can't bind socket
642 * - couldn't find any Kerberos host
644 * SKDC_RETRY - couldn't get an answer from any Kerberos server,
645 * after several retries
648 typedef struct krb_server {
649 struct krb_server *nextp;
650 struct sockaddr_in addr;
653 static long krb_udp_port = KRB_PORT; /* In host byte order */
654 static krb_server_t *krb_hosts_p = NULL;
655 static int krb_nhosts = 0;
657 static void krb_set_port
664 (struct sockaddr_in *server_list_p)
666 krb_server_t *krb_host_p;
668 krb_host_p = malloc(sizeof(krb_server_t));
670 /* add host to list */
671 krb_host_p->nextp = krb_hosts_p;
672 krb_hosts_p = krb_host_p;
675 /* copy in the data */
676 memcpy(&krb_host_p->addr, server_list_p, sizeof(struct sockaddr_in));
681 static send_to_kdc(pkt,rpkt)
689 struct sockaddr_in to;
690 int timeAvail, timePerIter, numIters;
692 memset((char *)&to, 0, sizeof(to));
693 if ((f = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
695 fprintf(stderr,"%s: Can't open socket\n", prog);
698 /* from now on, exit through rtn label for cleanup */
700 /* compute # of retries */
701 /* The SMB client seems to time out after 60 seconds. */
703 /* Leave ourselves some margin for fooling around
705 /* How long does one iteration take? */
706 timePerIter = krb_nhosts * CLIENT_KRB_TIMEOUT;
707 /* How many iters? */
708 numIters = timeAvail / timePerIter;
709 /* No more than max */
710 if (numIters > CLIENT_KRB_RETRY) numIters = CLIENT_KRB_RETRY;
712 if (numIters < 1) numIters = 1;
714 /* retry each host in sequence */
715 for (retry = 0; retry < numIters; ++retry) {
716 for(tsp = krb_hosts_p; tsp; tsp = tsp->nextp) {
718 to.sin_family = AF_INET;
719 to.sin_port = htons(((unsigned short)krb_udp_port));
720 if (send_recv(pkt, rpkt, f, &to)) {
730 (void) closesocket(f);
736 * try to send out and receive message.
737 * return 1 on success, 0 on failure
740 static send_recv(pkt,rpkt,f,_to)
744 struct sockaddr_in *_to;
747 struct sockaddr_in from;
753 if (_to->sin_family == AF_INET)
754 printf("Sending message to %s...",
755 inet_ntoa(_to->sin_addr));
757 printf("Sending message...");
758 (void) fflush(stdout);
760 if ((numsent = sendto(f,(char *)(pkt->dat), pkt->length, 0,
761 (struct sockaddr *)_to,
762 S_AD_SZ)) != (int) pkt->length) {
764 printf("sent only %d/%d\n",numsent, pkt->length);
768 printf("Sent\nWaiting for reply...");
769 (void) fflush(stdout);
774 /* select - either recv is ready, or timeout */
775 /* see if timeout or error or wrong descriptor */
776 if (select(f + 1, &readfds, (fd_set *)0, (fd_set *)0, &timeout) < 1
777 || !FD_ISSET(f, &readfds)) {
779 fprintf(stderr, "select failed: readfds=%x",
785 sin_size = sizeof(from);
786 if ((code = recvfrom(f, (char *)(rpkt->dat), sizeof(rpkt->dat), 0,
787 (struct sockaddr *)&from, &sin_size))
794 printf("received packet from %s\n", inet_ntoa(from.sin_addr));
802 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
805 * For copying and distribution information, please see the file
810 * This routine takes a reply packet from the Kerberos ticket-granting
811 * service and returns a pointer to the beginning of the ciphertext in it.
813 * See "krb_prot.h" for packet format.
816 static KTEXT pkt_cipher
819 unsigned char *ptr = pkt_a_realm(packet) + 6
820 + strlen((char *)pkt_a_realm(packet));
821 /* Skip a few more fields */
822 ptr += 3 + 4; /* add 4 for exp_date */
824 /* And return the pointer */
830 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
833 * For copying and distribution information, please see the file
838 * Given a pointer to an AUTH_MSG_KDC_REPLY packet, return the length of
839 * its ciphertext portion. The external variable "swap_bytes" is assumed
840 * to have been set to indicate whether or not the packet is in local
841 * byte order. pkt_clen() takes this into account when reading the
842 * ciphertext length out of the packet.
845 static int pkt_clen(KTEXT pkt)
849 /* Start of ticket list */
850 unsigned char *ptr = pkt_a_realm(pkt) + 10
851 + strlen((char *)pkt_a_realm(pkt));
853 /* Finally the length */
854 memcpy((char *)&temp, (char *)(++ptr), 2); /* alignment */
860 printf("Clen is %d\n", temp);
866 /* This defines the Andrew string_to_key function. It accepts a password
867 string as input and converts its via a one-way encryption algorithm to a DES
868 encryption key. It is compatible with the original Andrew authentication
869 service password database. */
871 static void Andrew_StringToKey (str, cell, key)
873 char *cell; /* cell for password */
874 struct ktc_encryptionKey *key;
875 { char password[8+1]; /* crypt's limit is 8 chars anyway */
879 memset(key, 0, sizeof(struct ktc_encryptionKey));
881 strncpy (password, cell, 8);
882 passlen = strlen (str);
883 if (passlen > 8) passlen = 8;
885 for (i=0; i<passlen; i++)
886 password[i] ^= str[i];
889 if (password[i] == '\0') password[i] = 'X';
891 /* crypt only considers the first 8 characters of password but for some
892 reason returns eleven characters of result (plus the two salt chars). */
893 strncpy((char *) key, (char *)crypt(password, "p1") + 2, sizeof(struct ktc_encryptionKey));
895 /* parity is inserted into the LSB so leftshift each byte up one bit. This
896 allows ascii characters with a zero MSB to retain as much significance
898 { char *keybytes = (char *)key;
901 for (i = 0; i < 8; i++) {
902 temp = (unsigned int) keybytes[i];
903 keybytes[i] = (unsigned char) (temp << 1);
906 des_fixup_key_parity ((unsigned char *) key);
910 static void StringToKey (str, cell, key)
912 char *cell; /* cell for password */
913 struct ktc_encryptionKey *key;
914 { des_key_schedule schedule;
917 char password[BUFSIZ];
920 strncpy (password, str, sizeof(password));
921 if ((passlen = strlen (password)) < sizeof(password)-1)
922 strncat (password, cell, sizeof(password)-passlen);
923 if ((passlen = strlen(password)) > sizeof(password))
924 passlen = sizeof(password);
926 memcpy(ivec, "kerberos", 8);
927 memcpy(temp_key, "kerberos", 8);
928 des_fixup_key_parity (temp_key);
929 des_key_sched (temp_key, schedule);
930 des_cbc_cksum (password, ivec, passlen, schedule, ivec);
932 memcpy(temp_key, ivec, 8);
933 des_fixup_key_parity (temp_key);
934 des_key_sched (temp_key, schedule);
935 des_cbc_cksum (password, (char *) key, passlen, schedule, ivec);
937 des_fixup_key_parity ((char *) key);