Compare commits

...

7 Commits

Author SHA1 Message Date
chloe 3446f78436 endpoint: server: Add server creation logic
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-04 02:42:17 -04:00
chloe 9ffcf336d7 libremail+tools: Add mailbox_create() function
It is best to prefer mailbox_create() rather than try_mkdir() directly
as we'll be able to encapslate mailbox creation logic within it.

Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-05-03 15:49:05 -04:00
chloe 0a69f7f4f4 tools: mailutil: Add mailbox remove command
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-03 02:05:37 -04:00
chloe 67155faed1 tools: mailutil: Add mailbox list command
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-03 02:01:42 -04:00
chloe 7513445d1a tools+libremail: Some refactoring
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-02 22:56:40 -04:00
chloe 92de3cdebb tools: Add mailutil groundwork
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-02 22:52:03 -04:00
chloe 7892ac5bd3 build: Add clean target for libremail
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-02 22:29:32 -04:00
10 changed files with 443 additions and 1 deletions
+7 -1
View File
@@ -4,7 +4,7 @@
# #
.PHONY: all .PHONY: all
all: bin libremail endpoint all: bin libremail tools endpoint
.PHONY: bin .PHONY: bin
bin: bin:
@@ -14,6 +14,10 @@ bin:
libremail: libremail:
cd libremail/; make cd libremail/; make
.PHONY: tools
tools:
cd tools/; make
.PHONY: endpoint .PHONY: endpoint
endpoint: endpoint:
cd endpoint/; make cd endpoint/; make
@@ -21,3 +25,5 @@ endpoint:
.PHONY: clean .PHONY: clean
clean: clean:
cd endpoint/; make clean cd endpoint/; make clean
cd libremail/; make clean
cd tools/; make clean
+70
View File
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#include "endpoint/server.h"
#include "endpoint/common.h"
int
endpoint_server_init(struct endpoint_server *svp)
{
struct sockaddr_in saddr;
int error;
if (svp == NULL) {
errno = EINVAL;
return -1;
}
svp->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (svp->sockfd < 0) {
perror("socket");
return -1;
}
/* Assign the address */
bzero(&saddr, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(SERVER_PORT);
saddr.sin_addr.s_addr = INADDR_ANY;
/* Try to bind it */
error = bind(svp->sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
if (error < 0) {
perror("bind");
close(svp->sockfd);
svp->sockfd = -1;
return -1;
}
/* Now mark it is active and listening */
error = listen(svp->sockfd, SERVER_BACKLOG);
if (error < 0) {
perror("listen");
close(svp->sockfd);
svp->sockfd = -1;
return -1;
}
return 0;
}
int
endpoint_server_close(struct endpoint_server *svp)
{
if (svp == NULL) {
errno = EINVAL;
return -1;
}
close(svp->sockfd);
return 0;
}
+6
View File
@@ -9,4 +9,10 @@
/* Server software version */ /* Server software version */
#define SERVER_VERSION "0.0.1" #define SERVER_VERSION "0.0.1"
/* Server listen port */
#define SERVER_PORT 717
/* Server listen backlog */
#define SERVER_BACKLOG 16
#endif /* !ENDPOINT_COMMON_H */ #endif /* !ENDPOINT_COMMON_H */
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#ifndef ENDPOINT_SERVER_H
#define ENDPOINT_SERVER_H 1
#include <stdint.h>
#include <stddef.h>
/*
* Represents the endpoint server state
*
* @sockfd: Socket file descriptor
*/
struct endpoint_server {
int sockfd;
};
/*
* Initialize a new server
*
* @svp: Server pointer
*
* Returns zero on success
*/
int endpoint_server_init(struct endpoint_server *svp);
/*
* Close the endpoint server
*
* @svp: Server to close
*
* Returns zero on success
*/
int endpoint_server_close(struct endpoint_server *svp);
#endif /* !ENDPOINT_SERVER_H */
+29
View File
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include "libremail/mailbox.h"
int
mailbox_create(const char *path, mode_t mode)
{
int error;
if (path == NULL) {
errno = EINVAL;
return -1;
}
error = mkdir(path, mode);
if (error < 0) {
perror("mkdir");
return -1;
}
return 0;
}
+6
View File
@@ -9,4 +9,10 @@
/* Length of SHA256 hash in bytes */ /* Length of SHA256 hash in bytes */
#define SHA256_N_BYTES 32 #define SHA256_N_BYTES 32
/* Mailbox directory prefix */
#define MAILBOX_PREFIX "/var/mail"
/* Mailbox creation mode */
#define DEFAULT_MAILBOX_MODE 0600
#endif /* !LIBREMAIL_COMMON_H */ #endif /* !LIBREMAIL_COMMON_H */
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#ifndef LIBREMAIL_MAILBOX_H
#define LIBREMAIL_MAILBOX_H 1
#include <sys/stat.h>
/*
* Create a mailbox directory
*
* @path: Path to mailbox to create
* @mode: Mode to assign to directory
*
* Returns zero on success
*/
int mailbox_create(const char *path, mode_t mode);
#endif /* !LIBREMAIL_MAILBOX_H */
+12
View File
@@ -0,0 +1,12 @@
#
# Copyright (c) 2026, Chloe Moffett
# Provided under the BSD-3 clause
#
.PHONY: all
all:
cd mailutil/; make
.PHONY: clean
clean:
cd mailutil/; make clean
+34
View File
@@ -0,0 +1,34 @@
#
# Copyright (c) 2026, Chloe Moffett
# Provided under the BSD-3 clause
#
CFILES = $(shell find . -name "*.c")
OFILES = $(CFILES:.c=.o)
OFILES += $(shell find ../../libremail/ -name "*.o")
DFILES = $(CFILES:.c=.d)
OUTPUT = ../../bin/mailutil
CC = gcc
CFLAGS = \
-Wall \
-pedantic \
-MMD \
-Iinc \
-I../../libremail/inc
.PHONY: all
all: $(OUTPUT) $(OFILES)
.PHONY: $(OUTPUT)
$(OUTPUT): $(OFILES)
$(CC) $^ -o $@
-include $(DFILES)
%.o: %.c
$(CC) -c $< $(CFLAGS) -o $@
.PHONY: clean
clean:
rm -f $(OFILES) $(DFILES)
+219
View File
@@ -0,0 +1,219 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include "libremail/file.h"
#include "libremail/common.h"
#include "libremail/mailbox.h"
/* Maximum command components */
#define CMD_MAX_CNP 6
/* Mailutil version */
#define MAILUTIL_VERSION "0.0.1"
/*
* Macro used to assert that the command list count is not
* zero to avoid code duplication.
*/
#define ASSERT_COUNT(CNT) \
if ((CNT) == 0) { \
printf("error: cmdlist count is zero\n"); \
return -1; \
}
/*
* Macro used to ensure that overflows don't happen
*/
#define ASSERT_COUNT_N(CNT, MAX) \
if ((CNT) < (MAX)) { \
printf("error: too few parameters for command\n"); \
return -1; \
}
static void
help(void)
{
printf("usage: ./mailutil [... flags] [verb] [...]\n");
printf("... [-h] Display this help menu\n");
printf("... [-v] Display the version\n");
}
static void
version(void)
{
printf("Version v%s\n", MAILUTIL_VERSION);
}
static int
cmd_mailbox_list(void)
{
DIR *dir;
struct dirent *dirent;
if ((dir = opendir(MAILBOX_PREFIX)) == NULL) {
perror("opendir");
return -1;
}
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.') {
continue;
}
printf("[mailbox] :: %s\n", dirent->d_name);
}
closedir(dir);
return 0;
}
/*
* Parse a "mailbox rm" command
*
* @cmdlist: Command list to parse
* @count: Number of entries inside command list
*
* Returns zero on success
*/
static int
cmd_mailbox_rm(const char *cmdlist[CMD_MAX_CNP], size_t count)
{
char pathbuf[64];
ASSERT_COUNT_N(count, 3);
snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
MAILBOX_PREFIX, cmdlist[2]);
/* Destroy the mailbox */
if (rmdir(pathbuf) < 0) {
perror("rmdir");
return -1;
}
printf("deleted mailbox '%s'\n", cmdlist[2]);
return 0;
}
/*
* Parse a "mailbox create" command
*
* @cmdlist: Command list to parse
* @count: Number of entries inside command list
*
* Returns zero on success
*/
static int
cmd_mailbox_create(const char *cmdlist[CMD_MAX_CNP], size_t count)
{
char pathbuf[64];
ASSERT_COUNT_N(count, 3);
snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
MAILBOX_PREFIX, cmdlist[2]);
/* Create the mailbox */
if (mailbox_create(pathbuf, DEFAULT_MAILBOX_MODE) < 0) {
printf("fatal: failed to create mailbox\n");
perror("try_mkdir");
return -1;
}
printf("created mailbox '%s' @ %s\n", cmdlist[2], pathbuf);
return 0;
}
/*
* Parse a "mailbox" command
*
* @cmdlist: Command list to parse
* @count: Number of entries inside command list
*
* Returns zero on success
*/
static int
cmd_mailbox(const char *cmdlist[CMD_MAX_CNP], size_t count)
{
ASSERT_COUNT_N(count, 2);
if (strcmp(cmdlist[1], "create") == 0) {
return cmd_mailbox_create(cmdlist, count);
}
if (strcmp(cmdlist[1], "list") == 0) {
return cmd_mailbox_list();
}
if (strcmp(cmdlist[1], "rm") == 0) {
return cmd_mailbox_rm(cmdlist, count);
}
printf("error: bad parameter\n");
return -1;
}
/*
* Parse a command given from the command line
*
* @cmdlist: Command list to parse
* @count: Number of entries inside command list
*
* Returns zero on success
*/
static int
parse_cmd(const char *cmdlist[CMD_MAX_CNP], size_t count)
{
ASSERT_COUNT(count);
if (strcmp(cmdlist[0], "mailbox") == 0) {
return cmd_mailbox(cmdlist, count);
}
printf("error: bad parameter\n");
return -1;
}
int
main(int argc, char **argv)
{
int opt;
const char *cmdlist[CMD_MAX_CNP];
size_t cmdlist_i = 0;
if (argc < 2) {
printf("fatal: too few arguments\n");
help();
return -1;
}
while ((opt = getopt(argc, argv, "hv")) != -1) {
switch (opt) {
case 'h':
help();
return -1;
case 'v':
version();
return 0;
}
}
/* Accumulate command line parameters */
while (optind < argc) {
cmdlist[cmdlist_i++] = argv[optind++];
}
if (parse_cmd(cmdlist, cmdlist_i) < 0) {
printf("fatal: error parsing command list\n");
return -1;
}
return 0;
}