NeoMutt  2021-10-29-43-g6b8931
Teaching an old dog new tricks
DOXYGEN
auth_gss.c
Go to the documentation of this file.
1 
36 #include "config.h"
37 #include <netinet/in.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include "private.h"
41 #include "mutt/lib.h"
42 #include "config/lib.h"
43 #include "core/lib.h"
44 #include "conn/lib.h"
45 #include "adata.h"
46 #include "auth.h"
47 #include "mutt_socket.h"
48 #ifdef HAVE_HEIMDAL
49 #include <gssapi/gssapi.h>
50 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
51 #else
52 #include <gssapi/gssapi.h>
53 #include <gssapi/gssapi_generic.h>
54 #endif
55 
56 #define GSS_AUTH_P_NONE 1
57 #define GSS_AUTH_P_INTEGRITY 2
58 #define GSS_AUTH_P_PRIVACY 4
59 
65 static void print_gss_error(OM_uint32 err_maj, OM_uint32 err_min)
66 {
67  OM_uint32 maj_stat, min_stat;
68  OM_uint32 msg_ctx = 0;
69  gss_buffer_desc status_string;
70  char buf_maj[512];
71  char buf_min[512];
72 
73  do
74  {
75  maj_stat = gss_display_status(&min_stat, err_maj, GSS_C_GSS_CODE,
76  GSS_C_NO_OID, &msg_ctx, &status_string);
77  if (GSS_ERROR(maj_stat))
78  break;
79  size_t status_len = status_string.length;
80  if (status_len >= sizeof(buf_maj))
81  status_len = sizeof(buf_maj) - 1;
82  strncpy(buf_maj, (char *) status_string.value, status_len);
83  buf_maj[status_len] = '\0';
84  gss_release_buffer(&min_stat, &status_string);
85 
86  maj_stat = gss_display_status(&min_stat, err_min, GSS_C_MECH_CODE,
87  GSS_C_NULL_OID, &msg_ctx, &status_string);
88  if (!GSS_ERROR(maj_stat))
89  {
90  status_len = status_string.length;
91  if (status_len >= sizeof(buf_min))
92  status_len = sizeof(buf_min) - 1;
93  strncpy(buf_min, (char *) status_string.value, status_len);
94  buf_min[status_len] = '\0';
95  gss_release_buffer(&min_stat, &status_string);
96  }
97  } while (!GSS_ERROR(maj_stat) && (msg_ctx != 0));
98 
99  mutt_debug(LL_DEBUG2, "((%s:%d )(%s:%d))\n", buf_maj, err_maj, buf_min, err_min);
100 }
101 
105 enum ImapAuthRes imap_auth_gss(struct ImapAccountData *adata, const char *method)
106 {
107  gss_buffer_desc request_buf, send_token;
108  gss_buffer_t sec_token;
109  gss_name_t target_name;
110  gss_ctx_id_t context;
111  gss_OID mech_name;
112  char server_conf_flags;
113  gss_qop_t quality;
114  int cflags;
115  OM_uint32 maj_stat, min_stat;
116  unsigned long buf_size;
117  int rc, retval = IMAP_AUTH_FAILURE;
118 
119  if (!(adata->capabilities & IMAP_CAP_AUTH_GSSAPI))
120  return IMAP_AUTH_UNAVAIL;
121 
122  if (mutt_account_getuser(&adata->conn->account) < 0)
123  return IMAP_AUTH_FAILURE;
124 
125  struct Buffer *buf1 = mutt_buffer_pool_get();
126  struct Buffer *buf2 = mutt_buffer_pool_get();
127 
128  /* get an IMAP service ticket for the server */
129  mutt_buffer_printf(buf1, "imap@%s", adata->conn->account.host);
130  request_buf.value = buf1->data;
131  request_buf.length = mutt_buffer_len(buf1);
132 
133  const short c_debug_level = cs_subset_number(NeoMutt->sub, "debug_level");
134  maj_stat = gss_import_name(&min_stat, &request_buf, gss_nt_service_name, &target_name);
135  if (maj_stat != GSS_S_COMPLETE)
136  {
137  mutt_debug(LL_DEBUG2, "Couldn't get service name for [%s]\n", buf1->data);
138  retval = IMAP_AUTH_UNAVAIL;
139  goto cleanup;
140  }
141  else if (c_debug_level >= 2)
142  {
143  gss_display_name(&min_stat, target_name, &request_buf, &mech_name);
144  mutt_debug(LL_DEBUG2, "Using service name [%s]\n", (char *) request_buf.value);
145  gss_release_buffer(&min_stat, &request_buf);
146  }
147  /* Acquire initial credentials - without a TGT GSSAPI is UNAVAIL */
148  sec_token = GSS_C_NO_BUFFER;
149  context = GSS_C_NO_CONTEXT;
150 
151  /* build token */
152  maj_stat = gss_init_sec_context(&min_stat, GSS_C_NO_CREDENTIAL, &context, target_name,
153  GSS_C_NO_OID, GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG,
154  0, GSS_C_NO_CHANNEL_BINDINGS, sec_token, NULL,
155  &send_token, (unsigned int *) &cflags, NULL);
156  if ((maj_stat != GSS_S_COMPLETE) && (maj_stat != GSS_S_CONTINUE_NEEDED))
157  {
158  print_gss_error(maj_stat, min_stat);
159  mutt_debug(LL_DEBUG1, "Error acquiring credentials - no TGT?\n");
160  gss_release_name(&min_stat, &target_name);
161 
162  retval = IMAP_AUTH_UNAVAIL;
163  goto cleanup;
164  }
165 
166  /* now begin login */
167  // L10N: (%s) is the method name, e.g. Anonymous, CRAM-MD5, GSSAPI, SASL
168  mutt_message(_("Authenticating (%s)..."), "GSSAPI");
169 
170  imap_cmd_start(adata, "AUTHENTICATE GSSAPI");
171 
172  /* expect a null continuation response ("+") */
173  do
174  {
175  rc = imap_cmd_step(adata);
176  } while (rc == IMAP_RES_CONTINUE);
177 
178  if (rc != IMAP_RES_RESPOND)
179  {
180  mutt_debug(LL_DEBUG2, "Invalid response from server: %s\n", buf1->data);
181  gss_release_name(&min_stat, &target_name);
182  goto bail;
183  }
184 
185  /* now start the security context initialisation loop... */
186  mutt_debug(LL_DEBUG2, "Sending credentials\n");
187  mutt_b64_buffer_encode(buf1, send_token.value, send_token.length);
188  gss_release_buffer(&min_stat, &send_token);
189  mutt_buffer_addstr(buf1, "\r\n");
191 
192  while (maj_stat == GSS_S_CONTINUE_NEEDED)
193  {
194  /* Read server data */
195  do
196  {
197  rc = imap_cmd_step(adata);
198  } while (rc == IMAP_RES_CONTINUE);
199 
200  if (rc != IMAP_RES_RESPOND)
201  {
202  mutt_debug(LL_DEBUG1, "#1 Error receiving server response\n");
203  gss_release_name(&min_stat, &target_name);
204  goto bail;
205  }
206 
207  if (mutt_b64_buffer_decode(buf2, adata->buf + 2) < 0)
208  {
209  mutt_debug(LL_DEBUG1, "Invalid base64 server response\n");
210  gss_release_name(&min_stat, &target_name);
211  goto err_abort_cmd;
212  }
213  request_buf.value = buf2->data;
214  request_buf.length = mutt_buffer_len(buf2);
215  sec_token = &request_buf;
216 
217  /* Write client data */
218  maj_stat = gss_init_sec_context(
219  &min_stat, GSS_C_NO_CREDENTIAL, &context, target_name, GSS_C_NO_OID,
220  GSS_C_MUTUAL_FLAG | GSS_C_SEQUENCE_FLAG, 0, GSS_C_NO_CHANNEL_BINDINGS,
221  sec_token, NULL, &send_token, (unsigned int *) &cflags, NULL);
222  if ((maj_stat != GSS_S_COMPLETE) && (maj_stat != GSS_S_CONTINUE_NEEDED))
223  {
224  print_gss_error(maj_stat, min_stat);
225  mutt_debug(LL_DEBUG1, "Error exchanging credentials\n");
226  gss_release_name(&min_stat, &target_name);
227 
228  goto err_abort_cmd;
229  }
230  mutt_b64_buffer_encode(buf1, send_token.value, send_token.length);
231  gss_release_buffer(&min_stat, &send_token);
232  mutt_buffer_addstr(buf1, "\r\n");
234  }
235 
236  gss_release_name(&min_stat, &target_name);
237 
238  /* get security flags and buffer size */
239  do
240  {
241  rc = imap_cmd_step(adata);
242  } while (rc == IMAP_RES_CONTINUE);
243 
244  if (rc != IMAP_RES_RESPOND)
245  {
246  mutt_debug(LL_DEBUG1, "#2 Error receiving server response\n");
247  goto bail;
248  }
249  if (mutt_b64_buffer_decode(buf2, adata->buf + 2) < 0)
250  {
251  mutt_debug(LL_DEBUG1, "Invalid base64 server response\n");
252  goto err_abort_cmd;
253  }
254  request_buf.value = buf2->data;
255  request_buf.length = mutt_buffer_len(buf2);
256 
257  maj_stat = gss_unwrap(&min_stat, context, &request_buf, &send_token, &cflags, &quality);
258  if (maj_stat != GSS_S_COMPLETE)
259  {
260  print_gss_error(maj_stat, min_stat);
261  mutt_debug(LL_DEBUG2, "Couldn't unwrap security level data\n");
262  gss_release_buffer(&min_stat, &send_token);
263  goto err_abort_cmd;
264  }
265  mutt_debug(LL_DEBUG2, "Credential exchange complete\n");
266 
267  /* first octet is security levels supported. We want NONE */
268  server_conf_flags = ((char *) send_token.value)[0];
269  if (!(((char *) send_token.value)[0] & GSS_AUTH_P_NONE))
270  {
271  mutt_debug(LL_DEBUG2, "Server requires integrity or privacy\n");
272  gss_release_buffer(&min_stat, &send_token);
273  goto err_abort_cmd;
274  }
275 
276  /* we don't care about buffer size if we don't wrap content. But here it is */
277  ((char *) send_token.value)[0] = '\0';
278  buf_size = ntohl(*((long *) send_token.value));
279  gss_release_buffer(&min_stat, &send_token);
280  mutt_debug(LL_DEBUG2, "Unwrapped security level flags: %c%c%c\n",
281  (server_conf_flags & GSS_AUTH_P_NONE) ? 'N' : '-',
282  (server_conf_flags & GSS_AUTH_P_INTEGRITY) ? 'I' : '-',
283  (server_conf_flags & GSS_AUTH_P_PRIVACY) ? 'P' : '-');
284  mutt_debug(LL_DEBUG2, "Maximum GSS token size is %ld\n", buf_size);
285 
286  /* agree to terms (hack!) */
287  buf_size = htonl(buf_size); /* not relevant without integrity/privacy */
288  mutt_buffer_reset(buf1);
290  mutt_buffer_addstr_n(buf1, ((char *) &buf_size) + 1, 3);
291  /* server decides if principal can log in as user */
292  mutt_buffer_addstr(buf1, adata->conn->account.user);
293  request_buf.value = buf1->data;
294  request_buf.length = mutt_buffer_len(buf1);
295  maj_stat = gss_wrap(&min_stat, context, 0, GSS_C_QOP_DEFAULT, &request_buf,
296  &cflags, &send_token);
297  if (maj_stat != GSS_S_COMPLETE)
298  {
299  mutt_debug(LL_DEBUG2, "Error creating login request\n");
300  goto err_abort_cmd;
301  }
302 
303  mutt_b64_buffer_encode(buf1, send_token.value, send_token.length);
304  mutt_debug(LL_DEBUG2, "Requesting authorisation as %s\n", adata->conn->account.user);
305  mutt_buffer_addstr(buf1, "\r\n");
307 
308  /* Joy of victory or agony of defeat? */
309  do
310  {
311  rc = imap_cmd_step(adata);
312  } while (rc == IMAP_RES_CONTINUE);
313  if (rc == IMAP_RES_RESPOND)
314  {
315  mutt_debug(LL_DEBUG1, "Unexpected server continuation request\n");
316  goto err_abort_cmd;
317  }
318  if (imap_code(adata->buf))
319  {
320  /* flush the security context */
321  mutt_debug(LL_DEBUG2, "Releasing GSS credentials\n");
322  maj_stat = gss_delete_sec_context(&min_stat, &context, &send_token);
323  if (maj_stat != GSS_S_COMPLETE)
324  mutt_debug(LL_DEBUG1, "Error releasing credentials\n");
325 
326  /* send_token may contain a notification to the server to flush
327  * credentials. RFC1731 doesn't specify what to do, and since this
328  * support is only for authentication, we'll assume the server knows
329  * enough to flush its own credentials */
330  gss_release_buffer(&min_stat, &send_token);
331 
332  retval = IMAP_AUTH_SUCCESS;
333  goto cleanup;
334  }
335  else
336  goto bail;
337 
338 err_abort_cmd:
339  mutt_socket_send(adata->conn, "*\r\n");
340  do
341  {
342  rc = imap_cmd_step(adata);
343  } while (rc == IMAP_RES_CONTINUE);
344 
345 bail:
346  // L10N: %s is the method name, e.g. Anonymous, CRAM-MD5, GSSAPI, SASL
347  mutt_error(_("%s authentication failed"), "GSSAPI");
348  retval = IMAP_AUTH_FAILURE;
349 
350 cleanup:
353 
354  return retval;
355 }
IMAP authenticator multiplexor.
ImapAuthRes
Results of IMAP Authentication.
Definition: auth.h:38
@ IMAP_AUTH_FAILURE
Authentication failed.
Definition: auth.h:40
@ IMAP_AUTH_SUCCESS
Authentication successful.
Definition: auth.h:39
@ IMAP_AUTH_UNAVAIL
Authentication method not permitted.
Definition: auth.h:41
enum ImapAuthRes imap_auth_gss(struct ImapAccountData *adata, const char *method)
GSS Authentication support - Implements ImapAuth::authenticate()
Definition: auth_gss.c:105
#define GSS_AUTH_P_NONE
Definition: auth_gss.c:56
static void print_gss_error(OM_uint32 err_maj, OM_uint32 err_min)
Print detailed error message to the debug log.
Definition: auth_gss.c:65
#define GSS_AUTH_P_PRIVACY
Definition: auth_gss.c:58
#define GSS_AUTH_P_INTEGRITY
Definition: auth_gss.c:57
size_t mutt_b64_buffer_encode(struct Buffer *buf, const char *in, size_t len)
Convert raw bytes to null-terminated base64 string.
Definition: base64.c:191
int mutt_b64_buffer_decode(struct Buffer *buf, const char *in)
Convert null-terminated base64 string to raw bytes.
Definition: base64.c:209
size_t mutt_buffer_addstr_n(struct Buffer *buf, const char *s, size_t len)
Add a string to a Buffer, expanding it if necessary.
Definition: buffer.c:99
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:356
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
Convenience wrapper for the config headers.
Connection Library.
int mutt_account_getuser(struct ConnAccount *cac)
Retrieve username into ConnAccount, if necessary.
Definition: connaccount.c:48
Convenience wrapper for the core headers.
#define mutt_error(...)
Definition: logging.h:87
#define mutt_message(...)
Definition: logging.h:86
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:169
int imap_cmd_start(struct ImapAccountData *adata, const char *cmdstr)
Given an IMAP command, send it to the server.
Definition: command.c:1070
int imap_cmd_step(struct ImapAccountData *adata)
Reads server responses from an IMAP command.
Definition: command.c:1084
bool imap_code(const char *s)
Was the command successful.
Definition: command.c:1207
#define IMAP_RES_RESPOND
+
Definition: private.h:58
#define IMAP_CAP_AUTH_GSSAPI
RFC1731: GSSAPI authentication.
Definition: private.h:129
#define IMAP_RES_CONTINUE
* ...
Definition: private.h:57
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
NeoMutt connections.
#define mutt_socket_send(conn, buf)
Definition: mutt_socket.h:37
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
Pop-specific Account data.
GUI display the mailboxes in a side panel.
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
char user[128]
Username.
Definition: connaccount.h:56
char host[128]
Server to login to.
Definition: connaccount.h:54
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
IMAP-specific Account data -.
Definition: adata.h:40
ImapCapFlags capabilities
Capability flags.
Definition: adata.h:55
char * buf
Definition: adata.h:59
struct Connection * conn
Connection to IMAP server.
Definition: adata.h:41
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39