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