feat: implemented hash based login command.

This commit is contained in:
Lorenzo Torres 2025-02-12 20:53:42 +01:00
parent 9418c7b9a4
commit d2d12e9767
8 changed files with 56 additions and 25 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
config.h
users.h
**/*.swp
**/*~
**/*.o

View file

@ -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}

19
auth.c
View file

@ -25,13 +25,18 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef AUTH_PAM
#include <openssl/sha.h>
#include <auth.h>
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]);
}
uint8_t auth_pam(char *username, char *password)
{
buffer[64] = '\0';
}
#endif

14
auth.h
View file

@ -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 <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/pam_ext.h>
uint8_t auth_pam(char *username, char *password);
#endif
void auth_sha256(char *string, char buffer[65]);
#endif

View file

@ -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",

12
imap.c
View file

@ -38,6 +38,7 @@
#include <ctype.h>
#include <utils.h>
#include <imap.h>
#include <auth.h>
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 <users.h>
#include <imap.routines>
uint8_t imap_cmd_exec(imap_cmd cmd, client_list *node, uint8_t ssl, uint8_t state)

View file

@ -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)) {
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)
} else {
found = 1;
}
}
}
if (!found) {
IMAP_ROUTINE_NO(LOGIN)
}

6
users.def.h Normal file
View file

@ -0,0 +1,6 @@
/* See LICENSE file for copyright and license details. */
static const struct user imap_users[] = {
/* username password hash */
/* {"jhon", "e6cc90956e99b1b96b319ed9ccffb18cb6c5d7c731ffe80fbd42fb674adfe444"} */
};