feat: implemented hash based login command.
This commit is contained in:
parent
9418c7b9a4
commit
d2d12e9767
8 changed files with 56 additions and 25 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
|||
config.h
|
||||
users.h
|
||||
**/*.swp
|
||||
**/*~
|
||||
**/*.o
|
||||
|
|
|
|||
5
Makefile
5
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}
|
||||
|
||||
|
|
|
|||
21
auth.c
21
auth.c
|
|
@ -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]);
|
||||
}
|
||||
buffer[64] = '\0';
|
||||
}
|
||||
|
||||
uint8_t auth_pam(char *username, char *password)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
14
auth.h
14
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 <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
|
||||
|
|
|
|||
|
|
@ -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
12
imap.c
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
6
users.def.h
Normal file
6
users.def.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
|
||||
static const struct user imap_users[] = {
|
||||
/* username password hash */
|
||||
/* {"jhon", "e6cc90956e99b1b96b319ed9ccffb18cb6c5d7c731ffe80fbd42fb674adfe444"} */
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue