Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SASL Code Quality and Feature Improvements #598

Merged
merged 119 commits into from Dec 26, 2017
Merged
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
6573037
modules/saslserv/main: fix function prototype
aaronmdjones Dec 13, 2017
21d6a26
include/sasl.h: tidy up structures
aaronmdjones Dec 13, 2017
4d75c43
modules/saslserv/: various adjustments
aaronmdjones Dec 13, 2017
49c12af
modules/saslserv/*.c: move mod(de)init to the bottom of the file
aaronmdjones Dec 13, 2017
e0572b5
modules/saslserv/*.c: move sasl_mechanism_t declaration
aaronmdjones Dec 13, 2017
3a3f600
modules/saslserv/*.c: put function names on their own line
aaronmdjones Dec 13, 2017
6605352
libathemecore/authcookie: promise not to modify tickets
aaronmdjones Dec 13, 2017
fbf6025
modules/saslserv/authcookie: general code quality cleanups
aaronmdjones Dec 13, 2017
72900dc
modules/saslserv/ecdsa-nist256p-challenge: general code quality cleanups
aaronmdjones Dec 13, 2017
3c459c7
modules/saslserv/external: general code quality cleanups
aaronmdjones Dec 13, 2017
d861474
modules/saslserv/plain: general code quality cleanups
aaronmdjones Dec 13, 2017
a44cccb
modules/saslserv/scram-sha: general code quality cleanups
aaronmdjones Dec 13, 2017
ada622f
PBKDF2v2/SCRAM-SHA: Move shared functions to a shared structure
aaronmdjones Dec 13, 2017
0c10d11
modules/saslserv/main: make private symbols actually private
aaronmdjones Dec 13, 2017
2e37d4a
modules/saslserv/main: put function names on their own line
aaronmdjones Dec 13, 2017
13404b2
modules/saslserv/main: move mod(de)init functions to the bottom
aaronmdjones Dec 13, 2017
8bdc0b9
modules/saslserv/main: fix (dead) potential segfault in moddeinit
aaronmdjones Dec 13, 2017
9ab5578
modules/saslserv/main: move structures to the top of the file
aaronmdjones Dec 13, 2017
bd7790b
modules/saslserv/ecdsa-nist256p-challenge: guard against no mechdata
aaronmdjones Dec 13, 2017
eb37524
modules/saslserv/main: use size_t, not int, for buffer/string lengths
aaronmdjones Dec 13, 2017
ababf28
modules/saslserv/main: reindent vtable structure
aaronmdjones Dec 13, 2017
0139282
modules/saslserv/main: saslserv: cleanup
aaronmdjones Dec 13, 2017
61e130b
modules/saslserv/main: sasl_mech_register: cleanup
aaronmdjones Dec 13, 2017
a6ca0b0
modules/saslserv/main: sasl_mech_unregister: cleanup
aaronmdjones Dec 13, 2017
f1e9c57
modules/saslserv/main: find_session: cleanup
aaronmdjones Dec 13, 2017
5069e21
modules/saslserv/main: make_session: cleanup
aaronmdjones Dec 13, 2017
eeb38f7
modules/saslserv/main: destroy_session: cleanup
aaronmdjones Dec 13, 2017
e75f378
modules/saslserv/main: sasl_sourceinfo_delete: cleanup
aaronmdjones Dec 13, 2017
94b5e51
modules/saslserv/main: sasl_sourceinfo_create: cleanup
aaronmdjones Dec 13, 2017
b14ef97
modules/saslserv/main: sasl_input: cleanup
aaronmdjones Dec 13, 2017
a603131
modules/saslserv/main: find_mechanism: cleanup
aaronmdjones Dec 13, 2017
142f075
modules/saslserv/main: sasl_server_eob: cleanup
aaronmdjones Dec 13, 2017
5d0455a
modules/saslserv/main: mechlist_do_rebuild: cleanup
aaronmdjones Dec 13, 2017
0025a44
modules/saslserv/main: mechlist_build_string: cleanup
aaronmdjones Dec 13, 2017
c461c3d
modules/saslserv/main: sasl_packet: cleanup
aaronmdjones Dec 13, 2017
0e2fbd6
modules/saslserv/main: sasl_write: cleanup
aaronmdjones Dec 13, 2017
1e9e5ad
modules/saslserv/main: may_impersonate: cleanup
aaronmdjones Dec 13, 2017
20d7232
modules/saslserv/main: login_user: cleanup
aaronmdjones Dec 13, 2017
1d68dc2
modules/saslserv/main: sasl_newuser: cleanup
aaronmdjones Dec 13, 2017
72ba0f6
modules/saslserv/main: delete_stale: cleanup
aaronmdjones Dec 13, 2017
29fcbe9
modules/saslserv/main: sasl_format_sourceinfo: cleanup
aaronmdjones Dec 13, 2017
95c386d
modules/saslserv/main: sasl_get_source_name: cleanup
aaronmdjones Dec 13, 2017
daec26c
modules/saslserv/main: mod_(de)init: cleanup
aaronmdjones Dec 13, 2017
0e72886
modules/saslserv/main: sasl_get_source_name: more cleanup
aaronmdjones Dec 13, 2017
8056bfd
modules/saslserv/main: less magic numbers
aaronmdjones Dec 13, 2017
d97e5eb
modules/saslserv/main: remove free(3) indirection
aaronmdjones Dec 13, 2017
8110458
modules/saslserv/main: mod_(de)init: more cleanup
aaronmdjones Dec 13, 2017
8b47109
modules/saslserv/main: saslserv: move
aaronmdjones Dec 13, 2017
eef56b7
modules/saslserv/main: sasl_mech_(un)register: move
aaronmdjones Dec 13, 2017
df0302f
modules/saslserv/main: remove erroneous comment
aaronmdjones Dec 13, 2017
f77a8b6
modules/saslserv/main: sasl_vtable: move
aaronmdjones Dec 13, 2017
eafc7e6
modules/saslserv/main: remove unnecessary forward decls
aaronmdjones Dec 13, 2017
9a16559
modules/saslserv/main: rename a variable
aaronmdjones Dec 13, 2017
227f021
modules/saslserv/main: sasl_write: move
aaronmdjones Dec 13, 2017
a683f27
modules/saslserv/main: sasl_input: move
aaronmdjones Dec 13, 2017
59e1d1e
modules/saslserv/main: login_user: move
aaronmdjones Dec 13, 2017
6b26bec
modules/saslserv/main: sasl_session_abort: move
aaronmdjones Dec 13, 2017
822e985
modules/saslserv/main: sasl_sourceinfo_create: move
aaronmdjones Dec 13, 2017
219cf3a
modules/saslserv/main: may_impersonate: move
aaronmdjones Dec 13, 2017
0f8c200
modules/saslserv/main: remove unnecessary forward decls
aaronmdjones Dec 13, 2017
dccd978
modules/saslserv/main: mechlist_do_rebuild: move
aaronmdjones Dec 13, 2017
40857d0
modules/saslserv/main: sasl_sourceinfo_t: reindent
aaronmdjones Dec 13, 2017
5e1dde6
modules/saslserv/main: sasl_sourceinfo_t: rename
aaronmdjones Dec 13, 2017
4788844
modules/saslserv/: don't use reserved identifier names
aaronmdjones Dec 13, 2017
e77f4bd
modules/saslserv/main: avoid TOCTOU and make user_can_login hooks useful
aaronmdjones Dec 13, 2017
b39a9d2
modules/saslserv/plain: no need to wrap this line
aaronmdjones Dec 13, 2017
6fe8864
modules/saslserv/main: remove duplicate code
aaronmdjones Dec 13, 2017
6ae3da8
modules/saslserv/main: sasl_packet: use larger buffer
aaronmdjones Dec 13, 2017
4b7dc65
modules/saslserv/scram-sha: various adjustments
aaronmdjones Dec 13, 2017
59d0328
modules/saslserv/*.c: adjust function parameter types
aaronmdjones Dec 13, 2017
ca18f82
modules/saslserv/main: overlooked opportunity to use less memory
aaronmdjones Dec 13, 2017
3962290
modules/saslserv/ecdsa-nist256p-challenge: remove unnecessary code
aaronmdjones Dec 13, 2017
44cf98f
libathemecore/authcookie: provide a macro for the cookie length
aaronmdjones Dec 13, 2017
3dd7da7
modules/saslserv/{authcookie,plain}: remove magic number, explain format
aaronmdjones Dec 13, 2017
8c64dce
modules/saslserv/{authcookie,plain}: clarify pointer arithmetic inten…
aaronmdjones Dec 13, 2017
099edf0
libmowgli: update to current master HEAD
aaronmdjones Dec 13, 2017
7e8bcbf
modules/saslserv/*.c: more guards against aberrant program states
aaronmdjones Dec 13, 2017
5ca1e7f
modules/saslserv/main: more const correctness
aaronmdjones Dec 13, 2017
9a3a97f
modules/saslserv/main: don't user_can_login twice for the same user
aaronmdjones Dec 14, 2017
57ebc82
modules/saslserv/main: remove unnecessary pointer gymnastics
aaronmdjones Dec 14, 2017
97f9395
modules/saslserv/main: remove more unnecessary pointer gymnastics
aaronmdjones Dec 14, 2017
1f3a05a
modules/saslserv/main: remove unnecessary test
aaronmdjones Dec 14, 2017
9f43a16
modules/saslserv/main: test for max length data from client first
aaronmdjones Dec 14, 2017
001b2be
modules/saslserv/main: realloc(NULL, foo>0) == malloc(foo)
aaronmdjones Dec 14, 2017
333bab7
modules/saslserv/main: fail EXTERNAL without certfp sooner
aaronmdjones Dec 14, 2017
d6047a2
modules/saslserv/main: simplify SASL 'S' command processing
aaronmdjones Dec 14, 2017
ba384a4
modules/saslserv/main: more const-correctness
aaronmdjones Dec 14, 2017
015e006
modules/saslserv/main: clarify function intention
aaronmdjones Dec 14, 2017
5fef7f9
modules/saslserv/main: add debugging log message
aaronmdjones Dec 14, 2017
44d0b84
modules/saslserv/*.c: ensure/assume mech_step input is NULL-terminated
aaronmdjones Dec 14, 2017
8b6f12b
modules/saslserv/main: short-circuit client data if possible
aaronmdjones Dec 14, 2017
b246ed5
modules/saslserv/main: add some defensive code to sasl_packet()
aaronmdjones Dec 14, 2017
6fdeb73
modules/saslserv/main: fix bound on client-provided data
aaronmdjones Dec 14, 2017
cd3a79c
modules/saslserv/*.c: update copyright notices
aaronmdjones Dec 16, 2017
703a0fe
modules/saslserv/main: don't erroneously process end-of-data marker
aaronmdjones Dec 16, 2017
2e0c282
modules/saslserv/main: remove (now) duplicate code
aaronmdjones Dec 16, 2017
fc1a909
sasl_sts / proto modules: promise not to modify string arguments
aaronmdjones Dec 16, 2017
e3e22dd
modules/saslserv/main: optimise sasl_write for short data
aaronmdjones Dec 16, 2017
a55a115
modules/saslserv/main: don't allow malicious clients to flood log
aaronmdjones Dec 16, 2017
b3e5e28
modules/saslserv/main: sasl_packet: use smaller buffer length
aaronmdjones Dec 16, 2017
42dff7e
modules/saslserv/main: more const-correctness
aaronmdjones Dec 16, 2017
09a1e27
modules/saslserv/{authcookie,plain}: arithmetic on void* is a GNU ext…
aaronmdjones Dec 16, 2017
7808cc3
modules/saslserv/main: mechlist_build_string: more cleanup
aaronmdjones Dec 16, 2017
2e4968d
include/sasl.h: move some macros to the top and reindent them
aaronmdjones Dec 17, 2017
b5493b4
modules/saslserv/*.c: add new kind of start/step mech error code
aaronmdjones Dec 17, 2017
8c942f3
modules/saslserv/ecdsa-nist256p-challenge: add condition to feature test
aaronmdjones Dec 17, 2017
6557f68
modules/saslserv/*.c: results and bitfield flags should be unsigned
aaronmdjones Dec 17, 2017
3a2809a
modules/saslserv/scram-sha: correct function type
aaronmdjones Dec 17, 2017
23922a7
modules/saslserv/scram-sha: simplify attribute processing
aaronmdjones Dec 17, 2017
baf6ada
include/sasl.h: add documentation comments
aaronmdjones Dec 17, 2017
9b7179e
modules/saslserv/main: fix use-after-free & double-free
aaronmdjones Dec 17, 2017
2335d28
modules/saslserv/main: break out complex logic to separate functions
aaronmdjones Dec 17, 2017
58f8ad5
modules/saslserv/main: replace hardcoded function names
aaronmdjones Dec 17, 2017
dbb8555
modules/saslserv/main: remove unnecessary zero-initialisation
aaronmdjones Dec 21, 2017
1eca6ba
modules/saslserv/main: move 'struct sasl_sourceinfo' to shared header
aaronmdjones Dec 21, 2017
cf3473f
modules/saslserv/scram-sha: generate snonce in a simpler way
aaronmdjones Dec 21, 2017
42031c9
modules/saslserv/scram-sha: respect MU_NOPASSWORD flag
aaronmdjones Dec 23, 2017
636bb2d
modules/saslserv/main: preserve auth[cz]id for better logging
aaronmdjones Dec 23, 2017
45a7f19
modules/saslserv/main: remove code duplication
aaronmdjones Dec 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/authcookie.h
Expand Up @@ -20,10 +20,10 @@ struct authcookie_ {

E void authcookie_init(void);
E authcookie_t *authcookie_create(myuser_t *mu);
E authcookie_t *authcookie_find(char *ticket, myuser_t *myuser);
E authcookie_t *authcookie_find(const char *ticket, myuser_t *myuser);
E void authcookie_destroy(authcookie_t *ac);
E void authcookie_destroy_all(myuser_t *mu);
E bool authcookie_validate(char *ticket, myuser_t *myuser);
E bool authcookie_validate(const char *ticket, myuser_t *myuser);
E void authcookie_expire(void *arg);

#endif
Expand Down
16 changes: 5 additions & 11 deletions include/pbkdf2v2.h
Expand Up @@ -78,16 +78,10 @@ static const unsigned char ClientKeyStr[] = {
0x43, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x4B, 0x65, 0x79
};

/*
* For modules/saslserv/scram-sha to make an inter-module function call to
* modules/crypto/pbkdf2v2:atheme_pbkdf2v2_scram_dbextract()
*/
typedef bool (*atheme_pbkdf2v2_scram_dbextract_fn)(const char *restrict, struct pbkdf2v2_dbentry *restrict);

/*
* For modules/saslserv/scram-sha to make an inter-module function call to
* modules/crypto/pbkdf2v2:atheme_pbkdf2v2_scram_normalize()
*/
typedef const char *(*atheme_pbkdf2v2_scram_normalize_fn)(const char *restrict);
struct pbkdf2v2_scram_functions
{
bool (*dbextract)(const char *restrict, struct pbkdf2v2_dbentry *restrict);
const char * (*normalize)(const char *restrict);
};

#endif /* !PBKDF2V2_H */
99 changes: 52 additions & 47 deletions include/sasl.h
Expand Up @@ -9,57 +9,68 @@
#ifndef SASL_H
#define SASL_H

#define SASL_MESSAGE_MAXPARA 8 /* arbitrary, increment if needed in future */

typedef struct sasl_session_ sasl_session_t;
typedef struct sasl_message_ sasl_message_t;
typedef struct sasl_mechanism_ sasl_mechanism_t;

struct sasl_session_ {
char *uid;
char *buf, *p;
int len, flags;

server_t *server;

struct sasl_mechanism_ *mechptr;
void *mechdata;

char *username;
char *certfp;
char *authzid;

char *host;
char *ip;
bool tls;
#define SASL_MESSAGE_MAXPARA 8 /* arbitrary, increment if needed in future */
#define SASL_MECHANISM_MAXLEN 60
#define SASL_S2S_MAXLEN 400

struct sasl_session;
struct sasl_message;
struct sasl_mechanism;
struct sasl_core_functions;

struct sasl_session
{
struct sasl_mechanism *mechptr;
server_t *server;
sourceinfo_t *si;
char *uid;
char *buf;
char *p;
void *mechdata;
char *authceid;
char *authzeid;
char *certfp;
char *host;
char *ip;
size_t len;
int flags;
bool tls;
};

struct sasl_message_ {
char *uid;
char mode;
char *parv[SASL_MESSAGE_MAXPARA];
int parc;
struct sasl_message
{
server_t *server;
char *uid;
char *parv[SASL_MESSAGE_MAXPARA];
int parc;
char mode;
};

server_t *server;
struct sasl_mechanism
{
char name[SASL_MECHANISM_MAXLEN];
int (*mech_start)(struct sasl_session *, char **, size_t *);
int (*mech_step)(struct sasl_session *, char *, size_t, char **, size_t *);
void (*mech_finish)(struct sasl_session *);
};

struct sasl_mechanism_ {
char name[60];
int (*mech_start) (struct sasl_session_ *sptr, char **buffer, size_t *buflen);
int (*mech_step) (struct sasl_session_ *sptr, char *message, size_t length, char **buffer, size_t *buflen);
void (*mech_finish) (struct sasl_session_ *sptr);
struct sasl_core_functions
{
void (*mech_register)(struct sasl_mechanism *);
void (*mech_unregister)(struct sasl_mechanism *);
bool (*authcid_can_login)(struct sasl_session *, const char *, myuser_t **);
bool (*authzid_can_login)(struct sasl_session *, const char *, myuser_t **);
};

typedef struct {
myuser_t *source_mu;
myuser_t *target_mu;
bool allowed;

myuser_t *source_mu;
myuser_t *target_mu;
bool allowed;

} hook_sasl_may_impersonate_t;

typedef struct {
void (*mech_register) (struct sasl_mechanism_ *mech);
void (*mech_unregister) (struct sasl_mechanism_ *mech);
} sasl_mech_register_func_t;
typedef struct sasl_message sasl_message_t;

#define ASASL_FAIL 0 /* client supplied invalid credentials / screwed up their formatting */
#define ASASL_MORE 1 /* everything looks good so far, but we're not done yet */
Expand All @@ -69,9 +80,3 @@ typedef struct {
#define ASASL_NEED_LOG 2 /* user auth success needs to be logged still */

#endif

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
4 changes: 2 additions & 2 deletions libathemecore/authcookie.c
Expand Up @@ -75,7 +75,7 @@ authcookie_t *authcookie_create(myuser_t *mu)
* Side Effects:
* none
*/
authcookie_t *authcookie_find(char *ticket, myuser_t *myuser)
authcookie_t *authcookie_find(const char *ticket, myuser_t *myuser)
{
mowgli_node_t *n;
authcookie_t *ac;
Expand Down Expand Up @@ -205,7 +205,7 @@ void authcookie_expire(void *arg)
* Side Effects:
* expired authcookies are destroyed here
*/
bool authcookie_validate(char *ticket, myuser_t *myuser)
bool authcookie_validate(const char *ticket, myuser_t *myuser)
{
authcookie_t *ac = authcookie_find(ticket, myuser);

Expand Down
23 changes: 10 additions & 13 deletions modules/crypto/pbkdf2v2.c
Expand Up @@ -218,17 +218,11 @@ atheme_pbkdf2v2_scram_derive(const struct pbkdf2v2_dbentry *const dbe, const uns
* These 2 functions are provided for modules/saslserv/scram-sha (RFC 5802, RFC 7677, RFC 4013) *
* The second function is also used by *this* module for password normalization (in SCRAM mode) *
* *
* Prototypes for them appear first, to avoid `-Wmissing-prototypes' diagnostics (under Clang) *
* *
* Constant-but-unused function pointers for them appear last, so that the compiler can verify *
* their signatures match the typedefs in include/pbkdf2v2.h (necessary for bug-free inter- *
* module function calls) *
* A structure containing pointers to them appears last, so that it can be imported by the *
* SCRAM-SHA module. *
********************************************************************************************** */

bool atheme_pbkdf2v2_scram_dbextract(const char *restrict, struct pbkdf2v2_dbentry *restrict);
const char *atheme_pbkdf2v2_scram_normalize(const char *restrict);

bool
static bool
atheme_pbkdf2v2_scram_dbextract(const char *const restrict parameters, struct pbkdf2v2_dbentry *const restrict dbe)
{
if (! atheme_pbkdf2v2_parse_dbentry(dbe, parameters, true))
Expand Down Expand Up @@ -285,7 +279,7 @@ atheme_pbkdf2v2_scram_dbextract(const char *const restrict parameters, struct pb
return true;
}

const char *
static const char *
atheme_pbkdf2v2_scram_normalize(const char *const restrict input)
{
static char buf[ATHEME_SASLPREP_MAXLEN];
Expand All @@ -309,11 +303,14 @@ atheme_pbkdf2v2_scram_normalize(const char *const restrict input)
return buf;
}

static const atheme_pbkdf2v2_scram_dbextract_fn __attribute__((unused)) ex_fn_ptr = &atheme_pbkdf2v2_scram_dbextract;
static const atheme_pbkdf2v2_scram_normalize_fn __attribute__((unused)) nm_fn_ptr = &atheme_pbkdf2v2_scram_normalize;
const struct pbkdf2v2_scram_functions pbkdf2v2_scram_functions = {

.dbextract = &atheme_pbkdf2v2_scram_dbextract,
.normalize = &atheme_pbkdf2v2_scram_normalize,
};

/* **********************************************************************************************
* End external functions *
* End SCRAM-SHA functions *
********************************************************************************************** */

#endif /* HAVE_LIBIDN */
Expand Down
92 changes: 45 additions & 47 deletions modules/saslserv/authcookie.c
Expand Up @@ -8,72 +8,70 @@
#include "atheme.h"
#include "authcookie.h"

sasl_mech_register_func_t *regfuncs;
static int mech_start(sasl_session_t *p, char **out, size_t *out_len);
static int mech_step(sasl_session_t *p, char *message, size_t len, char **out, size_t *out_len);
static void mech_finish(sasl_session_t *p);
sasl_mechanism_t mech = {"AUTHCOOKIE", &mech_start, &mech_step, &mech_finish};
static const struct sasl_core_functions *sasl_core_functions = NULL;

static void
mod_init(module_t *const restrict m)
{
MODULE_TRY_REQUEST_SYMBOL(m, regfuncs, "saslserv/main", "sasl_mech_register_funcs");
regfuncs->mech_register(&mech);
}

static void
mod_deinit(const module_unload_intent_t intent)
static int
mech_step(struct sasl_session *const restrict p, char *const restrict message, const size_t len,
char __attribute__((unused)) **const restrict out, size_t __attribute__((unused)) *const restrict out_len)
{
regfuncs->mech_unregister(&mech);
}
if (! (message && len))
return ASASL_FAIL;

static int mech_start(sasl_session_t *p, char **out, size_t *out_len)
{
return ASASL_MORE;
}
char data[768];
if (len >= sizeof data)
return ASASL_FAIL;

static int mech_step(sasl_session_t *p, char *message, size_t len, char **out, size_t *out_len)
{
char authz[256];
char authc[256];
char cookie[256];
myuser_t *mu;
(void) memset(data, 0x00, sizeof data);
(void) memcpy(data, message, len);

/* Skip the authzid entirely */
const char *ptr = data;
const char *const end = data + len;

if(strlen(message) > 255)
const char *const authzid = ptr;
if (! *ptr || (ptr += strlen(ptr) + 1) >= end)
return ASASL_FAIL;
len -= strlen(message) + 1;
if(len <= 0)

const char *const authcid = ptr;
if (! *ptr || (ptr += strlen(ptr) + 1) >= end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really not sure whether I like assignments hidden so deep inside an if().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative is a lot more lines that do the same thing; a la

    if (! *ptr)
        return ASASL_FAIL;

    ptr += strlen(ptr) + 1;
    if (ptr >= end)
        return ASASL_FAIL;

That would get old, quickly, no?

EDIT: Added syntax highlighting

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I think that'd be fine. Yes, I guess you can't fit as much in a single screen, but (as someone who isn't all that fluent in C) it's easier to decipher at a quick glance – specifically, it's easier to notice that ptr is being assigned to.

|| chains are fine, assignments inside if() are okay I suppose, but combining the two begins to look like golfing just for the sake of it... How about this then:

const char *const authzid = ptr;

if (! *ptr)
    return ASASL_FAIL;
if ((ptr += strlen(ptr) + 1) >= end)
    return ASASL_FAIL;

const char *const authcid = ptr;

(Maybe that first check should in fact be if (! *authzid)? Technically it's the same thing, but semantically it makes more sense.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

const char *const authzid = ptr;

if (! *authzid)
    return ASASL_FAIL;
if ((ptr += strlen(authzid) + 1) >= end)
    return ASASL_FAIL;

const char *const authcid = ptr;

// ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ASASL_FAIL;
strcpy(authz, message);
message += strlen(message) + 1;

/* Copy the authcid */
if(strlen(message) > 255)
const char *const secret = ptr;
if (! *secret)
return ASASL_FAIL;
len -= strlen(message) + 1;
if(len <= 0)

if (! sasl_core_functions->authzid_can_login(p, authzid, NULL))
return ASASL_FAIL;
strcpy(authc, message);
message += strlen(message) + 1;

/* Copy the authcookie */
if(strlen(message) > 255)
myuser_t *mu = NULL;
if (! sasl_core_functions->authcid_can_login(p, authcid, &mu))
return ASASL_FAIL;
mowgli_strlcpy(cookie, message, len + 1);

/* Done dissecting, now check. */
if(!(mu = myuser_find_by_nick(authc)))
if (! authcookie_find(secret, mu))
return ASASL_FAIL;

p->username = sstrdup(authc);
p->authzid = sstrdup(authz);
return authcookie_find(cookie, mu) != NULL ? ASASL_DONE : ASASL_FAIL;
return ASASL_DONE;
}

static void mech_finish(sasl_session_t *p)
static struct sasl_mechanism mech = {

.name = "AUTHCOOKIE",
.mech_start = NULL,
.mech_step = &mech_step,
.mech_finish = NULL,
};

static void
mod_init(module_t *const restrict m)
{
MODULE_TRY_REQUEST_SYMBOL(m, sasl_core_functions, "saslserv/main", "sasl_core_functions");

(void) sasl_core_functions->mech_register(&mech);
}

static void
mod_deinit(const module_unload_intent_t __attribute__((unused)) intent)
{
(void) sasl_core_functions->mech_unregister(&mech);
}

SIMPLE_DECLARE_MODULE_V1("saslserv/authcookie", MODULE_UNLOAD_CAPABILITY_OK)