2 * kerberos.c -- Kerberos authentication (see RFC 1731).
4 * For license terms, see the file COPYING in this directory.
13 #if defined(STDC_HEADERS)
16 #include "fetchmail.h"
20 #include <sys/types.h>
21 #include <netinet/in.h> /* for htonl/ntohl */
27 #elif SIZEOF_SHORT == 4
29 #elif SIZEOF_LONG == 4
32 #error Cannot deduce a 32-bit-type
35 int do_rfc1731(int sock, const char *command, const char *truename)
36 /* authenticate as per RFC1731 -- note 32-bit integer requirement here */
39 char buf1[4096], buf2[4096];
43 } challenge1, challenge2;
44 char srvinst[INST_SZ];
46 char srvrealm[REALM_SZ];
47 KTEXT_ST authenticator;
48 CREDENTIALS credentials;
49 char tktuser[MAX_K_NAME_SZ+1+INST_SZ+1+REALM_SZ+1];
50 char tktinst[INST_SZ];
51 char tktrealm[REALM_SZ];
53 des_key_schedule schedule;
55 gen_send(sock, "%s KERBEROS_V4", command);
57 /* The data encoded in the first ready response contains a random
58 * 32-bit number in network byte order. The client should respond
59 * with a Kerberos ticket and an authenticator for the principal
60 * "imap.hostname@realm", where "hostname" is the first component
61 * of the host name of the server with all letters in lower case
62 * and where "realm" is the Kerberos realm of the server. The
63 * encrypted checksum field included within the Kerberos
64 * authenticator should contain the server provided 32-bit number
65 * in network byte order.
68 if ((result = gen_recv(sock, buf1, sizeof buf1)) != 0) {
72 len = from64tobits(challenge1.cstr, buf1, sizeof(challenge1.cstr));
74 report(stderr, GT_("could not decode initial BASE64 challenge\n"));
78 /* this patch by Dan Root <dar@thekeep.org> solves an endianess
83 *(int *)tmp = ntohl(*(int *) challenge1.cstr);
84 memcpy(challenge1.cstr, tmp, sizeof(tmp));
87 /* Client responds with a Kerberos ticket and an authenticator for
88 * the principal "imap.hostname@realm" where "hostname" is the
89 * first component of the host name of the server with all letters
90 * in lower case and where "realm" is the Kerberos realm of the
91 * server. The encrypted checksum field included within the
92 * Kerberos authenticator should contain the server-provided
93 * 32-bit number in network byte order.
96 strncpy(srvinst, truename, (sizeof srvinst)-1);
97 srvinst[(sizeof srvinst)-1] = '\0';
98 for (p = srvinst; *p; p++) {
99 if (isupper((unsigned char)*p)) {
100 *p = tolower((unsigned char)*p);
104 strncpy(srvrealm, (char *)krb_realmofhost(srvinst), (sizeof srvrealm)-1);
105 srvrealm[(sizeof srvrealm)-1] = '\0';
106 if ((p = strchr(srvinst, '.')) != NULL) {
110 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm, 0);
112 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
116 result = krb_get_cred("imap", srvinst, srvrealm, &credentials);
118 report(stderr, "krb_get_cred: %s\n", krb_get_err_text(result));
122 memcpy(session, credentials.session, sizeof session);
123 memset(&credentials, 0, sizeof credentials);
124 des_key_sched(&session, schedule);
126 result = krb_get_tf_fullname(TKT_FILE, tktuser, tktinst, tktrealm);
128 report(stderr, "krb_get_tf_fullname: %s\n", krb_get_err_text(result));
134 * Andrew H. Chatham <andrew.chatham@duke.edu> alleges that this check
135 * is not necessary and has consistently been messing him up.
137 if (strcmp(tktuser, user) != 0) {
139 GT_("principal %s in ticket does not match -u %s\n"), tktuser,
143 #endif /* __UNUSED__ */
147 GT_("non-null instance (%s) might cause strange behavior\n"),
149 strlcat(tktuser, ".", sizeof(tktuser));
150 strlcat(tktuser, tktinst, sizeof(tktuser));
153 if (strcmp(tktrealm, srvrealm) != 0) {
154 strlcat(tktuser, "@", sizeof(tktuser));
155 strlcat(tktuser, tktrealm, sizeof(tktuser));
158 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm,
161 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
165 to64frombits(buf1, authenticator.dat, authenticator.length);
166 if (outlevel >= O_MONITOR) {
167 report(stdout, "IMAP> %s\n", buf1);
169 strcat(buf1, "\r\n");
170 SockWrite(sock, buf1, strlen(buf1));
172 /* Upon decrypting and verifying the ticket and authenticator, the
173 * server should verify that the contained checksum field equals
174 * the original server provided random 32-bit number. Should the
175 * verification be successful, the server must add one to the
176 * checksum and construct 8 octets of data, with the first four
177 * octets containing the incremented checksum in network byte
178 * order, the fifth octet containing a bit-mask specifying the
179 * protection mechanisms supported by the server, and the sixth
180 * through eighth octets containing, in network byte order, the
181 * maximum cipher-text buffer size the server is able to receive.
182 * The server must encrypt the 8 octets of data in the session key
183 * and issue that encrypted data in a second ready response. The
184 * client should consider the server authenticated if the first
185 * four octets the un-encrypted data is equal to one plus the
186 * checksum it previously sent.
189 if ((result = gen_recv(sock, buf1, sizeof buf1)) != 0)
192 /* The client must construct data with the first four octets
193 * containing the original server-issued checksum in network byte
194 * order, the fifth octet containing the bit-mask specifying the
195 * selected protection mechanism, the sixth through eighth octets
196 * containing in network byte order the maximum cipher-text buffer
197 * size the client is able to receive, and the following octets
198 * containing a user name string. The client must then append
199 * from one to eight octets so that the length of the data is a
200 * multiple of eight octets. The client must then PCBC encrypt the
201 * data with the session key and respond to the second ready
202 * response with the encrypted data. The server decrypts the data
203 * and verifies the contained checksum. The username field
204 * identifies the user for whom subsequent IMAP operations are to
205 * be performed; the server must verify that the principal
206 * identified in the Kerberos ticket is authorized to connect as
207 * that user. After these verifications, the authentication
208 * process is complete.
211 len = from64tobits(buf2, buf1, sizeof(buf2));
213 report(stderr, GT_("could not decode BASE64 ready response\n"));
217 des_ecb_encrypt((des_cblock *)buf2, (des_cblock *)buf2, schedule, 0);
218 memcpy(challenge2.cstr, buf2, 4);
219 if ((int32)ntohl(challenge2.cint) != challenge1.cint + 1) {
220 report(stderr, GT_("challenge mismatch\n"));
224 memset(authenticator.dat, 0, sizeof authenticator.dat);
226 result = htonl(challenge1.cint);
227 memcpy(authenticator.dat, &result, sizeof result);
229 /* The protection mechanisms and their corresponding bit-masks are as
232 * 1 No protection mechanism
233 * 2 Integrity (krb_mk_safe) protection
234 * 4 Privacy (krb_mk_priv) protection
236 authenticator.dat[4] = 1;
238 len = strlen(tktuser);
239 strncpy((char *)authenticator.dat+8, tktuser, len);
240 authenticator.length = len + 8 + 1;
241 while (authenticator.length & 7) {
242 authenticator.length++;
244 des_pcbc_encrypt((const unsigned char *)authenticator.dat,
245 (unsigned char *)authenticator.dat, authenticator.length, schedule,
248 to64frombits(buf1, authenticator.dat, authenticator.length);
250 /* ship down the response, accept the server's error/ok indication */
251 suppress_tags = TRUE;
252 result = gen_transact(sock, "%s", buf1);
253 suppress_tags = FALSE;
259 #endif /* KERBEROS_V4 */
261 /* kerberos.c ends here */