Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/lib/eventlog/regress/logwrap/check_wrap.c
1532 views
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (c) 2011-2013 Todd C. Miller <[email protected]>
5
*
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
9
*
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
19
#include <config.h>
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
#include <limits.h>
25
#include <unistd.h>
26
27
#define SUDO_ERROR_WRAP 0
28
29
#include <sudo_compat.h>
30
#include <sudo_eventlog.h>
31
#include <sudo_fatal.h>
32
#include <sudo_plugin.h>
33
#include <sudo_util.h>
34
35
sudo_dso_public int main(int argc, char *argv[]);
36
37
sudo_noreturn static void
38
usage(void)
39
{
40
fprintf(stderr, "usage: %s [-v] inputfile\n", getprogname());
41
exit(EXIT_FAILURE);
42
}
43
44
int
45
main(int argc, char *argv[])
46
{
47
int ch, lineno = 0, which = 0;
48
char *line, lines[2][2048];
49
const char *infile;
50
unsigned int len;
51
FILE *fp;
52
53
initprogname(argc > 0 ? argv[0] : "check_wrap");
54
55
while ((ch = getopt(argc, argv, "v")) != -1) {
56
switch (ch) {
57
case 'v':
58
/* ignored */
59
break;
60
default:
61
usage();
62
/* NOTREACHED */
63
}
64
}
65
argc -= optind;
66
argv += optind;
67
68
if (argc != 1)
69
usage();
70
infile = argv[0];
71
72
fp = fopen(infile, "r");
73
if (fp == NULL)
74
sudo_fatalx("unable to open %s", infile);
75
76
/*
77
* Each test record consists of a log entry on one line and a list of
78
* line lengths to test it with on the next. E.g.
79
*
80
* Jun 30 14:49:51 : millert : TTY=ttypn ; PWD=/usr/src/local/millert/hg/sudo/trunk/plugins/sudoers ; USER=root ; TSID=0004LD ; COMMAND=/usr/local/sbin/visudo
81
* 60-80,40
82
*/
83
while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {
84
char *cp, *last;
85
86
line[strcspn(line, "\n")] = '\0';
87
88
/* If we read the 2nd line, parse list of line lengths and check. */
89
if (which) {
90
lineno++;
91
for (cp = strtok_r(lines[1], ",", &last); cp != NULL; cp = strtok_r(NULL, ",", &last)) {
92
unsigned int maxlen;
93
const char *errstr;
94
char *dash;
95
96
/* May be either a number or a range. */
97
dash = strchr(cp, '-');
98
if (dash != NULL) {
99
*dash = '\0';
100
len = (unsigned int)sudo_strtonum(cp, 0, INT_MAX, &errstr);
101
if (errstr == NULL)
102
maxlen = (unsigned int)sudo_strtonum(dash + 1, 0, INT_MAX, &errstr);
103
} else {
104
len = maxlen = (unsigned int)sudo_strtonum(cp, 0, INT_MAX, &errstr);
105
}
106
if (errstr != NULL) {
107
sudo_fatalx("%s: invalid length on line %d", infile, lineno);
108
}
109
while (len <= maxlen) {
110
if (len == 0) {
111
puts("# word wrap disabled");
112
} else {
113
printf("# word wrap at %u characters\n", len);
114
}
115
eventlog_writeln(stdout, lines[0], strlen(lines[0]), len);
116
len++;
117
}
118
}
119
}
120
which = !which;
121
}
122
123
return EXIT_SUCCESS;
124
}
125
126