NeoMutt  2025-12-11-769-g906513
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, *p2 = NULL;
329
330 FREE(&adata->timestamp);
331
332 if ((p1 = strchr(buf, '<')) && (p2 = strchr(p1, '>')))
333 {
334 p2[1] = '\0';
335 adata->timestamp = mutt_str_dup(p1);
336 }
337}
#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 527 of file auth.c.

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

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

Accepted authentication methods.

Definition at line 504 of file auth.c.

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