diff --git a/Makefile b/Makefile index 0c31815..d2190f4 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ include config.mk -SRC = sis.c imap.c utils.c -HDR = config.def.h imap.h utils.h imap.routines +SRC = sis.c imap.c auth.c utils.c +HDR = config.def.h imap.h auth.h utils.h imap.routines OBJ = ${SRC:.c=.o} all: options sis diff --git a/auth.c b/auth.c new file mode 100644 index 0000000..4da3ce7 --- /dev/null +++ b/auth.c @@ -0,0 +1,37 @@ +/*- + * Copyright (c) 2024, Lorenzo Torres + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifdef AUTH_PAM + +int pam_conv_func(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr) { +} + +uint8_t auth_pam(char *username, char *password) +{ +} + +#endif diff --git a/auth.h b/auth.h new file mode 100644 index 0000000..8fd3ffc --- /dev/null +++ b/auth.h @@ -0,0 +1,43 @@ +/*- + * Copyright (c) 2024, Lorenzo Torres + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AUTH_H +#define AUTH_H + +#define AUTH_OK 0x1 +#define AUTH_FAIL 0x1 << 1 + +#ifdef AUTH_PAM +#include +#include +#include + +uint8_t auth_pam(char *username, char *password); + +#endif + +#endif diff --git a/config.def.h b/config.def.h index e4eb0c8..cf5acd2 100644 --- a/config.def.h +++ b/config.def.h @@ -21,6 +21,11 @@ * 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/config.mk b/config.mk index 518989f..a4e6864 100644 --- a/config.mk +++ b/config.mk @@ -15,7 +15,7 @@ INCS = -I. LIBS = -lssl -lcrypto # flags CPPFLAGS = -DVERSION=\"${VERSION}\" -CFLAGS := -std=c99 -pedantic -Wall -O0 -Wno-gnu-label-as-value -Wno-gnu-zero-variadic-macro-arguments ${INCS} ${CPPFLAGS} +CFLAGS := -std=gnu99 -pedantic -Wall -O0 -Wno-gnu-label-as-value -Wno-gnu-zero-variadic-macro-arguments ${INCS} ${CPPFLAGS} CFLAGS := ${CFLAGS} -g LDFLAGS = ${LIBS} diff --git a/imap.c b/imap.c index 55b4cab..7987eb5 100644 --- a/imap.c +++ b/imap.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -370,10 +371,11 @@ uint8_t imap_match_cmd(char *cmd, size_t len) imap_cmd imap_parse_cmd(char *s) { + char *cpy; imap_cmd cmd; strstrip(s); size_t params = 0, id_len = 0, i = 0; - char *cpy; + printf("%s\n", s); cmd.params = NULL; @@ -393,6 +395,9 @@ imap_cmd imap_parse_cmd(char *s) id_len -= 1; s -= id_len+1; + + cpy = s; + for (; (cpy-s) <= id_len; ++cpy) *cpy = tolower(*cpy); cmd.id = imap_match_cmd(s, id_len); s += id_len+2; diff --git a/imap.routines b/imap.routines index f334e9b..ce74423 100644 --- a/imap.routines +++ b/imap.routines @@ -16,6 +16,8 @@ name: { \ #define IMAP_ROUTINE_END imap_flush(node, ssl); #define IMAP_ROUTINE_OK(routine) \ imap_write(node, ssl, "%s OK " #routine " completed\n", cmd.tag); +#define IMAP_ROUTINE_NO(routine) \ + imap_write(node, ssl, "%s NO " #routine " completed\n", cmd.tag); #define IMAP_STRING(fmt, ...) \ imap_write(node, ssl, fmt, ##__VA_ARGS__); #define IMAP_NLINE imap_write(node, ssl, "\n"); @@ -77,7 +79,7 @@ static inline uint8_t imap_routine_auth(imap_cmd cmd, client_list *node, uint8_t int bytes; - if (strcmp(cmd.params[0], "PLAIN") == 0) { + if (strcmp(cmd.params[0], "plain") == 0) { IMAP_STRING("+\n"); if ((bytes = imap_read(node, buf, CMD_MAX_SIZE, ssl)) < 0) { perror("recv"); @@ -98,5 +100,15 @@ static inline uint8_t imap_routine_auth(imap_cmd cmd, client_list *node, uint8_t static inline uint8_t imap_routine_login(imap_cmd cmd, client_list *node, uint8_t ssl, uint8_t state) { + IMAP_CHECK_STATE(NO_AUTH) + IMAP_CHECK_ARGS(2) + + if ((strcmp(cmd.params[0], "lorenzo") == 0) && (strcmp(cmd.params[1], "lorenzo06") == 0)) { + IMAP_ROUTINE_OK(LOGIN) + } else { + IMAP_ROUTINE_NO(LOGIN) + } + + IMAP_ROUTINE_END return IMAP_SUCCESS; }