2 * kerberos.c -- Kerberos authentication (see RFC 1731).
4 * For license terms, see the file COPYING in this directory.
10 #if defined(STDC_HEADERS)
13 #include "fetchmail.h"
16 #include <netinet/in.h> /* for htonl/ntohl */
21 # include <kerberosIV/des.h>
22 # include <kerberosIV/krb.h>
24 # if defined (__bsdi__)
26 # define krb_get_err_text(e) (krb_err_txt[e])
28 # if defined(__NetBSD__) || (__FreeBSD__) || defined(__linux__)
29 # define krb_get_err_text(e) (krb_err_txt[e])
34 /* des.h might define _ for no good reason. */
40 #elif SIZEOF_SHORT == 4
42 #elif SIZEOF_LONG == 4
45 #error Cannot deduce a 32-bit-type
48 int do_rfc1731(int sock, char *command, char *truename)
49 /* authenticate as per RFC1731 -- note 32-bit integer requirement here */
52 char buf1[4096], buf2[4096];
56 } challenge1, challenge2;
57 char srvinst[INST_SZ];
59 char srvrealm[REALM_SZ];
60 KTEXT_ST authenticator;
61 CREDENTIALS credentials;
62 char tktuser[MAX_K_NAME_SZ+1+INST_SZ+1+REALM_SZ+1];
63 char tktinst[INST_SZ];
64 char tktrealm[REALM_SZ];
66 des_key_schedule schedule;
68 gen_send(sock, "%s KERBEROS_V4", command);
70 /* The data encoded in the first ready response contains a random
71 * 32-bit number in network byte order. The client should respond
72 * with a Kerberos ticket and an authenticator for the principal
73 * "imap.hostname@realm", where "hostname" is the first component
74 * of the host name of the server with all letters in lower case
75 * and where "realm" is the Kerberos realm of the server. The
76 * encrypted checksum field included within the Kerberos
77 * authenticator should contain the server provided 32-bit number
78 * in network byte order.
81 if (result = gen_recv(sock, buf1, sizeof buf1)) {
85 len = from64tobits(challenge1.cstr, buf1);
87 report(stderr, _("could not decode initial BASE64 challenge\n"));
91 /* this patch by Dan Root <dar@thekeep.org> solves an endianess
96 *(int *)tmp = ntohl(*(int *) challenge1.cstr);
97 memcpy(challenge1.cstr, tmp, sizeof(tmp));
100 /* Client responds with a Kerberos ticket and an authenticator for
101 * the principal "imap.hostname@realm" where "hostname" is the
102 * first component of the host name of the server with all letters
103 * in lower case and where "realm" is the Kerberos realm of the
104 * server. The encrypted checksum field included within the
105 * Kerberos authenticator should contain the server-provided
106 * 32-bit number in network byte order.
109 strncpy(srvinst, truename, (sizeof srvinst)-1);
110 srvinst[(sizeof srvinst)-1] = '\0';
111 for (p = srvinst; *p; p++) {
117 strncpy(srvrealm, (char *)krb_realmofhost(srvinst), (sizeof srvrealm)-1);
118 srvrealm[(sizeof srvrealm)-1] = '\0';
119 if (p = strchr(srvinst, '.')) {
123 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm, 0);
125 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
129 result = krb_get_cred("imap", srvinst, srvrealm, &credentials);
131 report(stderr, "krb_get_cred: %s\n", krb_get_err_text(result));
135 memcpy(session, credentials.session, sizeof session);
136 memset(&credentials, 0, sizeof credentials);
137 des_key_sched(&session, schedule);
139 result = krb_get_tf_fullname(TKT_FILE, tktuser, tktinst, tktrealm);
141 report(stderr, "krb_get_tf_fullname: %s\n", krb_get_err_text(result));
147 * Andrew H. Chatham <andrew.chatham@duke.edu> alleges that this check
148 * is not necessary and has consistently been messing him up.
150 if (strcmp(tktuser, user) != 0) {
152 _("principal %s in ticket does not match -u %s\n"), tktuser,
156 #endif /* __UNUSED__ */
160 _("non-null instance (%s) might cause strange behavior\n"),
162 strcat(tktuser, ".");
163 strcat(tktuser, tktinst);
166 if (strcmp(tktrealm, srvrealm) != 0) {
167 strcat(tktuser, "@");
168 strcat(tktuser, tktrealm);
171 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm,
174 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
178 to64frombits(buf1, authenticator.dat, authenticator.length);
179 if (outlevel >= O_MONITOR) {
180 report(stdout, "IMAP> %s\n", buf1);
182 strcat(buf1, "\r\n");
183 SockWrite(sock, buf1, strlen(buf1));
185 /* Upon decrypting and verifying the ticket and authenticator, the
186 * server should verify that the contained checksum field equals
187 * the original server provided random 32-bit number. Should the
188 * verification be successful, the server must add one to the
189 * checksum and construct 8 octets of data, with the first four
190 * octets containing the incremented checksum in network byte
191 * order, the fifth octet containing a bit-mask specifying the
192 * protection mechanisms supported by the server, and the sixth
193 * through eighth octets containing, in network byte order, the
194 * maximum cipher-text buffer size the server is able to receive.
195 * The server must encrypt the 8 octets of data in the session key
196 * and issue that encrypted data in a second ready response. The
197 * client should consider the server authenticated if the first
198 * four octets the un-encrypted data is equal to one plus the
199 * checksum it previously sent.
202 if (result = gen_recv(sock, buf1, sizeof buf1))
205 /* The client must construct data with the first four octets
206 * containing the original server-issued checksum in network byte
207 * order, the fifth octet containing the bit-mask specifying the
208 * selected protection mechanism, the sixth through eighth octets
209 * containing in network byte order the maximum cipher-text buffer
210 * size the client is able to receive, and the following octets
211 * containing a user name string. The client must then append
212 * from one to eight octets so that the length of the data is a
213 * multiple of eight octets. The client must then PCBC encrypt the
214 * data with the session key and respond to the second ready
215 * response with the encrypted data. The server decrypts the data
216 * and verifies the contained checksum. The username field
217 * identifies the user for whom subsequent IMAP operations are to
218 * be performed; the server must verify that the principal
219 * identified in the Kerberos ticket is authorized to connect as
220 * that user. After these verifications, the authentication
221 * process is complete.
224 len = from64tobits(buf2, buf1);
226 report(stderr, _("could not decode BASE64 ready response\n"));
230 des_ecb_encrypt((des_cblock *)buf2, (des_cblock *)buf2, schedule, 0);
231 memcpy(challenge2.cstr, buf2, 4);
232 if (ntohl(challenge2.cint) != challenge1.cint + 1) {
233 report(stderr, _("challenge mismatch\n"));
237 memset(authenticator.dat, 0, sizeof authenticator.dat);
239 result = htonl(challenge1.cint);
240 memcpy(authenticator.dat, &result, sizeof result);
242 /* The protection mechanisms and their corresponding bit-masks are as
245 * 1 No protection mechanism
246 * 2 Integrity (krb_mk_safe) protection
247 * 4 Privacy (krb_mk_priv) protection
249 authenticator.dat[4] = 1;
251 len = strlen(tktuser);
252 strncpy(authenticator.dat+8, tktuser, len);
253 authenticator.length = len + 8 + 1;
254 while (authenticator.length & 7) {
255 authenticator.length++;
257 des_pcbc_encrypt((des_cblock *)authenticator.dat,
258 (des_cblock *)authenticator.dat, authenticator.length, schedule,
261 to64frombits(buf1, authenticator.dat, authenticator.length);
263 /* ship down the response, accept the server's error/ok indication */
264 suppress_tags = TRUE;
265 result = gen_transact(sock, buf1, strlen(buf1));
266 suppress_tags = FALSE;
272 #endif /* KERBEROS_V4 */
274 /* kerberos.c ends here */