2020-03-25 22:56:09 +00:00
|
|
|
/* Copyright (C) 2020 C. McEnroe <june@causal.agency>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-06-08 21:48:07 +00:00
|
|
|
*
|
|
|
|
* Additional permission under GNU GPL version 3 section 7:
|
|
|
|
*
|
|
|
|
* If you modify this Program, or any covered work, by linking or
|
2020-08-04 16:19:14 +00:00
|
|
|
* combining it with OpenSSL (or a modified version of that library),
|
2020-06-08 21:48:07 +00:00
|
|
|
* containing parts covered by the terms of the OpenSSL License and the
|
|
|
|
* original SSLeay license, the licensors of this Program grant you
|
|
|
|
* additional permission to convey the resulting work. Corresponding
|
|
|
|
* Source for a non-source form of such a combination shall include the
|
2020-08-04 16:19:14 +00:00
|
|
|
* source code for the parts of OpenSSL used as well as that of the
|
2020-06-08 21:48:07 +00:00
|
|
|
* covered work.
|
2020-03-25 22:56:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <err.h>
|
2021-06-24 20:58:28 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2020-03-25 22:56:09 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-06-24 20:58:28 +00:00
|
|
|
#include <sys/stat.h>
|
2020-03-25 22:56:09 +00:00
|
|
|
#include <sysexits.h>
|
|
|
|
#include <time.h>
|
2021-06-24 20:58:28 +00:00
|
|
|
#include <unistd.h>
|
2020-03-25 22:56:09 +00:00
|
|
|
|
2021-06-24 22:17:52 +00:00
|
|
|
#ifdef __FreeBSD__
|
2021-06-28 13:11:02 +00:00
|
|
|
#include <capsicum_helpers.h>
|
2021-06-24 22:17:52 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-25 22:56:09 +00:00
|
|
|
#include "chat.h"
|
|
|
|
|
2021-06-24 20:58:28 +00:00
|
|
|
static int logDir = -1;
|
|
|
|
|
|
|
|
void logOpen(void) {
|
2021-10-16 21:03:31 +00:00
|
|
|
char buf[PATH_MAX];
|
|
|
|
int error = mkdir(dataPath(buf, sizeof(buf), "", 0), S_IRWXU);
|
|
|
|
if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", buf);
|
|
|
|
|
|
|
|
error = mkdir(dataPath(buf, sizeof(buf), "log", 0), S_IRWXU);
|
|
|
|
if (error && errno != EEXIST) err(EX_CANTCREAT, "%s", buf);
|
|
|
|
|
|
|
|
logDir = open(buf, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (logDir < 0) err(EX_CANTCREAT, "%s", buf);
|
2021-06-24 22:17:52 +00:00
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
cap_rights_t rights;
|
2021-07-13 17:58:14 +00:00
|
|
|
cap_rights_init(
|
|
|
|
&rights, CAP_MKDIRAT, CAP_CREATE, CAP_WRITE,
|
|
|
|
/* for fdopen(3) */ CAP_FCNTL, CAP_FSTAT
|
|
|
|
);
|
2021-10-16 21:03:31 +00:00
|
|
|
error = caph_rights_limit(logDir, &rights);
|
2021-06-24 22:17:52 +00:00
|
|
|
if (error) err(EX_OSERR, "cap_rights_limit");
|
|
|
|
#endif
|
2021-06-24 20:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void logMkdir(const char *path) {
|
|
|
|
int error = mkdirat(logDir, path, S_IRWXU);
|
|
|
|
if (error && errno != EEXIST) err(EX_CANTCREAT, "log/%s", path);
|
|
|
|
}
|
2020-03-25 22:56:09 +00:00
|
|
|
|
2022-04-20 22:29:28 +00:00
|
|
|
static void sanitize(char *ptr, char *end) {
|
|
|
|
for (char *ch = ptr; ch < end && *ch == '.'; ++ch) {
|
|
|
|
*ch = '_';
|
|
|
|
}
|
|
|
|
for (char *ch = ptr; ch < end; ++ch) {
|
|
|
|
if (*ch == '/') *ch = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:56:09 +00:00
|
|
|
static struct {
|
|
|
|
int year;
|
|
|
|
int month;
|
|
|
|
int day;
|
|
|
|
FILE *file;
|
|
|
|
} logs[IDCap];
|
|
|
|
|
|
|
|
static FILE *logFile(uint id, const struct tm *tm) {
|
|
|
|
if (
|
|
|
|
logs[id].file &&
|
|
|
|
logs[id].year == tm->tm_year &&
|
|
|
|
logs[id].month == tm->tm_mon &&
|
|
|
|
logs[id].day == tm->tm_mday
|
|
|
|
) return logs[id].file;
|
|
|
|
|
|
|
|
if (logs[id].file) {
|
|
|
|
int error = fclose(logs[id].file);
|
|
|
|
if (error) err(EX_IOERR, "%s", idNames[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
logs[id].year = tm->tm_year;
|
|
|
|
logs[id].month = tm->tm_mon;
|
|
|
|
logs[id].day = tm->tm_mday;
|
|
|
|
|
2021-06-24 20:58:28 +00:00
|
|
|
char path[PATH_MAX];
|
2022-04-20 22:29:28 +00:00
|
|
|
char *ptr = path, *end = &path[sizeof(path)];
|
|
|
|
|
|
|
|
ptr = seprintf(ptr, end, "%s", network.name);
|
|
|
|
sanitize(path, ptr);
|
2021-06-24 20:58:28 +00:00
|
|
|
logMkdir(path);
|
2020-03-25 22:56:09 +00:00
|
|
|
|
2022-04-20 22:29:28 +00:00
|
|
|
char *name = ptr;
|
|
|
|
ptr = seprintf(ptr, end, "/%s", idNames[id]);
|
|
|
|
sanitize(&name[1], ptr);
|
2021-06-24 20:58:28 +00:00
|
|
|
logMkdir(path);
|
2020-03-25 22:56:09 +00:00
|
|
|
|
2022-04-20 22:29:28 +00:00
|
|
|
size_t len = strftime(ptr, end - ptr, "/%F.log", tm);
|
|
|
|
if (!len) errx(EX_CANTCREAT, "log path too long");
|
|
|
|
|
2021-06-24 20:58:28 +00:00
|
|
|
int fd = openat(
|
|
|
|
logDir, path,
|
|
|
|
O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC,
|
|
|
|
S_IRUSR | S_IWUSR
|
|
|
|
);
|
|
|
|
if (fd < 0) err(EX_CANTCREAT, "log/%s", path);
|
|
|
|
logs[id].file = fdopen(fd, "a");
|
|
|
|
if (!logs[id].file) err(EX_OSERR, "fdopen");
|
2020-03-25 22:56:09 +00:00
|
|
|
|
|
|
|
setlinebuf(logs[id].file);
|
|
|
|
return logs[id].file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logClose(void) {
|
2021-06-24 20:58:28 +00:00
|
|
|
if (logDir < 0) return;
|
2020-03-25 22:56:09 +00:00
|
|
|
for (uint id = 0; id < IDCap; ++id) {
|
|
|
|
if (!logs[id].file) continue;
|
|
|
|
int error = fclose(logs[id].file);
|
|
|
|
if (error) err(EX_IOERR, "%s", idNames[id]);
|
|
|
|
}
|
2021-06-24 20:58:28 +00:00
|
|
|
close(logDir);
|
2020-03-25 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void logFormat(uint id, const time_t *src, const char *format, ...) {
|
2021-06-24 20:58:28 +00:00
|
|
|
if (logDir < 0) return;
|
2020-03-25 22:56:09 +00:00
|
|
|
|
|
|
|
time_t ts = (src ? *src : time(NULL));
|
|
|
|
struct tm *tm = localtime(&ts);
|
|
|
|
if (!tm) err(EX_OSERR, "localtime");
|
|
|
|
|
|
|
|
FILE *file = logFile(id, tm);
|
|
|
|
|
|
|
|
char buf[sizeof("0000-00-00T00:00:00+0000")];
|
|
|
|
strftime(buf, sizeof(buf), "%FT%T%z", tm);
|
2021-01-11 03:00:40 +00:00
|
|
|
int n = fprintf(file, "[%s] ", buf);
|
|
|
|
if (n < 0) err(EX_IOERR, "%s", idNames[id]);
|
2020-03-25 22:56:09 +00:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2021-01-11 03:00:40 +00:00
|
|
|
n = vfprintf(file, format, ap);
|
2020-03-25 22:56:09 +00:00
|
|
|
va_end(ap);
|
2021-01-11 03:00:40 +00:00
|
|
|
if (n < 0) err(EX_IOERR, "%s", idNames[id]);
|
2020-03-25 22:56:09 +00:00
|
|
|
|
2021-01-11 03:00:40 +00:00
|
|
|
n = fprintf(file, "\n");
|
|
|
|
if (n < 0) err(EX_IOERR, "%s", idNames[id]);
|
2020-03-25 22:56:09 +00:00
|
|
|
}
|