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