Skip to content

Commit f0d2ff6

Browse files
committed
sudo_logsrvd: Make random drop work when relaying too.
1 parent cdc42d8 commit f0d2ff6

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

logsrvd/logsrvd.c

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ static void server_commit_cb(int fd, int what, void *v);
9292
static void tls_handshake_cb(int fd, int what, void *v);
9393
#endif
9494

95+
static double random_drop;
96+
9597
/*
9698
* Free a struct connection_closure container and its contents.
9799
*/
@@ -717,6 +719,17 @@ handle_iobuf(int iofd, const IoBuffer *iobuf, const uint8_t *buf, size_t len,
717719
sudo_debug_printf(SUDO_DEBUG_INFO, "%s: received IoBuffer from %s",
718720
source, __func__);
719721

722+
/* Random drop is a debugging tool to test client restart. */
723+
if (random_drop > 0.0) {
724+
double randval = arc4random() / (double)UINT32_MAX;
725+
if (randval < random_drop) {
726+
closure->errstr = _("randomly dropping connection");
727+
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
728+
"randomly dropping connection (%f < %f)", randval, random_drop);
729+
debug_return_bool(false);
730+
}
731+
}
732+
720733
if (!closure->cms->iobuf(iofd, iobuf, buf, len, closure))
721734
debug_return_bool(false);
722735
if (!enable_commit(closure))
@@ -1186,8 +1199,11 @@ client_msg_cb(int fd, int what, void *v)
11861199
"%s: parsing ClientMessage, size %u", __func__, msg_len);
11871200
buf->off += sizeof(msg_len);
11881201
if (!handle_client_message(buf->data + buf->off, msg_len, closure)) {
1189-
sudo_warnx(U_("%s: %s"), source, U_("invalid ClientMessage"));
1190-
closure->errstr = _("invalid ClientMessage");
1202+
/* Use specific error string if one is set. */
1203+
if (closure->errstr == NULL) {
1204+
sudo_warnx(U_("%s: %s"), source, U_("invalid ClientMessage"));
1205+
closure->errstr = _("invalid ClientMessage");
1206+
}
11911207
goto send_error;
11921208
}
11931209
buf->off += msg_len;
@@ -2054,6 +2070,7 @@ main(int argc, char *argv[])
20542070
{
20552071
struct sudo_event_base *evbase;
20562072
bool nofork = false;
2073+
char *ep;
20572074
int ch;
20582075
debug_decl_vars(main, SUDO_DEBUG_MAIN);
20592076

@@ -2097,8 +2114,11 @@ main(int argc, char *argv[])
20972114
break;
20982115
case 'R':
20992116
/* random connection drop probability as a percentage (debug) */
2100-
if (!set_random_drop(optarg))
2101-
sudo_fatalx(U_("invalid random drop value: %s"), optarg);
2117+
errno = 0;
2118+
random_drop = strtod(optarg, &ep);
2119+
if (*ep != '\0' || errno != 0)
2120+
sudo_fatalx(U_("invalid random drop value: %s"), optarg);
2121+
random_drop /= 100.0; /* convert from percentage */
21022122
break;
21032123
case 'V':
21042124
(void)printf(_("%s version %s\n"), getprogname(),

logsrvd/logsrvd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ extern struct client_message_switch cms_journal;
242242

243243
/* logsrvd_local.c */
244244
extern struct client_message_switch cms_local;
245-
bool set_random_drop(const char *dropstr);
246245
bool store_accept_local(const AcceptMessage *msg, const uint8_t *buf, size_t len, struct connection_closure *closure);
247246
bool store_reject_local(const RejectMessage *msg, const uint8_t *buf, size_t len, struct connection_closure *closure);
248247
bool store_exit_local(const ExitMessage *msg, const uint8_t *buf, size_t len, struct connection_closure *closure);

logsrvd/logsrvd_local.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,6 @@ struct logsrvd_info_closure {
6464
size_t infolen;
6565
};
6666

67-
static double random_drop;
68-
69-
bool
70-
set_random_drop(const char *dropstr)
71-
{
72-
char *ep;
73-
debug_decl(set_random_drop, SUDO_DEBUG_UTIL);
74-
75-
errno = 0;
76-
random_drop = strtod(dropstr, &ep);
77-
if (*ep != '\0' || errno != 0)
78-
debug_return_bool(false);
79-
random_drop /= 100.0; /* convert from percentage */
80-
81-
debug_return_bool(true);
82-
}
83-
8467
static bool
8568
logsrvd_json_log_cb(struct json_container *jsonc, void *v)
8669
{
@@ -703,17 +686,6 @@ store_iobuf_local(int iofd, const IoBuffer *iobuf, const uint8_t *buf,
703686

704687
update_elapsed_time(iobuf->delay, &closure->elapsed_time);
705688

706-
/* Random drop is a debugging tool to test client restart. */
707-
if (random_drop > 0.0) {
708-
double randval = arc4random() / (double)UINT32_MAX;
709-
if (randval < random_drop) {
710-
closure->errstr = _("randomly dropping connection");
711-
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
712-
"randomly dropping connection (%f < %f)", randval, random_drop);
713-
goto bad;
714-
}
715-
}
716-
717689
free(newbuf);
718690
debug_return_bool(true);
719691
bad:

0 commit comments

Comments
 (0)