2011-01-06 11:05:28 -05:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2017-12-03 17:53:40 -07:00
|
|
|
* Copyright (c) 2011-2013 Todd C. Miller <Todd.Miller@sudo.ws>
|
2011-01-06 11:05:28 -05:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2020-05-18 07:59:24 -06:00
|
|
|
#include <string.h>
|
2019-10-24 20:04:33 -06:00
|
|
|
#include <limits.h>
|
2011-01-06 11:05:28 -05:00
|
|
|
#include <time.h>
|
2019-10-24 20:04:31 -06:00
|
|
|
#include <unistd.h>
|
2011-01-06 11:05:28 -05:00
|
|
|
|
2011-10-26 10:19:48 -04:00
|
|
|
#define SUDO_ERROR_WRAP 0
|
|
|
|
|
2019-10-24 20:04:31 -06:00
|
|
|
#include "sudo_compat.h"
|
|
|
|
#include "sudo_util.h"
|
|
|
|
#include "sudo_fatal.h"
|
|
|
|
#include "sudo_iolog.h"
|
2011-01-06 11:05:28 -05:00
|
|
|
|
2019-10-24 20:04:31 -06:00
|
|
|
static struct iolog_escape_data {
|
|
|
|
char sessid[7];
|
|
|
|
char *user;
|
|
|
|
char *group;
|
|
|
|
char *runas_user;
|
|
|
|
char *runas_group;
|
|
|
|
char *host;
|
|
|
|
char *command;
|
|
|
|
} escape_data;
|
2011-03-14 11:30:32 -04:00
|
|
|
|
2012-10-02 15:08:02 -04:00
|
|
|
__dso_public int main(int argc, char *argv[]);
|
|
|
|
|
2011-01-06 11:05:28 -05:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-12-01 19:12:21 -07:00
|
|
|
fprintf(stderr, "usage: %s datafile\n", getprogname());
|
2020-02-08 12:43:00 -07:00
|
|
|
exit(EXIT_FAILURE);
|
2011-01-06 11:05:28 -05:00
|
|
|
}
|
|
|
|
|
2019-10-24 20:04:31 -06:00
|
|
|
static void
|
|
|
|
reset_escape_data(struct iolog_escape_data *data)
|
|
|
|
{
|
|
|
|
free(data->user);
|
|
|
|
free(data->group);
|
|
|
|
free(data->runas_user);
|
|
|
|
free(data->runas_group);
|
|
|
|
free(data->host);
|
|
|
|
free(data->command);
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_seq(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* Path is of the form /var/log/sudo-io/00/00/01. */
|
|
|
|
len = snprintf(str, strsize, "%c%c/%c%c/%c%c", escape_data.sessid[0],
|
|
|
|
escape_data.sessid[1], escape_data.sessid[2], escape_data.sessid[3],
|
|
|
|
escape_data.sessid[4], escape_data.sessid[5]);
|
|
|
|
if (len < 0)
|
|
|
|
return strsize; /* handle non-standard snprintf() */
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_user(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.user, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_group(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.group, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_runas_user(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.runas_user, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_runas_group(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.runas_group, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_hostname(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.host, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2019-10-24 20:04:33 -06:00
|
|
|
fill_command(char *str, size_t strsize, void *unused)
|
2019-10-24 20:04:31 -06:00
|
|
|
{
|
|
|
|
return strlcpy(str, escape_data.command, strsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note: "seq" must be first in the list. */
|
|
|
|
static struct iolog_path_escape path_escapes[] = {
|
|
|
|
{ "seq", fill_seq },
|
|
|
|
{ "user", fill_user },
|
|
|
|
{ "group", fill_group },
|
|
|
|
{ "runas_user", fill_runas_user },
|
|
|
|
{ "runas_group", fill_runas_group },
|
|
|
|
{ "hostname", fill_hostname },
|
|
|
|
{ "command", fill_command },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2011-01-06 11:05:28 -05:00
|
|
|
static int
|
|
|
|
do_check(char *dir_in, char *file_in, char *tdir_out, char *tfile_out)
|
|
|
|
{
|
2019-10-24 20:04:33 -06:00
|
|
|
char dir[PATH_MAX], dir_out[PATH_MAX];
|
|
|
|
char file[PATH_MAX], file_out[PATH_MAX];
|
2011-01-06 11:05:28 -05:00
|
|
|
struct tm *timeptr;
|
|
|
|
time_t now;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand any strftime(3) escapes
|
|
|
|
* XXX - want to pass timeptr to expand_iolog_path
|
|
|
|
*/
|
|
|
|
time(&now);
|
|
|
|
timeptr = localtime(&now);
|
2014-01-21 16:32:00 -07:00
|
|
|
if (timeptr == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx("localtime returned NULL");
|
2011-01-06 11:05:28 -05:00
|
|
|
strftime(dir_out, sizeof(dir_out), tdir_out, timeptr);
|
|
|
|
strftime(file_out, sizeof(file_out), tfile_out, timeptr);
|
|
|
|
|
2019-10-24 20:04:33 -06:00
|
|
|
if (!expand_iolog_path(dir_in, dir, sizeof(dir), &path_escapes[1], NULL))
|
|
|
|
sudo_fatalx("unable to expand I/O log dir");
|
|
|
|
if (!expand_iolog_path(file_in, file, sizeof(file), &path_escapes[0], dir))
|
|
|
|
sudo_fatalx("unable to expand I/O log file");
|
|
|
|
|
|
|
|
if (strcmp(dir, dir_out) != 0) {
|
|
|
|
sudo_warnx("%s: expected %s, got %s", dir_in, dir_out, dir);
|
2011-01-06 11:05:28 -05:00
|
|
|
error = 1;
|
|
|
|
}
|
2019-10-24 20:04:33 -06:00
|
|
|
if (strcmp(file, file_out) != 0) {
|
|
|
|
sudo_warnx("%s: expected %s, got %s", file_in, file_out, file);
|
2011-01-06 11:05:28 -05:00
|
|
|
error = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_STATE 12
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
FILE *fp;
|
|
|
|
char line[2048];
|
|
|
|
char *file_in = NULL, *file_out = NULL;
|
|
|
|
char *dir_in = NULL, *dir_out = NULL;
|
|
|
|
int state = 0;
|
|
|
|
int errors = 0;
|
|
|
|
int tests = 0;
|
|
|
|
|
2013-12-01 19:12:21 -07:00
|
|
|
initprogname(argc > 0 ? argv[0] : "check_iolog_path");
|
2011-08-29 14:51:12 -04:00
|
|
|
|
2011-01-06 11:05:28 -05:00
|
|
|
if (argc != 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
fp = fopen(argv[1], "r");
|
|
|
|
if (fp == NULL)
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx("unable to open %s", argv[1]);
|
2011-01-06 11:05:28 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Input consists of 12 lines:
|
|
|
|
* sequence number
|
|
|
|
* user name
|
|
|
|
* user gid
|
|
|
|
* runas user name
|
|
|
|
* runas gid
|
|
|
|
* hostname [short form]
|
|
|
|
* command
|
|
|
|
* dir [with escapes]
|
|
|
|
* file [with escapes]
|
|
|
|
* expanded dir
|
|
|
|
* expanded file
|
|
|
|
* empty line
|
|
|
|
*/
|
|
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
|
|
|
len = strcspn(line, "\n");
|
|
|
|
line[len] = '\0';
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case 0:
|
2019-10-24 20:04:31 -06:00
|
|
|
strlcpy(escape_data.sessid, line, sizeof(escape_data.sessid));
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 1:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.user = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 2:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.group = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 3:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.runas_user = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 4:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.runas_group = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 5:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.host = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 6:
|
2019-10-24 20:04:31 -06:00
|
|
|
if ((escape_data.command = strdup(line)) == NULL)
|
|
|
|
sudo_fatal(NULL);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
case 7:
|
2016-01-29 11:34:09 -07:00
|
|
|
if (dir_in != NULL)
|
|
|
|
free(dir_in);
|
2011-01-06 11:05:28 -05:00
|
|
|
dir_in = strdup(line);
|
|
|
|
break;
|
|
|
|
case 8:
|
2016-01-29 11:34:09 -07:00
|
|
|
if (file_in != NULL)
|
|
|
|
free(file_in);
|
2011-01-06 11:05:28 -05:00
|
|
|
file_in = strdup(line);
|
|
|
|
break;
|
|
|
|
case 9:
|
2016-01-29 11:34:09 -07:00
|
|
|
if (dir_out != NULL)
|
|
|
|
free(dir_out);
|
2011-01-06 11:05:28 -05:00
|
|
|
dir_out = strdup(line);
|
|
|
|
break;
|
|
|
|
case 10:
|
2016-01-29 11:34:09 -07:00
|
|
|
if (file_out != NULL)
|
|
|
|
free(file_out);
|
2011-01-06 11:05:28 -05:00
|
|
|
file_out = strdup(line);
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
errors += do_check(dir_in, file_in, dir_out, file_out);
|
|
|
|
tests++;
|
2019-10-24 20:04:31 -06:00
|
|
|
reset_escape_data(&escape_data);
|
2011-01-06 11:05:28 -05:00
|
|
|
break;
|
|
|
|
default:
|
2014-06-27 09:30:52 -06:00
|
|
|
sudo_fatalx("internal error, invalid state %d", state);
|
2011-01-06 11:05:28 -05:00
|
|
|
}
|
|
|
|
state = (state + 1) % MAX_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tests != 0) {
|
|
|
|
printf("iolog_path: %d test%s run, %d errors, %d%% success rate\n",
|
|
|
|
tests, tests == 1 ? "" : "s", errors,
|
|
|
|
(tests - errors) * 100 / tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(errors);
|
|
|
|
}
|