]> Pileus Git - ~andy/fetchmail/blob - kerberos.c
Note Earl's regression fix for SSL_CTX_clear_options() on older OpenSSL.
[~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
8 #ifdef KERBEROS_V4
9
10 #include  <stdio.h>
11 #include  <string.h>
12 #include  <ctype.h>
13 #if defined(STDC_HEADERS)
14 #include  <stdlib.h>
15 #endif
16 #include  "fetchmail.h"
17 #include  "socket.h"
18 #include  "kerberos.h"
19
20 #include <sys/types.h>
21 #include <netinet/in.h>  /* for htonl/ntohl */
22
23 #include  "i18n.h"
24
25 #if SIZEOF_INT == 4
26 typedef int     int32;
27 #elif SIZEOF_SHORT == 4
28 typedef short   int32;
29 #elif SIZEOF_LONG == 4
30 typedef long    int32;
31 #else
32 #error Cannot deduce a 32-bit-type
33 #endif
34
35 int do_rfc1731(int sock, const char *command, const char *truename)
36 /* authenticate as per RFC1731 -- note 32-bit integer requirement here */
37 {
38     int result = 0, len;
39     char buf1[4096], buf2[4096];
40     union {
41       int32 cint;
42       char cstr[4];
43     } challenge1, challenge2;
44     char srvinst[INST_SZ];
45     char *p;
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];
52     des_cblock session;
53     des_key_schedule schedule;
54
55     gen_send(sock, "%s KERBEROS_V4", command);
56
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.
66      */
67
68     if ((result = gen_recv(sock, buf1, sizeof buf1)) != 0) {
69         return result;
70     }
71
72     len = from64tobits(challenge1.cstr, buf1, sizeof(challenge1.cstr));
73     if (len < 0) {
74         report(stderr, GT_("could not decode initial BASE64 challenge\n"));
75         return PS_AUTHFAIL;
76     }
77
78     /* this patch by Dan Root <dar@thekeep.org> solves an endianess
79      * problem. */
80     {
81         char tmp[4];
82
83         *(int *)tmp = ntohl(*(int *) challenge1.cstr);
84         memcpy(challenge1.cstr, tmp, sizeof(tmp));
85     }
86
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.
94      */
95
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);
101       }
102     }
103
104     strncpy(srvrealm, (char *)krb_realmofhost(srvinst), (sizeof srvrealm)-1);
105     srvrealm[(sizeof srvrealm)-1] = '\0';
106     if ((p = strchr(srvinst, '.')) != NULL) {
107       *p = '\0';
108     }
109
110     result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm, 0);
111     if (result) {
112         report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
113         return PS_AUTHFAIL;
114     }
115
116     result = krb_get_cred("imap", srvinst, srvrealm, &credentials);
117     if (result) {
118         report(stderr, "krb_get_cred: %s\n", krb_get_err_text(result));
119         return PS_AUTHFAIL;
120     }
121
122     memcpy(session, credentials.session, sizeof session);
123     memset(&credentials, 0, sizeof credentials);
124     des_key_sched(&session, schedule);
125
126     result = krb_get_tf_fullname(TKT_FILE, tktuser, tktinst, tktrealm);
127     if (result) {
128         report(stderr, "krb_get_tf_fullname: %s\n", krb_get_err_text(result));
129         return PS_AUTHFAIL;
130     }
131
132 #ifdef __UNUSED__
133     /*
134      * Andrew H. Chatham <andrew.chatham@duke.edu> alleges that this check
135      * is not necessary and has consistently been messing him up.
136      */
137     if (strcmp(tktuser, user) != 0) {
138         report(stderr, 
139                GT_("principal %s in ticket does not match -u %s\n"), tktuser,
140                 user);
141         return PS_AUTHFAIL;
142     }
143 #endif /* __UNUSED__ */
144
145     if (tktinst[0]) {
146         report(stderr, 
147                GT_("non-null instance (%s) might cause strange behavior\n"),
148                 tktinst);
149         strlcat(tktuser, ".", sizeof(tktuser));
150         strlcat(tktuser, tktinst, sizeof(tktuser));
151     }
152
153     if (strcmp(tktrealm, srvrealm) != 0) {
154         strlcat(tktuser, "@", sizeof(tktuser));
155         strlcat(tktuser, tktrealm, sizeof(tktuser));
156     }
157
158     result = krb_mk_req(&authenticator, "imap", srvinst, srvrealm,
159             challenge1.cint);
160     if (result) {
161         report(stderr, "krb_mq_req: %s\n", krb_get_err_text(result));
162         return PS_AUTHFAIL;
163     }
164
165     to64frombits(buf1, authenticator.dat, authenticator.length);
166     if (outlevel >= O_MONITOR) {
167         report(stdout, "IMAP> %s\n", buf1);
168     }
169     strcat(buf1, "\r\n");
170     SockWrite(sock, buf1, strlen(buf1));
171
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.
187      */
188     
189     if ((result = gen_recv(sock, buf1, sizeof buf1)) != 0)
190         return result;
191
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.
209      */
210
211     len = from64tobits(buf2, buf1, sizeof(buf2));
212     if (len < 0) {
213         report(stderr, GT_("could not decode BASE64 ready response\n"));
214         return PS_AUTHFAIL;
215     }
216
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"));
221         return PS_AUTHFAIL;
222     }       
223
224     memset(authenticator.dat, 0, sizeof authenticator.dat);
225
226     result = htonl(challenge1.cint);
227     memcpy(authenticator.dat, &result, sizeof result);
228
229     /* The protection mechanisms and their corresponding bit-masks are as
230      * follows:
231      *
232      * 1 No protection mechanism
233      * 2 Integrity (krb_mk_safe) protection
234      * 4 Privacy (krb_mk_priv) protection
235      */
236     authenticator.dat[4] = 1;
237
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++;
243     }
244     des_pcbc_encrypt((const unsigned char *)authenticator.dat,
245             (unsigned char *)authenticator.dat, authenticator.length, schedule,
246             &session, 1);
247
248     to64frombits(buf1, authenticator.dat, authenticator.length);
249
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;
254     if (result)
255         return(result);
256     else
257         return(PS_SUCCESS);
258 }
259 #endif /* KERBEROS_V4 */
260
261 /* kerberos.c ends here */
262