2022-08-17 19:28:09 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2021-01-14 12:31:51 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2022-08-17 19:28:09 +00:00
|
|
|
#include <regex.h>
|
2021-01-14 12:31:51 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
2022-08-08 20:57:03 +00:00
|
|
|
#include "vger.h"
|
2021-01-14 12:31:51 +00:00
|
|
|
|
2022-01-26 12:04:04 +00:00
|
|
|
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined( __NetBSD__) || defined(__DragonFly__)
|
2021-03-14 13:18:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#include <bsd/string.h>
|
|
|
|
#endif
|
|
|
|
|
2021-03-22 20:44:23 +00:00
|
|
|
/* e*foo() functions are the equivalent of foo() but handle errors.
|
|
|
|
* In case an error happens:
|
|
|
|
* The error is printed to stdout
|
|
|
|
* return 1
|
|
|
|
*/
|
|
|
|
|
2021-02-03 19:46:36 +00:00
|
|
|
#ifdef __OpenBSD__
|
2021-01-14 12:31:51 +00:00
|
|
|
void
|
|
|
|
eunveil(const char *path, const char *permissions)
|
|
|
|
{
|
|
|
|
if (unveil(path, permissions) == -1) {
|
2022-08-08 20:57:03 +00:00
|
|
|
status(41, "Error when unveil(), see logs");
|
|
|
|
stop(EXIT_FAILURE, "unveil on %s failed", path);
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
epledge(const char *promises, const char *execpromises)
|
|
|
|
{
|
|
|
|
if (pledge(promises, execpromises) == -1) {
|
2022-08-08 20:57:03 +00:00
|
|
|
status(41, "Error when pledge(), see logs");
|
|
|
|
stop(EXIT_FAILURE, "pledge failed for: %s", promises);
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-03 19:46:36 +00:00
|
|
|
#endif
|
2021-01-14 12:31:51 +00:00
|
|
|
|
|
|
|
size_t
|
2022-08-20 13:10:05 +00:00
|
|
|
esnprintf(char *str, size_t size, const char *format, ...)
|
2021-01-14 12:31:51 +00:00
|
|
|
{
|
2022-08-20 13:10:05 +00:00
|
|
|
/* usage : esnprintf(str, sizeof(str), "%s ... %s", arg1, arg2); */
|
|
|
|
va_list ap;
|
|
|
|
size_t ret = 0;
|
2021-01-14 12:31:51 +00:00
|
|
|
|
2022-08-20 13:10:05 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
ret = vsnprintf(str, size, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (ret < 0 || ret >= size) {
|
|
|
|
status(41, "vnsprintf failed: Output trunkated");
|
|
|
|
stop(EXIT_FAILURE, "vsnprintf: Output trunkated");
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 13:10:05 +00:00
|
|
|
return ret;
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
esetenv(const char *name, const char *value, int overwrite)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
ret = setenv(name, value, overwrite);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2022-08-08 20:57:03 +00:00
|
|
|
status(41, "setenv() failed, see logs");
|
|
|
|
stop(EXIT_FAILURE, "setenv() %s:%s", name, value);
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-08-08 20:57:03 +00:00
|
|
|
echdir(const char *path)
|
2021-01-14 12:31:51 +00:00
|
|
|
{
|
2022-08-08 20:57:03 +00:00
|
|
|
if (chdir(path) == -1) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOTDIR: /* FALLTHROUGH */
|
|
|
|
case ENOENT:
|
|
|
|
status(51, "file not found");
|
|
|
|
break;
|
|
|
|
case EACCES:
|
|
|
|
status(50, "Forbidden path");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
status(50, "Internal server error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stop(EXIT_FAILURE, "chdir(%s) failed", path);
|
|
|
|
}
|
|
|
|
}
|
2021-04-30 10:45:34 +00:00
|
|
|
|
2022-08-08 20:57:03 +00:00
|
|
|
/* read the file fd byte after byte in buffer and write it to stdout
|
|
|
|
* return number of bytes read
|
|
|
|
*/
|
|
|
|
size_t
|
|
|
|
print_file(FILE *fd)
|
|
|
|
{
|
|
|
|
ssize_t nread = 0;
|
2022-08-08 21:16:55 +00:00
|
|
|
ssize_t datasent = 0;
|
2022-08-08 20:57:03 +00:00
|
|
|
char *buffer[BUFSIZ];
|
2021-01-14 12:31:51 +00:00
|
|
|
|
2022-08-08 20:57:03 +00:00
|
|
|
while ((nread = fread(buffer, 1, sizeof(buffer), fd)) != 0)
|
2022-08-08 21:16:55 +00:00
|
|
|
datasent += fwrite(buffer, 1, nread, stdout);
|
|
|
|
return datasent;
|
2021-01-14 12:31:51 +00:00
|
|
|
}
|
2022-08-17 19:28:09 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
getsubexp(const char *str, regmatch_t m, char *dst)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
|
|
|
|
2022-08-18 13:27:44 +00:00
|
|
|
if ((len = m.rm_eo - m.rm_so) > 0) { /* skip empty substring */
|
2022-08-17 19:28:09 +00:00
|
|
|
len = m.rm_eo - m.rm_so;
|
|
|
|
memcpy(dst, str + m.rm_so, len);
|
|
|
|
dst[len] = '\0';
|
|
|
|
}
|
|
|
|
}
|