#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include <compat/stdbool.h>
#endif
#include <string.h>
#include <limits.h>
#include <time.h>
#include <unistd.h>
#include <sudo_compat.h>
#include <sudo_debug.h>
#include <sudo_fatal.h>
#include <sudo_gettext.h>
#include <sudo_iolog.h>
#include <sudo_util.h>
bool
expand_iolog_path(const char *inpath, char *path, size_t pathlen,
const struct iolog_path_escape *escapes, void *closure)
{
char *dst, *pathend, tmpbuf[PATH_MAX];
const char *endbrace, *src;
bool strfit = false;
size_t len;
debug_decl(expand_iolog_path, SUDO_DEBUG_UTIL);
while (inpath[0] == '/' && inpath[1] == '/')
inpath++;
pathend = path + pathlen;
for (src = inpath, dst = path; *src != '\0'; src++) {
if (src[0] == '%') {
if (src[1] == '{') {
endbrace = strchr(src + 2, '}');
if (endbrace != NULL) {
const struct iolog_path_escape *esc;
len = (size_t)(endbrace - src - 2);
for (esc = escapes; esc->name != NULL; esc++) {
if (strncmp(src + 2, esc->name, len) == 0 &&
esc->name[len] == '\0')
break;
}
if (esc->name != NULL) {
len = esc->copy_fn(dst, (size_t)(pathend - dst),
closure);
if (len >= (size_t)(pathend - dst))
goto bad;
dst += len;
src = endbrace;
continue;
}
}
} else if (src[1] == '%') {
src++;
} else {
strfit = true;
}
}
if (dst + 1 >= pathend)
goto bad;
*dst++ = *src;
}
while (dst > path && dst[-1] == '/')
dst--;
*dst = '\0';
if (strfit) {
struct tm tm;
time_t now;
time(&now);
if (localtime_r(&now, &tm) == NULL)
goto bad;
tmpbuf[sizeof(tmpbuf) - 1] = '\0';
len = strftime(tmpbuf, sizeof(tmpbuf), path, &tm);
if (len == 0 || tmpbuf[sizeof(tmpbuf) - 1] != '\0')
goto bad;
if (len >= (size_t)(pathend - path))
goto bad;
memcpy(path, tmpbuf, len);
dst = path + len;
*dst = '\0';
}
debug_return_bool(true);
bad:
debug_return_bool(false);
}
size_t
strlcpy_no_slash(char * restrict dst, const char * restrict src, size_t size)
{
size_t len = 0;
char ch;
debug_decl(strlcpy_no_slash, SUDO_DEBUG_UTIL);
while ((ch = *src++) != '\0') {
if (size > 1) {
if (ch == '/')
ch = '_';
*dst++ = ch;
size--;
}
len++;
}
if (size > 0)
*dst = '\0';
debug_return_size_t(len);
}