configure fixes.
[apps/madmutt.git] / imap / auth_gss.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 Brendan Cully <brendan@kublai.com>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 /* GSS login/authentication code */
11
12 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef USE_GSS
17
18 #include <lib-lib/macros.h>
19
20 #include <netinet/in.h>
21
22 #ifdef HAVE_HEIMDAL
23 #  include <gssapi/gssapi.h>
24 #  define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
25 #else
26 #  include <gssapi/gssapi.h>
27 #  include <gssapi/gssapi_generic.h>
28 #endif
29
30 #define GSS_BUFSIZE 8192
31
32 #define GSS_AUTH_P_NONE      1
33 #define GSS_AUTH_P_INTEGRITY 2
34 #define GSS_AUTH_P_PRIVACY   4
35
36 #include "mutt.h"
37 #include "imap_private.h"
38 #include "auth.h"
39
40 /* imap_auth_gss: AUTH=GSSAPI support. */
41 imap_auth_res_t imap_auth_gss (IMAP_DATA * idata, const char *method __attribute__ ((unused)))
42 {
43   gss_buffer_desc request_buf, send_token;
44   gss_buffer_t sec_token;
45   gss_name_t target_name;
46   gss_ctx_id_t context;
47   gss_OID mech_name;
48   gss_qop_t quality;
49   int cflags;
50   OM_uint32 maj_stat, min_stat;
51   char buf1[GSS_BUFSIZE], buf2[GSS_BUFSIZE], server_conf_flags;
52   unsigned long buf_size;
53   int rc;
54
55   if (!mutt_bit_isset (idata->capabilities, AGSSAPI))
56     return IMAP_AUTH_UNAVAIL;
57
58   if (mutt_account_getuser (&idata->conn->account))
59     return IMAP_AUTH_FAILURE;
60
61   /* get an IMAP service ticket for the server */
62   snprintf (buf1, sizeof (buf1), "imap@%s", idata->conn->account.host);
63   request_buf.value = buf1;
64   request_buf.length = m_strlen(buf1) + 1;
65   maj_stat = gss_import_name (&min_stat, &request_buf, gss_nt_service_name,
66                               &target_name);
67   if (maj_stat != GSS_S_COMPLETE) {
68     return IMAP_AUTH_UNAVAIL;
69   }
70
71   /* Acquire initial credentials - without a TGT GSSAPI is UNAVAIL */
72   sec_token = GSS_C_NO_BUFFER;
73   context = GSS_C_NO_CONTEXT;
74
75   /* build token */
76   maj_stat = gss_init_sec_context (&min_stat, GSS_C_NO_CREDENTIAL, &context,
77                                    target_name, GSS_C_NO_OID,
78                                    GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG, 0,
79                                    GSS_C_NO_CHANNEL_BINDINGS, sec_token, NULL,
80                                    &send_token, (unsigned int *) &cflags,
81                                    NULL);
82   if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
83     gss_release_name (&min_stat, &target_name);
84
85     return IMAP_AUTH_UNAVAIL;
86   }
87
88   /* now begin login */
89   mutt_message _("Authenticating (GSSAPI)...");
90
91   imap_cmd_start (idata, "AUTHENTICATE GSSAPI");
92
93   /* expect a null continuation response ("+") */
94   do
95     rc = imap_cmd_step (idata);
96   while (rc == IMAP_CMD_CONTINUE);
97
98   if (rc != IMAP_CMD_RESPOND) {
99     gss_release_name (&min_stat, &target_name);
100     goto bail;
101   }
102
103   /* now start the security context initialisation loop... */
104   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
105                   sizeof (buf1) - 2);
106   gss_release_buffer (&min_stat, &send_token);
107   m_strcat(buf1, sizeof(buf1), "\r\n");
108   mutt_socket_write (idata->conn, buf1);
109
110   while (maj_stat == GSS_S_CONTINUE_NEEDED) {
111     /* Read server data */
112     do
113       rc = imap_cmd_step (idata);
114     while (rc == IMAP_CMD_CONTINUE);
115
116     if (rc != IMAP_CMD_RESPOND) {
117       gss_release_name (&min_stat, &target_name);
118       goto bail;
119     }
120
121     request_buf.length = mutt_from_base64 (buf2, idata->cmd.buf + 2);
122     request_buf.value = buf2;
123     sec_token = &request_buf;
124
125     /* Write client data */
126     maj_stat = gss_init_sec_context (&min_stat, GSS_C_NO_CREDENTIAL, &context,
127                                      target_name, GSS_C_NO_OID,
128                                      GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
129                                      0, GSS_C_NO_CHANNEL_BINDINGS, sec_token,
130                                      NULL, &send_token,
131                                      (unsigned int *) &cflags, NULL);
132     if (maj_stat != GSS_S_COMPLETE && maj_stat != GSS_S_CONTINUE_NEEDED) {
133       gss_release_name (&min_stat, &target_name);
134
135       goto err_abort_cmd;
136     }
137     mutt_to_base64 ((unsigned char *) buf1, send_token.value,
138                     send_token.length, sizeof (buf1) - 2);
139     gss_release_buffer (&min_stat, &send_token);
140     m_strcat(buf1, sizeof(buf1), "\r\n");
141     mutt_socket_write (idata->conn, buf1);
142   }
143
144   gss_release_name (&min_stat, &target_name);
145
146   /* get security flags and buffer size */
147   do
148     rc = imap_cmd_step (idata);
149   while (rc == IMAP_CMD_CONTINUE);
150
151   if (rc != IMAP_CMD_RESPOND) {
152     goto bail;
153   }
154   request_buf.length = mutt_from_base64 (buf2, idata->cmd.buf + 2);
155   request_buf.value = buf2;
156
157   maj_stat = gss_unwrap (&min_stat, context, &request_buf, &send_token,
158                          &cflags, &quality);
159   if (maj_stat != GSS_S_COMPLETE) {
160     gss_release_buffer (&min_stat, &send_token);
161     goto err_abort_cmd;
162   }
163
164   /* first octet is security levels supported. We want NONE */
165   server_conf_flags = ((char *) send_token.value)[0];
166   if (!(((char *) send_token.value)[0] & GSS_AUTH_P_NONE)) {
167     gss_release_buffer (&min_stat, &send_token);
168     goto err_abort_cmd;
169   }
170
171   /* we don't care about buffer size if we don't wrap content. But here it is */
172   ((char *) send_token.value)[0] = 0;
173   buf_size = ntohl (*((long *) send_token.value));
174   gss_release_buffer (&min_stat, &send_token);
175
176   /* agree to terms (hack!) */
177   buf_size = htonl (buf_size);  /* not relevant without integrity/privacy */
178   memcpy (buf1, &buf_size, 4);
179   buf1[0] = GSS_AUTH_P_NONE;
180   /* server decides if principal can log in as user */
181   strncpy (buf1 + 4, idata->conn->account.user, sizeof (buf1) - 4);
182   request_buf.value = buf1;
183   request_buf.length = 4 + m_strlen(idata->conn->account.user) + 1;
184   maj_stat = gss_wrap (&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
185                        &cflags, &send_token);
186   if (maj_stat != GSS_S_COMPLETE) {
187     goto err_abort_cmd;
188   }
189
190   mutt_to_base64 ((unsigned char *) buf1, send_token.value, send_token.length,
191                   sizeof (buf1) - 2);
192   m_strcat(buf1, sizeof(buf1), "\r\n");
193   mutt_socket_write (idata->conn, buf1);
194
195   /* Joy of victory or agony of defeat? */
196   do
197     rc = imap_cmd_step (idata);
198   while (rc == IMAP_CMD_CONTINUE);
199   if (rc == IMAP_CMD_RESPOND) {
200     goto err_abort_cmd;
201   }
202   if (imap_code (idata->cmd.buf)) {
203     /* flush the security context */
204     maj_stat = gss_delete_sec_context (&min_stat, &context, &send_token);
205
206     /* send_token may contain a notification to the server to flush
207      * credentials. RFC 1731 doesn't specify what to do, and since this
208      * support is only for authentication, we'll assume the server knows
209      * enough to flush its own credentials */
210     gss_release_buffer (&min_stat, &send_token);
211
212     return IMAP_AUTH_SUCCESS;
213   }
214   else
215     goto bail;
216
217 err_abort_cmd:
218   mutt_socket_write (idata->conn, "*\r\n");
219   do
220     rc = imap_cmd_step (idata);
221   while (rc == IMAP_CMD_CONTINUE);
222
223 bail:
224   mutt_error _("GSSAPI authentication failed.");
225   mutt_sleep (2);
226   return IMAP_AUTH_FAILURE;
227 }
228
229 #endif /* USE_GSS */