NeoMutt  2025-12-11-980-ge38c27
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 528 of file auth.c.

529{
530 for (size_t i = 0; i < countof(PopAuthenticators); i++)
531 {
532 const struct PopAuth *auth = &PopAuthenticators[i];
533 if (auth->method && mutt_istr_equal(auth->method, authenticator))
534 return true;
535 }
536
537 return false;
538}
#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:505
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 549 of file auth.c.

550{
551 struct ConnAccount *cac = &adata->conn->account;
552 const struct PopAuth *authenticator = NULL;
553 int attempts = 0;
554 int rc = POP_A_UNAVAIL;
555
556 if ((mutt_account_getuser(cac) < 0) || (cac->user[0] == '\0'))
557 {
558 return -3;
559 }
560
561 const struct Slist *c_pop_authenticators = cs_subset_slist(NeoMutt->sub, "pop_authenticators");
562 const bool c_pop_auth_try_all = cs_subset_bool(NeoMutt->sub, "pop_auth_try_all");
563 if (c_pop_authenticators && (c_pop_authenticators->count > 0))
564 {
565 /* Try user-specified list of authentication methods */
566 struct ListNode *np = NULL;
567 STAILQ_FOREACH(np, &c_pop_authenticators->head, entries)
568 {
569 mutt_debug(LL_DEBUG2, "Trying method %s\n", np->data);
570 authenticator = PopAuthenticators;
571
572 /* Walk the built-in authenticator table looking for a match */
573 while (authenticator->authenticate)
574 {
575 if (!authenticator->method || mutt_istr_equal(authenticator->method, np->data))
576 {
577 rc = authenticator->authenticate(adata, np->data);
578 if (rc == POP_A_SOCKET)
579 {
580 /* Connection dropped; try reconnecting and retrying once */
581 switch (pop_connect(adata))
582 {
583 case 0:
584 {
585 rc = authenticator->authenticate(adata, np->data);
586 break;
587 }
588 case -2:
589 rc = POP_A_FAILURE;
590 }
591 }
592
593 if (rc != POP_A_UNAVAIL)
594 attempts++;
595 if ((rc == POP_A_SUCCESS) || (rc == POP_A_SOCKET) ||
596 ((rc == POP_A_FAILURE) && !c_pop_auth_try_all))
597 {
598 break;
599 }
600 }
601 authenticator++;
602 }
603 }
604 }
605 else
606 {
607 /* Fall back to default: try every authenticator in order */
608 mutt_debug(LL_DEBUG2, "Using any available method\n");
609 authenticator = PopAuthenticators;
610
611 while (authenticator->authenticate)
612 {
613 rc = authenticator->authenticate(adata, NULL);
614 if (rc == POP_A_SOCKET)
615 {
616 switch (pop_connect(adata))
617 {
618 case 0:
619 {
620 rc = authenticator->authenticate(adata, NULL);
621 break;
622 }
623 case -2:
624 rc = POP_A_FAILURE;
625 }
626 }
627
628 if (rc != POP_A_UNAVAIL)
629 attempts++;
630 if ((rc == POP_A_SUCCESS) || (rc == POP_A_SOCKET) ||
631 ((rc == POP_A_FAILURE) && !c_pop_auth_try_all))
632 {
633 break;
634 }
635
636 authenticator++;
637 }
638 }
639
640 /* Map internal result code to the caller's return values */
641 switch (rc)
642 {
643 case POP_A_SUCCESS:
644 return 0;
645 case POP_A_SOCKET:
646 return -1;
647 case POP_A_UNAVAIL:
648 if (attempts == 0)
649 mutt_error(_("No authenticators available"));
650 }
651
652 return -2;
653}
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:281
@ 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:392
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:450

Accepted authentication methods.

Definition at line 505 of file auth.c.

505 {
506 // clang-format off
507 { pop_auth_oauth, "oauthbearer" },
508#ifdef USE_SASL_CYRUS
509 { pop_auth_sasl, NULL },
510#endif
511#ifdef USE_SASL_GNU
512 { pop_auth_gsasl, NULL },
513#endif
514 { pop_auth_apop, "apop" },
515 { pop_auth_user, "user" },
516 { NULL, NULL },
517 // clang-format on
518};