| 1 |
#pragma once |
| 2 |
|
| 3 |
/* |
| 4 |
* Server-Side SASL Parser |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <c-stdaux.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <sys/types.h> |
| 11 |
|
| 12 |
typedef struct SASLClient SASLClient; |
| 13 |
typedef struct SASLServer SASLServer; |
| 14 |
|
| 15 |
enum { |
| 16 |
_SASL_E_SUCCESS, |
| 17 |
|
| 18 |
SASL_E_FAILURE, |
| 19 |
SASL_E_PROTOCOL_VIOLATION, |
| 20 |
}; |
| 21 |
|
| 22 |
/* client */ |
| 23 |
|
| 24 |
enum { |
| 25 |
SASL_CLIENT_STATE_INIT, |
| 26 |
SASL_CLIENT_STATE_DONE, |
| 27 |
SASL_CLIENT_STATE_AUTH, |
| 28 |
SASL_CLIENT_STATE_DATA, |
| 29 |
SASL_CLIENT_STATE_UNIX_FD, |
| 30 |
}; |
| 31 |
|
| 32 |
struct SASLClient { |
| 33 |
unsigned int state; |
| 34 |
}; |
| 35 |
|
| 36 |
#define SASL_CLIENT_NULL {} |
| 37 |
|
| 38 |
void sasl_client_init(SASLClient *sasl); |
| 39 |
void sasl_client_deinit(SASLClient *sasl); |
| 40 |
|
| 41 |
int sasl_client_dispatch(SASLClient *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp); |
| 42 |
|
| 43 |
C_DEFINE_CLEANUP(SASLClient *, sasl_client_deinit); |
| 44 |
|
| 45 |
/* server */ |
| 46 |
|
| 47 |
enum { |
| 48 |
SASL_SERVER_STATE_INIT, |
| 49 |
SASL_SERVER_STATE_DONE, |
| 50 |
SASL_SERVER_STATE_AUTH, |
| 51 |
SASL_SERVER_STATE_CHALLENGE, |
| 52 |
SASL_SERVER_STATE_AUTHENTICATED, |
| 53 |
SASL_SERVER_STATE_NEGOTIATED_FDS, |
| 54 |
}; |
| 55 |
|
| 56 |
struct SASLServer { |
| 57 |
unsigned int state; |
| 58 |
bool fds_allowed; |
| 59 |
uid_t uid; |
| 60 |
char ok_response[sizeof("OK 0123456789abcdef0123456789abdcef") - 1]; |
| 61 |
}; |
| 62 |
|
| 63 |
#define SASL_SERVER_NULL {} |
| 64 |
|
| 65 |
void sasl_server_init(SASLServer *sasl, uid_t uid, const char *guid); |
| 66 |
void sasl_server_deinit(SASLServer *sasl); |
| 67 |
|
| 68 |
int sasl_server_dispatch(SASLServer *sasl, const char *input, size_t n_input, const char **outputp, size_t *n_outputp); |
| 69 |
|
| 70 |
C_DEFINE_CLEANUP(SASLServer *, sasl_server_deinit); |
| 71 |
|
| 72 |
/* inline helpers */ |
| 73 |
|
| 74 |
static inline bool sasl_client_is_done(SASLClient *client) { |
| 75 |
return _c_likely_(client->state == SASL_CLIENT_STATE_DONE); |
| 76 |
} |
| 77 |
|
| 78 |
static inline bool sasl_server_is_done(SASLServer *server) { |
| 79 |
return _c_likely_(server->state == SASL_SERVER_STATE_DONE); |
| 80 |
} |