2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-08-22 01:49:11 +00:00
sudo/lib/iolog/regress/iolog_path/check_iolog_path.c

274 lines
6.5 KiB
C
Raw Normal View History

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>
#include <stdlib.h>
#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>
#include <unistd.h>
2011-01-06 11:05:28 -05:00
#define SUDO_ERROR_WRAP 0
#include "sudo_compat.h"
#include "sudo_util.h"
#include "sudo_fatal.h"
#include "sudo_iolog.h"
2011-01-06 11:05:28 -05: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;
sudo_dso_public int main(int argc, char *argv[]);
2011-01-06 11:05:28 -05:00
static void
usage(void)
{
fprintf(stderr, "usage: %s datafile\n", getprogname());
exit(EXIT_FAILURE);
2011-01-06 11:05:28 -05: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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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)
{
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);
if (timeptr == NULL)
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;
initprogname(argc > 0 ? argv[0] : "check_iolog_path");
2011-01-06 11:05:28 -05:00
if (argc != 2)
usage();
fp = fopen(argv[1], "r");
if (fp == NULL)
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:
strlcpy(escape_data.sessid, line, sizeof(escape_data.sessid));
2011-01-06 11:05:28 -05:00
break;
case 1:
if ((escape_data.user = strdup(line)) == NULL)
sudo_fatal(NULL);
2011-01-06 11:05:28 -05:00
break;
case 2:
if ((escape_data.group = strdup(line)) == NULL)
sudo_fatal(NULL);
2011-01-06 11:05:28 -05:00
break;
case 3:
if ((escape_data.runas_user = strdup(line)) == NULL)
sudo_fatal(NULL);
2011-01-06 11:05:28 -05:00
break;
case 4:
if ((escape_data.runas_group = strdup(line)) == NULL)
sudo_fatal(NULL);
2011-01-06 11:05:28 -05:00
break;
case 5:
if ((escape_data.host = strdup(line)) == NULL)
sudo_fatal(NULL);
2011-01-06 11:05:28 -05:00
break;
case 6:
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++;
reset_escape_data(&escape_data);
2011-01-06 11:05:28 -05:00
break;
default:
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);
}