FreeBSD: Limit rights on save file

master
C. McEnroe 2021-06-24 18:07:30 -04:00
parent 1239ffa689
commit b48d0d607c
1 changed files with 13 additions and 1 deletions

14
ui.c
View File

@ -48,6 +48,10 @@
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#ifdef __FreeBSD__
#include <sys/capsicum.h>
#endif
#include "chat.h" #include "chat.h"
// Annoying stuff from <term.h>: // Annoying stuff from <term.h>:
@ -1181,11 +1185,19 @@ static ssize_t readString(FILE *file, char **buf, size_t *cap) {
} }
void uiLoad(const char *name) { void uiLoad(const char *name) {
int error;
saveFile = dataOpen(name, "a+e"); saveFile = dataOpen(name, "a+e");
if (!saveFile) exit(EX_CANTCREAT); if (!saveFile) exit(EX_CANTCREAT);
rewind(saveFile); rewind(saveFile);
int error = flock(fileno(saveFile), LOCK_EX | LOCK_NB); #ifdef __FreeBSD__
cap_rights_t rights;
cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_FLOCK, CAP_FTRUNCATE);
error = cap_rights_limit(fileno(saveFile), &rights);
if (error) err(EX_OSERR, "cap_rights_limit");
#endif
error = flock(fileno(saveFile), LOCK_EX | LOCK_NB);
if (error && errno == EWOULDBLOCK) { if (error && errno == EWOULDBLOCK) {
errx(EX_CANTCREAT, "%s: save file in use", name); errx(EX_CANTCREAT, "%s: save file in use", name);
} }