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 <sys/types.h>
17 #include <netinet/in.h> /* for htonl/ntohl */
22 # include <kerberosIV/des.h>
23 # include <kerberosIV/krb.h>
25 # if defined (__bsdi__)
27 # define krb_get_err_text(e) (krb_err_txt[e])
29 # if defined(__NetBSD__) || (__FreeBSD__) || defined(__linux__)
30 # define krb_get_err_text(e) (krb_err_txt[e])
35 /* des.h might define _ for no good reason. */
41 #elif SIZEOF_SHORT == 4
43 #elif SIZEOF_LONG == 4
46 #error Cannot deduce a 32-bit-type
49 int do_rfc1731(int sock, char *command, char *truename)
50 /* authenticate as per RFC1731 -- note 32-bit integer requirement here */
53 char buf1[4096], buf2[4096];
57 } challenge1, challenge2;
58 char srvinst[INST_SZ];
60 char srvrealm[REALM_SZ];
61 KTEXT_ST authenticator;
62 CREDENTIALS credentials;
63 char tktuser[MAX_K_NAME_SZ+1+INST_SZ+1+REALM_SZ+1];
64 char tktinst[INST_SZ];
65 char tktrealm[REALM_SZ];
67 des_key_schedule schedule;
69 gen_send(sock, "%s KERBEROS_V4", command);
71 /* The data encoded in the first ready response contains a random
72 * 32-bit number in network byte order. The client should respond
73 * with a Kerberos ticket and an authenticator for the principal
74 * "imap.hostname@realm", where "hostname" is the first component
75 * of the host name of the server with all letters in lower case
76 * and where "realm" is the Kerberos realm of the server. The
77 * encrypted checksum field included within the Kerberos
78 * authenticator should contain the server provided 32-bit number
79 * in network byte order.
82 if (result = gen_recv(sock, buf1, sizeof buf1)) {
86 len = from64tobits(challenge1.cstr, buf1);
88 report(stderr, _("could not decode initial BASE64 challenge\n"));
92 /* this patch by Dan Root <dar@thekeep.org> solves an endianess
97 *(int *)tmp = ntohl(*(int *) challenge1.cstr);
98 memcpy(challenge1.cstr, tmp, sizeof(tmp));
101 /* Client responds with a Kerberos ticket and an authenticator for
102 * the principal "imap.hostname@realm" where "hostname" is the
103 * first component of the host name of the server with all letters
104 * in lower case and where "realm" is the Kerberos realm of the
105 * server. The encrypted checksum field included within the
106 * Kerberos authenticator should contain the server-provided
107 * 32-bit number in network byte order.
110 strncpy(srvinst, truename, (sizeof srvinst)-1);
111 srvinst[(sizeof srvinst)-1] = '\0';
112 for (p = srvinst; *p; p++) {
118 strncpy(srvrealm, (char *)krb_realmofhost(srvinst), (sizeof srvrealm)-1);
119 srvrealm[(sizeof srvrealm)-1] = '\0';
120 if (p = strchr(srvinst, '.')) {
124 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm, 0);
126 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
130 result = krb_get_cred("imap", srvinst, srvrealm, &credentials);
132 report(stderr, "krb_get_cred: %s\n", krb_get_err_text(result));
136 memcpy(session, credentials.session, sizeof session);
137 memset(&credentials, 0, sizeof credentials);
138 des_key_sched(session, schedule);
140 result = krb_get_tf_fullname(TKT_FILE, tktuser, tktinst, tktrealm);
142 report(stderr, "krb_get_tf_fullname: %s\n", krb_get_err_text(result));
148 * Andrew H. Chatham <andrew.chatham@duke.edu> alleges that this check
149 * is not necessary and has consistently been messing him up.
151 if (strcmp(tktuser, user) != 0) {
153 _("principal %s in ticket does not match -u %s\n"), tktuser,
157 #endif /* __UNUSED__ */
161 _("non-null instance (%s) might cause strange behavior\n"),
163 strcat(tktuser, ".");
164 strcat(tktuser, tktinst);
167 if (strcmp(tktrealm, srvrealm) != 0) {
168 strcat(tktuser, "@");
169 strcat(tktuser, tktrealm);
172 result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm,
175 report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
179 to64frombits(buf1, authenticator.dat, authenticator.length);
180 if (outlevel >= O_MONITOR) {
181 report(stdout, "IMAP> %s\n", buf1);
183 strcat(buf1, "\r\n");
184 SockWrite(sock, buf1, strlen(buf1));
186 /* Upon decrypting and verifying the ticket and authenticator, the
187 * server should verify that the contained checksum field equals
188 * the original server provided random 32-bit number. Should the
189 * verification be successful, the server must add one to the
190 * checksum and construct 8 octets of data, with the first four
191 * octets containing the incremented checksum in network byte
192 * order, the fifth octet containing a bit-mask specifying the
193 * protection mechanisms supported by the server, and the sixth
194 * through eighth octets containing, in network byte order, the
195 * maximum cipher-text buffer size the server is able to receive.
196 * The server must encrypt the 8 octets of data in the session key
197 * and issue that encrypted data in a second ready response. The
198 * client should consider the server authenticated if the first
199 * four octets the un-encrypted data is equal to one plus the
200 * checksum it previously sent.
203 if (result = gen_recv(sock, buf1, sizeof buf1))
206 /* The client must construct data with the first four octets
207 * containing the original server-issued checksum in network byte
208 * order, the fifth octet containing the bit-mask specifying the
209 * selected protection mechanism, the sixth through eighth octets
210 * containing in network byte order the maximum cipher-text buffer
211 * size the client is able to receive, and the following octets
212 * containing a user name string. The client must then append
213 * from one to eight octets so that the length of the data is a
214 * multiple of eight octets. The client must then PCBC encrypt the
215 * data with the session key and respond to the second ready
216 * response with the encrypted data. The server decrypts the data
217 * and verifies the contained checksum. The username field
218 * identifies the user for whom subsequent IMAP operations are to
219 * be performed; the server must verify that the principal
220 * identified in the Kerberos ticket is authorized to connect as
221 * that user. After these verifications, the authentication
222 * process is complete.
225 len = from64tobits(buf2, buf1);
227 report(stderr, _("could not decode BASE64 ready response\n"));
231 des_ecb_encrypt((des_cblock *)buf2, (des_cblock *)buf2, schedule, 0);
232 memcpy(challenge2.cstr, buf2, 4);
233 if (ntohl(challenge2.cint) != challenge1.cint + 1) {
234 report(stderr, _("challenge mismatch\n"));
238 memset(authenticator.dat, 0, sizeof authenticator.dat);
240 result = htonl(challenge1.cint);
241 memcpy(authenticator.dat, &result, sizeof result);
243 /* The protection mechanisms and their corresponding bit-masks are as
246 * 1 No protection mechanism
247 * 2 Integrity (krb_mk_safe) protection
248 * 4 Privacy (krb_mk_priv) protection
250 authenticator.dat[4] = 1;
252 len = strlen(tktuser);
253 strncpy(authenticator.dat+8, tktuser, len);
254 authenticator.length = len + 8 + 1;
255 while (authenticator.length & 7) {
256 authenticator.length++;
258 des_pcbc_encrypt((des_cblock *)authenticator.dat,
259 (des_cblock *)authenticator.dat, authenticator.length, schedule,
262 to64frombits(buf1, authenticator.dat, authenticator.length);
264 /* ship down the response, accept the server's error/ok indication */
265 suppress_tags = TRUE;
266 result = gen_transact(sock, buf1, strlen(buf1));
267 suppress_tags = FALSE;
273 #endif /* KERBEROS_V4 */
275 /* kerberos.c ends here */