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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2021-2022 Todd C. Miller <Todd.Miller@sudo.ws>
*
* 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.
*/
/*
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
*/
#include <config.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
# include "compat/stdbool.h"
#endif /* HAVE_STDBOOL_H */
#if defined(HAVE_STDINT_H)
# include <stdint.h>
#elif defined(HAVE_INTTYPES_H)
# include <inttypes.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "sudo_compat.h"
#include "sudo_conf.h"
#include "sudo_debug.h"
#include "sudo_event.h"
#include "sudo_eventlog.h"
#include "sudo_fatal.h"
#include "sudo_gettext.h"
#include "sudo_iolog.h"
#include "sudo_util.h"
#include "logsrvd.h"
/*
* Helper function to set closure->journal and closure->journal_path.
*/
static bool
journal_fdopen(int fd, const char *journal_path,
struct connection_closure *closure)
{
debug_decl(journal_fdopen, SUDO_DEBUG_UTIL);
free(closure->journal_path);
closure->journal_path = strdup(journal_path);
if (closure->journal_path == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_bool(false);
}
/* Defer fdopen() until last--it cannot be undone. */
if (closure->journal != NULL)
fclose(closure->journal);
if ((closure->journal = fdopen(fd, "r+")) == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to fdopen journal file %s", journal_path);
debug_return_bool(false);
}
debug_return_bool(true);
}
static int
journal_mkstemp(const char *parent_dir, char *pathbuf, int pathlen)
{
int len, dfd = -1, fd = -1;
mode_t dirmode, oldmask;
char *template;
debug_decl(journal_mkstemp, SUDO_DEBUG_UTIL);
/* umask must not be more restrictive than the file modes. */
dirmode = logsrvd_conf_iolog_mode() | S_IXUSR;
if (dirmode & (S_IRGRP|S_IWGRP))
dirmode |= S_IXGRP;
if (dirmode & (S_IROTH|S_IWOTH))
dirmode |= S_IXOTH;
oldmask = umask(ACCESSPERMS & ~dirmode);
len = snprintf(pathbuf, pathlen, "%s/%s/%s",
logsrvd_conf_relay_dir(), parent_dir, RELAY_TEMPLATE);
if (len >= pathlen) {
errno = ENAMETOOLONG;
sudo_warn("%s/%s/%s", logsrvd_conf_relay_dir(), parent_dir,
RELAY_TEMPLATE);
goto done;
}
dfd = sudo_open_parent_dir(pathbuf, logsrvd_conf_iolog_uid(),
logsrvd_conf_iolog_gid(), S_IRWXU|S_IXGRP|S_IXOTH, false);
if (dfd == -1) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to create parent dir for %s", pathbuf);
goto done;
}
template = pathbuf + (len - strlen(RELAY_TEMPLATE));
if ((fd = mkostempsat(dfd, template, 0, 0)) == -1) {
sudo_warn(U_("%s: %s"), "mkstemp", pathbuf);
goto done;
}
done:
umask(oldmask);
if (dfd != -1)
close(dfd);
debug_return_int(fd);
}
/*
* Create a temporary file in the relay dir and store it in the closure.
*/
static bool
journal_create(struct connection_closure *closure)
{
char journal_path[PATH_MAX];
int fd;
debug_decl(journal_create, SUDO_DEBUG_UTIL);
fd = journal_mkstemp("incoming", journal_path, sizeof(journal_path));
if (fd == -1) {
closure->errstr = _("unable to create journal file");
debug_return_bool(false);
}
if (!sudo_lock_file(fd, SUDO_TLOCK)) {
sudo_warn(U_("unable to lock %s"), journal_path);
unlink(journal_path);
close(fd);
closure->errstr = _("unable to lock journal file");
debug_return_bool(false);
}
if (!journal_fdopen(fd, journal_path, closure)) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to fdopen journal file %s", journal_path);
unlink(journal_path);
close(fd);
closure->errstr = _("unable to open journal file");
debug_return_bool(false);
}
debug_return_bool(true);
}
/*
* Flush any buffered data, rewind journal to the beginning and
* move to the outgoing directory.
* The actual open file is closed in connection_closure_free().
*/
static bool
journal_finish(struct connection_closure *closure)
{
char outgoing_path[PATH_MAX];
size_t len;
int fd;
debug_decl(journal_finish, SUDO_DEBUG_UTIL);
if (fflush(closure->journal) != 0) {
closure->errstr = _("unable to write journal file");
debug_return_bool(false);
}
rewind(closure->journal);
/* Move journal to the outgoing directory. */
fd = journal_mkstemp("outgoing", outgoing_path, sizeof(outgoing_path));
if (fd == -1) {
closure->errstr = _("unable to rename journal file");
debug_return_bool(false);
}
close(fd);
if (rename(closure->journal_path, outgoing_path) == -1) {
sudo_warn(U_("unable to rename %s to %s"), closure->journal_path,
outgoing_path);
closure->errstr = _("unable to rename journal file");
unlink(outgoing_path);
debug_return_bool(false);
}
sudo_debug_printf(SUDO_DEBUG_INFO|SUDO_DEBUG_LINENO,
"renamed %s -> %s", closure->journal_path, outgoing_path);
len = strlen(outgoing_path);
if (strlen(closure->journal_path) == len) {
/* This should always be true. */
memcpy(closure->journal_path, outgoing_path, len);
} else {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
"length mismatch %zu != %zu", strlen(closure->journal_path), len);
free(closure->journal_path);
closure->journal_path = strdup(outgoing_path);
if (closure->journal_path == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
closure->errstr = _("unable to allocate memory");
debug_return_bool(false);
}
}
debug_return_bool(true);
}
/*
* Seek ahead in the journal to the specified target time.
* Returns true if we reached the target time exactly, else false.
*/
static bool
journal_seek(struct timespec *target, struct connection_closure *closure)
{
ClientMessage *msg = NULL;
size_t nread, bufsize = 0;
uint8_t *buf = NULL;
uint32_t msg_len;
bool ret = false;
debug_decl(journal_seek, SUDO_DEBUG_UTIL);
for (;;) {
TimeSpec *delay = NULL;
/* Read message size (uint32_t in network byte order). */
nread = fread(&msg_len, sizeof(msg_len), 1, closure->journal);
if (nread != 1) {
if (feof(closure->journal)) {
sudo_warnx(U_("%s: %s"), closure->journal_path,
U_("unexpected EOF reading journal file"));
closure->errstr = _("unexpected EOF reading journal file");
} else {
sudo_warn(U_("%s: %s"), closure->journal_path,
U_("error reading journal file"));
closure->errstr = _("error reading journal file");
}
break;
}
msg_len = ntohl(msg_len);
if (msg_len > MESSAGE_SIZE_MAX) {
sudo_warnx(U_("%s: %s"), closure->journal_path,
U_("client message too large"));
closure->errstr = _("client message too large");
break;
}
/* Read actual message now that we know the size. */
if (msg_len != 0) {
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"%s: reading message %u bytes", closure->journal_path, msg_len);
if (msg_len > bufsize) {
bufsize = sudo_pow2_roundup(msg_len);
free(buf);
if ((buf = malloc(bufsize)) == NULL) {
closure->errstr = _("unable to allocate memory");
break;
}
}
nread = fread(buf, msg_len, 1, closure->journal);
if (nread != 1) {
if (feof(closure->journal)) {
sudo_warnx(U_("%s: %s"), closure->journal_path,
U_("unexpected EOF reading journal file"));
closure->errstr = _("unexpected EOF reading journal file");
} else {
sudo_warn(U_("%s: %s"), closure->journal_path,
U_("error reading journal file"));
closure->errstr = _("error reading journal file");
}
break;
}
}
client_message__free_unpacked(msg, NULL);
msg = client_message__unpack(NULL, msg_len, buf);
if (msg == NULL) {
sudo_warnx(U_("unable to unpack %s size %zu"), "ClientMessage",
(size_t)msg_len);
closure->errstr = _("invalid journal file, unable to restart");
break;
}
switch (msg->type_case) {
case CLIENT_MESSAGE__TYPE_HELLO_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past ClientHello (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_ACCEPT_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past AcceptMessage (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_REJECT_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past RejectMessage (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_EXIT_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past ExitMessage (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_RESTART_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past RestartMessage (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_ALERT_MSG:
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"seeking past AlertMessage (%d)", msg->type_case);
break;
case CLIENT_MESSAGE__TYPE_TTYIN_BUF:
delay = msg->u.ttyin_buf->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read IoBuffer (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_TTYOUT_BUF:
delay = msg->u.ttyout_buf->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read IoBuffer (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_STDIN_BUF:
delay = msg->u.stdin_buf->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read IoBuffer (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_STDOUT_BUF:
delay = msg->u.stdout_buf->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read stdout_buf (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_STDERR_BUF:
delay = msg->u.stderr_buf->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read stderr_buf (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_WINSIZE_EVENT:
delay = msg->u.winsize_event->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read ChangeWindowSize (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
case CLIENT_MESSAGE__TYPE_SUSPEND_EVENT:
delay = msg->u.suspend_event->delay;
sudo_debug_printf(SUDO_DEBUG_DEBUG|SUDO_DEBUG_LINENO,
"read CommandSuspend (%d), delay [%lld, %ld]", msg->type_case,
(long long)delay->tv_sec, (long)delay->tv_nsec);
break;
default:
sudo_warnx(U_("unexpected type_case value %d in %s from %s"),
msg->type_case, "ClientMessage", closure->journal_path);
break;
}
if (delay != NULL)
update_elapsed_time(delay, &closure->elapsed_time);
if (sudo_timespeccmp(&closure->elapsed_time, target, >=)) {
if (sudo_timespeccmp(&closure->elapsed_time, target, ==)) {
ret = true;
break;
}
/* Mismatch between resume point and stored log. */
closure->errstr = _("invalid journal file, unable to restart");
sudo_warnx(U_("%s: unable to find resume point [%lld, %ld]"),
closure->journal_path, (long long)target->tv_sec,
target->tv_nsec);
break;
}
}
client_message__free_unpacked(msg, NULL);
free(buf);
debug_return_bool(ret);
}
/*
* Restart an existing journal.
* Seeks to the resume_point in RestartMessage before continuing.
* Returns true if we reached the target time exactly, else false.
*/
static bool
journal_restart(RestartMessage *msg, uint8_t *buf, size_t buflen,
struct connection_closure *closure)
{
struct timespec target;
int fd, len;
char *cp, journal_path[PATH_MAX];
debug_decl(journal_restart, SUDO_DEBUG_UTIL);
/* Strip off leading hostname from log_id. */
if ((cp = strchr(msg->log_id, '/')) != NULL) {
if (cp != msg->log_id)
cp++;
} else {
cp = msg->log_id;
}
len = snprintf(journal_path, sizeof(journal_path), "%s/incoming/%s",
logsrvd_conf_relay_dir(), cp);
if (len >= ssizeof(journal_path)) {
errno = ENAMETOOLONG;
sudo_warn("%s/incoming/%s", logsrvd_conf_relay_dir(), cp);
closure->errstr = _("unable to create journal file");
debug_return_bool(false);
}
if ((fd = open(journal_path, O_RDWR)) == -1) {
sudo_warn(U_("unable to open %s"), journal_path);
closure->errstr = _("unable to create journal file");
debug_return_bool(false);
}
if (!journal_fdopen(fd, journal_path, closure)) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
close(fd);
closure->errstr = _("unable to allocate memory");
debug_return_bool(false);
}
/* Seek forward to resume point. */
target.tv_sec = msg->resume_point->tv_sec;
target.tv_nsec = msg->resume_point->tv_nsec;
if (!journal_seek(&target, closure)) {
sudo_warn(U_("unable to seek to [%lld, %ld] in journal file %s"),
(long long)target.tv_sec, target.tv_nsec, journal_path);
debug_return_bool(false);
}
debug_return_bool(true);
}
static bool
journal_write(uint8_t *buf, size_t len, struct connection_closure *closure)
{
uint32_t msg_len;
debug_decl(journal_write, SUDO_DEBUG_UTIL);
/* 32-bit message length in network byte order. */
msg_len = htonl((uint32_t)len);
if (fwrite(&msg_len, 1, sizeof(msg_len), closure->journal) != sizeof(msg_len)) {
closure->errstr = _("unable to write journal file");
debug_return_bool(false);
}
/* message payload */
if (fwrite(buf, 1, len, closure->journal) != len) {
closure->errstr = _("unable to write journal file");
debug_return_bool(false);
}
debug_return_bool(true);
}
/*
* Store an AcceptMessage from the client in the journal.
*/
static bool
journal_accept(AcceptMessage *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_accept, SUDO_DEBUG_UTIL);
if (closure->journal_path != NULL) {
/* Re-use existing journal file. */
debug_return_bool(journal_write(buf, len, closure));
}
/* Store message in a journal for later relaying. */
if (!journal_create(closure))
debug_return_bool(false);
if (!journal_write(buf, len, closure))
debug_return_bool(false);
if (msg->expect_iobufs) {
/* Send log ID to client for restarting connections. */
if (!fmt_log_id_message(closure->journal_path, closure))
debug_return_bool(false);
if (sudo_ev_add(closure->evbase, closure->write_ev,
logsrvd_conf_server_timeout(), false) == -1) {
sudo_warnx("%s", U_("unable to add event to queue"));
debug_return_bool(false);
}
}
debug_return_bool(true);
}
/*
* Store a RejectMessage from the client in the journal.
*/
static bool
journal_reject(RejectMessage *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_reject, SUDO_DEBUG_UTIL);
/* Store message in a journal for later relaying. */
if (closure->journal_path == NULL) {
if (!journal_create(closure))
debug_return_bool(false);
}
if (!journal_write(buf, len, closure))
debug_return_bool(false);
debug_return_bool(true);
}
/*
* Store an ExitMessage from the client in the journal.
*/
static bool
journal_exit(ExitMessage *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_exit, SUDO_DEBUG_UTIL);
/* Store exit message in journal. */
if (!journal_write(buf, len, closure))
debug_return_bool(false);
if (!journal_finish(closure))
debug_return_bool(false);
debug_return_bool(true);
}
/*
* Store an AlertMessage from the client in the journal.
*/
static bool
journal_alert(AlertMessage *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_alert, SUDO_DEBUG_UTIL);
/* Store message in a journal for later relaying. */
if (closure->journal_path == NULL) {
if (!journal_create(closure))
debug_return_bool(false);
}
if (!journal_write(buf, len, closure))
debug_return_bool(false);
debug_return_bool(true);
}
/*
* Store an IoBuffer from the client in the journal.
*/
static bool
journal_iobuf(int iofd, IoBuffer *iobuf, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_iobuf, SUDO_DEBUG_UTIL);
if (!journal_write(buf, len, closure))
debug_return_bool(false);
update_elapsed_time(iobuf->delay, &closure->elapsed_time);
debug_return_bool(true);
}
/*
* Store a CommandSuspend message from the client in the journal.
*/
static bool
journal_suspend(CommandSuspend *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_suspend, SUDO_DEBUG_UTIL);
update_elapsed_time(msg->delay, &closure->elapsed_time);
debug_return_bool(journal_write(buf, len, closure));
}
/*
* Store a ChangeWindowSize message from the client in the journal.
*/
static bool
journal_winsize(ChangeWindowSize *msg, uint8_t *buf, size_t len,
struct connection_closure *closure)
{
debug_decl(journal_winsize, SUDO_DEBUG_UTIL);
update_elapsed_time(msg->delay, &closure->elapsed_time);
debug_return_bool(journal_write(buf, len, closure));
}
struct client_message_switch cms_journal = {
journal_accept,
journal_reject,
journal_exit,
journal_restart,
journal_alert,
journal_iobuf,
journal_suspend,
journal_winsize
};
|