while (r != 0) { if (! (conn->account.flags & M_ACCT_USER)) { if (!ImapUser) { strfcpy (user, NONULL(Username), sizeof (user)); if (mutt_get_field (_("IMAP Username: "), user, sizeof (user), 0) != 0) { user[0] = 0; return (-1); } } else strfcpy (user, ImapUser, sizeof (user)); } else strfcpy (user, conn->account.user, sizeof (user));  if (!user[0]) { if (!mutt_bit_isset (idata->capabilities, AUTH_ANON)) { mutt_error _("Anonymous authentication not supported."); return -1; }  return imap_auth_anon (idata); }  #ifdef USE_GSS  if (mutt_bit_isset (idata->capabilities, AGSSAPI)) { if ((r = imap_auth_gss (idata, user))) { mutt_error _("GSSAPI authentication failed."); sleep (1); } else return 0; } else dprint (2, (debugfile, "GSSAPI authentication is not available\n")); #endif   if (mutt_bit_isset (idata->capabilities, ACRAM_MD5)) { if (!(conn->account.flags & M_ACCT_CRAM)) { if (!ImapCRAMKey) { ckey[0] = '\0'; snprintf (buf, sizeof (buf), _("CRAM key for %s@%s: "), user, conn->account.host); if (mutt_get_field (buf, ckey, sizeof (ckey), M_PASS) != 0) return -1; } else strfcpy (ckey, ImapCRAMKey, sizeof (ckey)); } else strfcpy (ckey, conn->account.pass, sizeof (ckey));  if (*ckey) { if ((r = imap_auth_cram_md5 (idata, user, ckey))) { mutt_error _("CRAM-MD5 authentication failed."); sleep (1); if (!(conn->account.flags & M_ACCT_CRAM)) FREE (&ImapCRAMKey); conn->account.flags &= ~M_ACCT_CRAM; } else { strfcpy (conn->account.pass, ckey, sizeof (conn->account.pass)); conn->account.flags |= M_ACCT_CRAM; return 0; } } else { mutt_message _("Skipping CRAM-MD5 authentication."); sleep (1); } } else dprint (2, (debugfile, "CRAM-MD5 authentication is not available\n"));  if (! (conn->account.flags & M_ACCT_PASS)) { if (!ImapPass) { pass[0]=0; snprintf (buf, sizeof (buf), _("Password for %s@%s: "), user, conn->account.host); if (mutt_get_field (buf, pass, sizeof (pass), M_PASS) != 0 || !pass[0]) return -1; } else strfcpy (pass, ImapPass, sizeof (pass)); } else strfcpy (pass, conn->account.pass, sizeof (pass));  imap_quote_string (q_user, sizeof (q_user), user); imap_quote_string (q_pass, sizeof (q_pass), pass);  mutt_message _("Logging in...");  #ifdef DEBUG   if (debuglevel < IMAP_LOG_PASS) dprint (2, (debugfile, "Sending LOGIN command for %s...\n", user)); #endif  snprintf (buf, sizeof (buf), "LOGIN %s %s", q_user, q_pass); r = imap_exec (buf, sizeof (buf), idata, buf, IMAP_CMD_FAIL_OK | IMAP_CMD_PASS); if (r == -1) {  imap_error ("imap_authenticate", buf); return (-1); } else if (r == -2) {  mutt_error _("Login failed."); sleep (1);  if (!(conn->account.flags & M_ACCT_USER)) FREE (&ImapUser); if (!(conn->account.flags & M_ACCT_PASS)) FREE (&ImapPass); conn->account.flags &= ~M_ACCT_PASS; } else {  if (!(conn->account.flags & M_ACCT_USER)) strfcpy (conn->account.user, user, sizeof (conn->account.user)); if (!(conn->account.flags & M_ACCT_PASS)) strfcpy (conn->account.pass, pass, sizeof (conn->account.pass));  conn->account.flags |= (M_ACCT_USER | M_ACCT_PASS); } } return 0;
return r;
int imap_authenticate (IMAP_DATA* idata){  char buf[LONG_STRING];  char user[SHORT_STRING], q_user[SHORT_STRING];  char ckey[SHORT_STRING];  char pass[SHORT_STRING], q_pass[SHORT_STRING];  CONNECTION* conn = idata->conn;    int r = 1;  while (r != 0)  {    if (! (conn->account.flags & M_ACCT_USER))    {      if (!ImapUser)      {	strfcpy (user, NONULL(Username), sizeof (user));	if (mutt_get_field (_("IMAP Username: "), user, sizeof (user), 0) != 0)	{	  user[0] = 0;	  return (-1);	}      }      else	strfcpy (user, ImapUser, sizeof (user));    }    else      strfcpy (user, conn->account.user, sizeof (user));    if (!user[0])    {      if (!mutt_bit_isset (idata->capabilities, AUTH_ANON))      {	mutt_error _("Anonymous authentication not supported.");	return -1;      }            return imap_auth_anon (idata);    }    #ifdef USE_GSS    /* attempt GSSAPI authentication, if available */    if (mutt_bit_isset (idata->capabilities, AGSSAPI))    {      if ((r = imap_auth_gss (idata, user)))      {        mutt_error _("GSSAPI authentication failed.");        sleep (1);      }      else        return 0;    }    else      dprint (2, (debugfile, "GSSAPI authentication is not available\n"));#endif    /* attempt CRAM-MD5 if available */    if (mutt_bit_isset (idata->capabilities, ACRAM_MD5))    {      if (!(conn->account.flags & M_ACCT_CRAM))      {	if (!ImapCRAMKey)	{	  ckey[0] = '\0';	  snprintf (buf, sizeof (buf), _("CRAM key for %s@%s: "), user,		    conn->account.host);	  if (mutt_get_field (buf, ckey, sizeof (ckey), M_PASS) != 0)	    return -1;	}	else	  strfcpy (ckey, ImapCRAMKey, sizeof (ckey));      }      else        strfcpy (ckey, conn->account.pass, sizeof (ckey));      if (*ckey)      {	if ((r = imap_auth_cram_md5 (idata, user, ckey)))	{	  mutt_error _("CRAM-MD5 authentication failed.");	  sleep (1);	  if (!(conn->account.flags & M_ACCT_CRAM))	    FREE (&ImapCRAMKey);	  conn->account.flags &= ~M_ACCT_CRAM;	}	else	{	  strfcpy (conn->account.pass, ckey, sizeof (conn->account.pass));	  conn->account.flags |= M_ACCT_CRAM;	  return 0;	}      }      else      {	mutt_message _("Skipping CRAM-MD5 authentication.");	sleep (1);      }    }    else      dprint (2, (debugfile, "CRAM-MD5 authentication is not available\n"));            if (! (conn->account.flags & M_ACCT_PASS))    {      if (!ImapPass)      {	pass[0]=0;	snprintf (buf, sizeof (buf), _("Password for %s@%s: "), user,          conn->account.host);	if (mutt_get_field (buf, pass, sizeof (pass), M_PASS) != 0 ||	    !pass[0])	  return -1;      }      else	strfcpy (pass, ImapPass, sizeof (pass));    }    else      strfcpy (pass, conn->account.pass, sizeof (pass));    imap_quote_string (q_user, sizeof (q_user), user);    imap_quote_string (q_pass, sizeof (q_pass), pass);    mutt_message _("Logging in...");#ifdef DEBUG    /* don't print the password unless we're at the ungodly debugging level     * of 5 or higher */    if (debuglevel < IMAP_LOG_PASS)      dprint (2, (debugfile, "Sending LOGIN command for %s...\n", user));#endif    snprintf (buf, sizeof (buf), "LOGIN %s %s", q_user, q_pass);    r = imap_exec (buf, sizeof (buf), idata, buf,      IMAP_CMD_FAIL_OK | IMAP_CMD_PASS);    if (r == -1)    {      /* connection or protocol problem */      imap_error ("imap_authenticate", buf);      return (-1);    }    else if (r == -2)    {      /* Login failed, try again */      mutt_error _("Login failed.");      sleep (1);      if (!(conn->account.flags & M_ACCT_USER))	FREE (&ImapUser);      if (!(conn->account.flags & M_ACCT_PASS))	FREE (&ImapPass);      conn->account.flags &= ~M_ACCT_PASS;    }    else    {      /* If they have a successful login, we may as well cache the        * user/password. */      if (!(conn->account.flags & M_ACCT_USER))	strfcpy (conn->account.user, user, sizeof (conn->account.user));      if (!(conn->account.flags & M_ACCT_PASS))	strfcpy (conn->account.pass, pass, sizeof (conn->account.pass));            conn->account.flags |= (M_ACCT_USER | M_ACCT_PASS);    }  }  return 0;}