]> Pileus Git - ~andy/fetchmail/blob - kerberos.c
ab8c72958f9b67a347e4e85737bc775aa98ed094
[~andy/fetchmail] / kerberos.c
1 /*
2  * kerberos.c -- Kerberos authentication (see RFC 1731).
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6 #include  "config.h"
7 #include  <stdio.h>
8 #include  <string.h>
9 #include  <ctype.h>
10 #if defined(STDC_HEADERS)
11 #include  <stdlib.h>
12 #endif
13 #include  "fetchmail.h"
14 #include  "socket.h"
15
16 #include <netinet/in.h>  /* for htonl/ntohl */
17
18 #ifdef KERBEROS_V4
19
20 #  ifdef KERBEROS_V5
21 #    include <kerberosIV/des.h>
22 #    include <kerberosIV/krb.h>
23 #  else
24 #    if defined (__bsdi__)
25 #       include <des.h>
26 #       define krb_get_err_text(e) (krb_err_txt[e])
27 #    endif
28 #    if defined(__NetBSD__) || (__FreeBSD__) || defined(__linux__)
29 #       define krb_get_err_text(e) (krb_err_txt[e])
30 #    endif
31 #    include <krb.h>
32 #  endif
33
34 /* des.h might define _ for no good reason.  */
35 #undef _
36 #include  "i18n.h"
37
38 #if SIZEOF_INT == 4
39 typedef int     int32;
40 #elif SIZEOF_SHORT == 4
41 typedef short   int32;
42 #elif SIZEOF_LONG == 4
43 typedef long    int32;
44 #else
45 #error Cannot deduce a 32-bit-type
46 #endif
47
48 int do_rfc1731(int sock, char *command, char *truename)
49 /* authenticate as per RFC1731 -- note 32-bit integer requirement here */
50 {
51     int result = 0, len;
52     char buf1[4096], buf2[4096];
53     union {
54       int32 cint;
55       char cstr[4];
56     } challenge1, challenge2;
57     char srvinst[INST_SZ];
58     char *p;
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];
65     des_cblock session;
66     des_key_schedule schedule;
67
68     gen_send(sock, "%s KERBEROS_V4", command);
69
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.
79      */
80
81     if (result = gen_recv(sock, buf1, sizeof buf1)) {
82         return result;
83     }
84
85     len = from64tobits(challenge1.cstr, buf1);
86     if (len < 0) {
87         report(stderr, _("could not decode initial BASE64 challenge\n"));
88         return PS_AUTHFAIL;
89     }
90
91     /* this patch by Dan Root <dar@thekeep.org> solves an endianess
92      * problem. */
93     {
94         char tmp[4];
95
96         *(int *)tmp = ntohl(*(int *) challenge1.cstr);
97         memcpy(challenge1.cstr, tmp, sizeof(tmp));
98     }
99
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.
107      */
108
109     strncpy(srvinst, truename, (sizeof srvinst)-1);
110     srvinst[(sizeof srvinst)-1] = '\0';
111     for (p = srvinst; *p; p++) {
112       if (isupper(*p)) {
113         *p = tolower(*p);
114       }
115     }
116
117     strncpy(srvrealm, (char *)krb_realmofhost(srvinst), (sizeof srvrealm)-1);
118     srvrealm[(sizeof srvrealm)-1] = '\0';
119     if (p = strchr(srvinst, '.')) {
120       *p = '\0';
121     }
122
123     result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm, 0);
124     if (result) {
125         report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
126         return PS_AUTHFAIL;
127     }
128
129     result = krb_get_cred("imap", srvinst, srvrealm, &credentials);
130     if (result) {
131         report(stderr, "krb_get_cred: %s\n", krb_get_err_text(result));
132         return PS_AUTHFAIL;
133     }
134
135     memcpy(session, credentials.session, sizeof session);
136     memset(&credentials, 0, sizeof credentials);
137     des_key_sched(&session, schedule);
138
139     result = krb_get_tf_fullname(TKT_FILE, tktuser, tktinst, tktrealm);
140     if (result) {
141         report(stderr, "krb_get_tf_fullname: %s\n", krb_get_err_text(result));
142         return PS_AUTHFAIL;
143     }
144
145 #ifdef __UNUSED__
146     /*
147      * Andrew H. Chatham <andrew.chatham@duke.edu> alleges that this check
148      * is not necessary and has consistently been messing him up.
149      */
150     if (strcmp(tktuser, user) != 0) {
151         report(stderr, 
152                _("principal %s in ticket does not match -u %s\n"), tktuser,
153                 user);
154         return PS_AUTHFAIL;
155     }
156 #endif /* __UNUSED__ */
157
158     if (tktinst[0]) {
159         report(stderr, 
160                _("non-null instance (%s) might cause strange behavior\n"),
161                 tktinst);
162         strcat(tktuser, ".");
163         strcat(tktuser, tktinst);
164     }
165
166     if (strcmp(tktrealm, srvrealm) != 0) {
167         strcat(tktuser, "@");
168         strcat(tktuser, tktrealm);
169     }
170
171     result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm,
172             challenge1.cint);
173     if (result) {
174         report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
175         return PS_AUTHFAIL;
176     }
177
178     to64frombits(buf1, authenticator.dat, authenticator.length);
179     if (outlevel >= O_MONITOR) {
180         report(stdout, "IMAP> %s\n", buf1);
181     }
182     strcat(buf1, "\r\n");
183     SockWrite(sock, buf1, strlen(buf1));
184
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.
200      */
201     
202     if (result = gen_recv(sock, buf1, sizeof buf1))
203         return result;
204
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.
222      */
223
224     len = from64tobits(buf2, buf1);
225     if (len < 0) {
226         report(stderr, _("could not decode BASE64 ready response\n"));
227         return PS_AUTHFAIL;
228     }
229
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"));
234         return PS_AUTHFAIL;
235     }       
236
237     memset(authenticator.dat, 0, sizeof authenticator.dat);
238
239     result = htonl(challenge1.cint);
240     memcpy(authenticator.dat, &result, sizeof result);
241
242     /* The protection mechanisms and their corresponding bit-masks are as
243      * follows:
244      *
245      * 1 No protection mechanism
246      * 2 Integrity (krb_mk_safe) protection
247      * 4 Privacy (krb_mk_priv) protection
248      */
249     authenticator.dat[4] = 1;
250
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++;
256     }
257     des_pcbc_encrypt((des_cblock *)authenticator.dat,
258             (des_cblock *)authenticator.dat, authenticator.length, schedule,
259             &session, 1);
260
261     to64frombits(buf1, authenticator.dat, authenticator.length);
262
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;
267     if (result)
268         return(result);
269     else
270         return(PS_SUCCESS);
271 }
272 #endif /* KERBEROS_V4 */
273
274 /* kerberos.c ends here */
275