diff --git a/.gitignore b/.gitignore index 3db0f54..5a31db8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ config.h +users.h **/*.swp **/*~ **/*.o diff --git a/Makefile b/Makefile index d2190f4..852f678 100644 --- a/Makefile +++ b/Makefile @@ -18,11 +18,14 @@ options: .c.o: ${CC} -c ${CFLAGS} $< -${OBJ}: config.h imap.routines config.mk +${OBJ}: config.h users.h imap.routines config.mk config.h: cp config.def.h $@ +users.h: + cp users.def.h $@ + sis: ${OBJ} ${CC} -o $@ ${OBJ} ${LDFLAGS} diff --git a/auth.c b/auth.c index 4da3ce7..ea0f52c 100644 --- a/auth.c +++ b/auth.c @@ -25,13 +25,18 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef AUTH_PAM +#include +#include -int pam_conv_func(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) { +void auth_sha256(char *string, char buffer[65]) { + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, string, strlen(string)); + SHA256_Final(hash, &sha256); + int i = 0; + for(i = 0; i < SHA256_DIGEST_LENGTH; i++) { + sprintf(buffer + (i * 2), "%02x", hash[i]); + } + buffer[64] = '\0'; } - -uint8_t auth_pam(char *username, char *password) -{ -} - -#endif diff --git a/auth.h b/auth.h index 8fd3ffc..e0eb7be 100644 --- a/auth.h +++ b/auth.h @@ -28,16 +28,14 @@ #ifndef AUTH_H #define AUTH_H +struct user { + char *username; + char *password; +} __attribute__((packed)); + #define AUTH_OK 0x1 #define AUTH_FAIL 0x1 << 1 -#ifdef AUTH_PAM -#include -#include -#include - -uint8_t auth_pam(char *username, char *password); - -#endif +void auth_sha256(char *string, char buffer[65]); #endif diff --git a/config.def.h b/config.def.h index cf5acd2..e4eb0c8 100644 --- a/config.def.h +++ b/config.def.h @@ -21,11 +21,6 @@ * modify this. */ #define CMD_MAX_SIZE 8000 -/*- - * Use pam (Pluggable Authentication Modules) - * as an authentication method - */ -#define AUTH_PAM static char *imap_capabilities[] = { "IMAP4rev1", diff --git a/imap.c b/imap.c index 7987eb5..ba263a2 100644 --- a/imap.c +++ b/imap.c @@ -38,6 +38,7 @@ #include #include #include +#include static char buf[CMD_MAX_SIZE]; static trie_node *trie; @@ -405,6 +406,8 @@ imap_cmd imap_parse_cmd(char *s) cpy = (char *) calloc(strlen(s), sizeof(char)); strcpy(cpy, s); for (tok = strtok(cpy, " "); tok; tok = strtok(NULL, " ")) { + + params++; } @@ -413,7 +416,15 @@ imap_cmd imap_parse_cmd(char *s) if (params > 0) { cmd.params = (char **) calloc(params, sizeof(char **)); for (tok = strtok(s, " "); tok; tok = strtok(NULL, " ")) { + cmd.params[i] = tok; + + for (size_t j=0; cmd.params[i][j] != '\0'; j++) { + if (cmd.params[i][j] == '\r' || cmd.params[i][j] == '\n' || tok[i] == '\t') { + cmd.params[i][j] = '\0'; + break; + } + } i++; } cmd.p_count = params; @@ -422,6 +433,7 @@ imap_cmd imap_parse_cmd(char *s) return cmd; } +#include #include uint8_t imap_cmd_exec(imap_cmd cmd, client_list *node, uint8_t ssl, uint8_t state) diff --git a/imap.routines b/imap.routines index ce74423..89c1549 100644 --- a/imap.routines +++ b/imap.routines @@ -102,10 +102,21 @@ static inline uint8_t imap_routine_login(imap_cmd cmd, client_list *node, uint8_ { IMAP_CHECK_STATE(NO_AUTH) IMAP_CHECK_ARGS(2) + size_t users = sizeof(imap_users)/sizeof(struct user); + uint8_t found = 0; - if ((strcmp(cmd.params[0], "lorenzo") == 0) && (strcmp(cmd.params[1], "lorenzo06") == 0)) { - IMAP_ROUTINE_OK(LOGIN) - } else { + for (size_t i=0; i < users; i++) { + if (strcmp(cmd.params[0], imap_users[i].username) == 0) { + char hash[65]; + auth_sha256(cmd.params[1], hash); + if (strcmp(hash, imap_users[i].password) == 0) { + IMAP_ROUTINE_OK(LOGIN) + found = 1; + } + } + } + + if (!found) { IMAP_ROUTINE_NO(LOGIN) } diff --git a/users.def.h b/users.def.h new file mode 100644 index 0000000..13d4fe0 --- /dev/null +++ b/users.def.h @@ -0,0 +1,6 @@ +/* See LICENSE file for copyright and license details. */ + +static const struct user imap_users[] = { + /* username password hash */ + /* {"jhon", "e6cc90956e99b1b96b319ed9ccffb18cb6c5d7c731ffe80fbd42fb674adfe444"} */ +};