NeoMutt  2025-12-11-1039-g550ac6
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
auth.c File Reference

POP authentication. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "adata.h"
Include dependency graph for auth.c:

Go to the source code of this file.

Functions

void pop_apop_timestamp (struct PopAccountData *adata, char *buf)
 Get the server timestamp for APOP authentication.
static enum PopAuthRes pop_auth_apop (struct PopAccountData *adata, const char *method)
 APOP authenticator - Implements PopAuth::authenticate() -.
static enum PopAuthRes pop_auth_user (struct PopAccountData *adata, const char *method)
 USER authenticator - Implements PopAuth::authenticate() -.
static enum PopAuthRes pop_auth_oauth (struct PopAccountData *adata, const char *method)
 Authenticate a POP connection using OAUTHBEARER - Implements PopAuth::authenticate() -.
bool pop_auth_is_valid (const char *authenticator)
 Check if string is a valid pop authentication method.
int pop_authenticate (struct PopAccountData *adata)
 Authenticate with a POP server.

Variables

static const struct PopAuth PopAuthenticators []
 Accepted authentication methods.

Detailed Description

POP authentication.

Authors
  • Vsevolod Volkov
  • Richard Russon
  • Pietro Cerutti
  • Yousef Akbar
  • Alejandro Colomar

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file auth.c.

Function Documentation

◆ pop_apop_timestamp()

void pop_apop_timestamp ( struct PopAccountData * adata,
char * buf )

Get the server timestamp for APOP authentication.

Parameters
adataPOP Account data
bufTimestamp string

Definition at line 326 of file auth.c.

327{
328 char *p1 = NULL;
329 char *p2 = NULL;
330
331 FREE(&adata->timestamp);
332
333 if ((p1 = strchr(buf, '<')) && (p2 = strchr(p1, '>')))
334 {
335 p2[1] = '\0';
336 adata->timestamp = mutt_str_dup(p1);
337 }
338}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
char * timestamp
APOP timestamp.
Definition adata.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop_auth_is_valid()

bool pop_auth_is_valid ( const char * authenticator)

Check if string is a valid pop authentication method.

Parameters
authenticatorAuthenticator string to check
Return values
trueArgument is a valid auth method

Validate whether an input string is an accepted pop authentication method as defined by PopAuthenticators.

Definition at line 534 of file auth.c.

535{
536 for (size_t i = 0; i < countof(PopAuthenticators); i++)
537 {
538 const struct PopAuth *auth = &PopAuthenticators[i];
539 if (auth->method && mutt_istr_equal(auth->method, authenticator))
540 return true;
541 }
542
543 return false;
544}
#define countof(x)
Definition memory.h:49
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
static const struct PopAuth PopAuthenticators[]
Accepted authentication methods.
Definition auth.c:511
POP authentication multiplexor.
Definition private.h:76
const char * method
Name of authentication method supported, NULL means variable.
Definition private.h:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pop_authenticate()

int pop_authenticate ( struct PopAccountData * adata)

Authenticate with a POP server.

Parameters
adataPOP Account data
Return values
numResult, e.g. POP_A_SUCCESS
0Successful
-1Connection lost
-2Login failed
-3Authentication cancelled

Definition at line 555 of file auth.c.

556{
557 struct ConnAccount *cac = &adata->conn->account;
558 const struct PopAuth *authenticator = NULL;
559 int attempts = 0;
560 int rc = POP_A_UNAVAIL;
561
562 if ((mutt_account_getuser(cac) < 0) || (cac->user[0] == '\0'))
563 {
564 return -3;
565 }
566
567 const struct Slist *c_pop_authenticators = cs_subset_slist(NeoMutt->sub, "pop_authenticators");
568 const bool c_pop_auth_try_all = cs_subset_bool(NeoMutt->sub, "pop_auth_try_all");
569 if (c_pop_authenticators && (c_pop_authenticators->count > 0))
570 {
571 /* Try user-specified list of authentication methods */
572 struct ListNode *np = NULL;
573 STAILQ_FOREACH(np, &c_pop_authenticators->head, entries)
574 {
575 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
576 authenticator = PopAuthenticators;
577
578 /* Walk the built-in authenticator table looking for a match */
579 while (authenticator->authenticate)
580 {
581 if (!authenticator->method || mutt_istr_equal(authenticator->method, np->data))
582 {
583 rc = authenticator->authenticate(adata, np->data);
584 if (rc == POP_A_SOCKET)
585 {
586 /* Connection dropped; try reconnecting and retrying once */
587 switch (pop_connect(adata))
588 {
589 case 0:
590 {
591 rc = authenticator->authenticate(adata, np->data);
592 break;
593 }
594 case -2:
595 rc = POP_A_FAILURE;
596 break;
597 default:
598 break;
599 }
600 }
601
602 if (rc != POP_A_UNAVAIL)
603 attempts++;
604 if ((rc == POP_A_SUCCESS) || (rc == POP_A_SOCKET) ||
605 ((rc == POP_A_FAILURE) && !c_pop_auth_try_all))
606 {
607 break;
608 }
609 }
610 authenticator++;
611 }
612 }
613 }
614 else
615 {
616 /* Fall back to default: try every authenticator in order */
617 mutt_debug(LL_DEBUG2, "Using any available method\n");
618 authenticator = PopAuthenticators;
619
620 while (authenticator->authenticate)
621 {
622 rc = authenticator->authenticate(adata, NULL);
623 if (rc == POP_A_SOCKET)
624 {
625 switch (pop_connect(adata))
626 {
627 case 0:
628 {
629 rc = authenticator->authenticate(adata, NULL);
630 break;
631 }
632 case -2:
633 rc = POP_A_FAILURE;
634 break;
635 default:
636 break;
637 }
638 }
639
640 if (rc != POP_A_UNAVAIL)
641 attempts++;
642 if ((rc == POP_A_SUCCESS) || (rc == POP_A_SOCKET) ||
643 ((rc == POP_A_FAILURE) && !c_pop_auth_try_all))
644 {
645 break;
646 }
647
648 authenticator++;
649 }
650 }
651
652 /* Map internal result code to the caller's return values */
653 switch (rc)
654 {
655 case POP_A_SUCCESS:
656 return 0;
657 case POP_A_SOCKET:
658 return -1;
659 case POP_A_UNAVAIL:
660 if (attempts == 0)
661 mutt_error(_("No authenticators available"));
662 break;
663
664 default:
665 break;
666 }
667
668 return -2;
669}
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition helpers.c:242
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
int mutt_account_getuser(struct ConnAccount *cac)
Retrieve username into ConnAccount, if necessary.
Definition connaccount.c:51
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
#define _(a)
Definition message.h:28
int pop_connect(struct PopAccountData *adata)
Open connection.
Definition lib.c:282
@ POP_A_UNAVAIL
No valid authentication method.
Definition private.h:60
@ POP_A_SUCCESS
Authenticated successfully.
Definition private.h:57
@ POP_A_FAILURE
Authentication failed.
Definition private.h:59
@ POP_A_SOCKET
Connection lost.
Definition private.h:58
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
Login details for a remote server.
Definition connaccount.h:59
char user[128]
Username.
Definition connaccount.h:62
struct ConnAccount account
Account details: username, password, etc.
Definition connection.h:49
A List node for strings.
Definition list.h:37
char * data
String.
Definition list.h:38
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
struct Connection * conn
Connection to POP server.
Definition adata.h:38
enum PopAuthRes(* authenticate)(struct PopAccountData *adata, const char *method)
Definition private.h:85
String list.
Definition slist.h:37
struct ListHead head
List containing values.
Definition slist.h:38
size_t count
Number of values in list.
Definition slist.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ PopAuthenticators

const struct PopAuth PopAuthenticators[]
static
Initial value:
= {
{ pop_auth_oauth, "oauthbearer" },
{ pop_auth_apop, "apop" },
{ pop_auth_user, "user" },
{ NULL, NULL },
}
static enum PopAuthRes pop_auth_user(struct PopAccountData *adata, const char *method)
USER authenticator - Implements PopAuth::authenticate() -.
Definition auth.c:394
static enum PopAuthRes pop_auth_apop(struct PopAccountData *adata, const char *method)
APOP authenticator - Implements PopAuth::authenticate() -.
Definition auth.c:343
static enum PopAuthRes pop_auth_oauth(struct PopAccountData *adata, const char *method)
Authenticate a POP connection using OAUTHBEARER - Implements PopAuth::authenticate() -.
Definition auth.c:454

Accepted authentication methods.

Definition at line 511 of file auth.c.

511 {
512 // clang-format off
513 { pop_auth_oauth, "oauthbearer" },
514#ifdef USE_SASL_CYRUS
515 { pop_auth_sasl, NULL },
516#endif
517#ifdef USE_SASL_GNU
518 { pop_auth_gsasl, NULL },
519#endif
520 { pop_auth_apop, "apop" },
521 { pop_auth_user, "user" },
522 { NULL, NULL },
523 // clang-format on
524};