made EOH optional

This commit is contained in:
chickfilla 2021-10-08 19:13:24 -04:00
parent c7694dee5d
commit fd2f12195b
4 changed files with 63 additions and 30 deletions

View File

@ -1,7 +1,7 @@
TARGET ?= tildebin
SRC_DIRS ?= ./
CFLAGS := -g
CFLAGS := -g -Wall -Wpedantic
SRCS := $(shell find $(SRC_DIRS) -name *.c)
OBJS := $(addsuffix .o,$(basename $(SRCS)))

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# tildebin
`tildebin` creates customizable [pastebin](https://pastebin.com/) pages from plain text commands received via a unix sockets. It sets up a socket within your filesystem to which users can connect to with tools like `nc` and send their requests. Requests are then applied to a template and saved to disk.
## Example
```bash
# Session 1 starts the server...
./tildebin
# Session 2 sends a request...
echo "My Request!"
```

View File

@ -140,8 +140,10 @@ static int
serve( int fd, const struct uinfo *ui )
{
int i;
bool found_header;
ssize_t len, wlen;
size_t fn_size;
const char * fmt;
char *op, *cp, *np, *st, *id;
len = 0;
@ -162,27 +164,29 @@ serve( int fd, const struct uinfo *ui )
INFO("received request from user \"%s\"", ui->name);
client_buffer[len -1] = '\0';
if((id = strstr(client_buffer, IDENTIFIER)) == NULL)
found_header = ((id = strstr(client_buffer, IDENTIFIER)) != NULL);
if(found_header)
{
ERROR("cannot parse request from user \"%s\", identifier is missing", ui->name);
return -1;
}
*id = '\0';
cp = client_buffer;
if(
(cp = strstr(cp, ".name.")) != NULL &&
(np = strstr(cp, ".ename.")) != NULL
)
{
*np = '\0';
cp += sizeof(".name.")-1;
fn_size += strlen(cp) + strlen(output_dir) + 3;
INFO("found request header");
*id = '\0';
}
else
id = client_buffer;
fn_size += strlen(output_dir) + 2;
if(found_header)
{
cp = "";
fn_size += strlen(output_dir) + 2;
cp = client_buffer;
if(
(cp = strstr(cp, ".name.")) != NULL &&
(np = strstr(cp, ".ename.")) != NULL
)
{
*np = '\0';
cp = &cp[GET_LEN(".name.")-1];
fn_size += strlen(cp) + strlen(output_dir) + 3;
}
}
// Make sure path string is big enough
@ -190,7 +194,9 @@ serve( int fd, const struct uinfo *ui )
MEM_CATCH(string_reserve(&output_path, fn_size+1));
// Create user directory if it doesn't exist
snprintf(string_get_data(&output_path), fn_size, "%s/%s/%s/", output_dir, ui->name, cp);
fmt = cp == NULL ? "%s/%s/%s" : "%s/%s/%s/";
cp = cp == NULL ? "" : cp;
snprintf(string_get_data(&output_path), fn_size, fmt, output_dir, ui->name, cp);
create_directory(string_get_data(&output_path));
// Get full path
@ -208,7 +214,11 @@ serve( int fd, const struct uinfo *ui )
"cannot set file permissions"
);
st = id + GET_LEN(IDENTIFIER) - 1;
if(found_header)
st = &id[GET_LEN(IDENTIFIER) - 1];
else
st = id;
op = cp = (char *)template;
len = strlen(op);
while (cp != NULL && cp < &template[len-1])
@ -217,7 +227,7 @@ serve( int fd, const struct uinfo *ui )
if((cp = strstr(cp, "{{")) != NULL && (np = strstr(cp, "}}")) != NULL)
{
fwrite(op, sizeof(char), cp - op, output_fd);
cp += sizeof("{{") -1;
cp += sizeof("{{") - 1*sizeof(char);
wlen = np - cp;
if(strncmp(cp, "client", wlen) == 0)
fprintf(output_fd, "%s", ui->name);
@ -246,12 +256,12 @@ serve( int fd, const struct uinfo *ui )
continue;
}
*st = '\0';
st = cp+1;
st = &cp[1];
}
}
fprintf(output_fd, "%s", st);
}
cp = np + sizeof("}}") -1;
cp = &np[GET_LEN("}}") -1];
}
else
{
@ -333,14 +343,14 @@ int
main( int argc, char const **argv )
{
int max_fd = 0, backlog = 0, i = 0, s = 0;
struct sockaddr_un s_addr = {};
struct timeval tv = {};
struct uinfo ui = {};
struct sockaddr_un s_addr;
struct timeval tv;
struct uinfo ui;
size_t len = 0;
const char* socket_path = NULL, *template_path = NULL;
FILE *template_file = NULL;
mode_t ou = 0;
fd_set client_sockets_set = {};
fd_set client_sockets_set;
VALIDATE((passwd = getpwuid(getuid())) != NULL, "cannot get user info");
@ -358,8 +368,14 @@ main( int argc, char const **argv )
string_init(&output_path);
string_init(&custom_template);
memset(client_buffer, 0, sizeof(client_buffer));
memset(client_sockets, 0, sizeof(client_sockets));
MEMSET_ZERO(client_buffer);
MEMSET_ZERO(client_sockets);
MEMSET_ZERO(s_addr);
MEMSET_ZERO(tv);
MEMSET_ZERO(ui);
MEMSET_ZERO(client_sockets_set);
// To stop gcc from giving me boggus warnings
UNUSED(string_shrink);
@ -413,8 +429,9 @@ main( int argc, char const **argv )
custom_template.data[len-1] = '\0';
template = custom_template.data;
fclose(template_file);
INFO("loaded template from file \"%s\"", template_path);
}
// Create output directory if it doesn't exists
@ -447,6 +464,8 @@ main( int argc, char const **argv )
"cannot set socket backlog"
);
INFO("listening to socket on \"%s\"", socket_path);
// Main loop
keep_running = true;
while(keep_running)

View File

@ -121,6 +121,8 @@ extern int errno;
return v->used/sizeof(T); \
}
#define MEMSET_ZERO(_v) memset(&_v, 0, sizeof(_v))
#define CHECK_MASK(_value, _mask) (((_value) & (_mask)) == 0)
#define MAX(a, b) ((a) >= (b) ? (a): (b))