Skip to content

Commit 3bbc7c8

Browse files
committed
Store submitenv in eventlog and pass it to sudo_logsrvd.
1 parent 726b646 commit 3bbc7c8

10 files changed

Lines changed: 99 additions & 7 deletions

File tree

include/sudo_eventlog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct eventlog {
109109
char *submithost;
110110
char *submituser;
111111
char *submitgroup;
112+
char **submitenv;
112113
char *ttyname;
113114
char **runargv;
114115
char **runenv;

lib/eventlog/eventlog.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,19 @@ eventlog_store_json(struct json_container *jsonc, const struct eventlog *evlog)
759759
goto oom;
760760
}
761761

762+
if (evlog->submitenv != NULL) {
763+
if (!sudo_json_open_array(jsonc, "submitenv"))
764+
goto oom;
765+
for (i = 0; (cp = evlog->submitenv[i]) != NULL; i++) {
766+
json_value.type = JSON_STRING;
767+
json_value.u.string = cp;
768+
if (!sudo_json_add_value(jsonc, NULL, &json_value))
769+
goto oom;
770+
}
771+
if (!sudo_json_close_array(jsonc))
772+
goto oom;
773+
}
774+
762775
debug_return_bool(true);
763776

764777
oom:

lib/eventlog/eventlog_free.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ eventlog_free(struct eventlog *evlog)
5555
free(evlog->peeraddr);
5656
free(evlog->signal_name);
5757
free(evlog->source);
58+
if (evlog->submitenv != NULL) {
59+
for (i = 0; evlog->submitenv[i] != NULL; i++)
60+
free(evlog->submitenv[i]);
61+
free(evlog->submitenv);
62+
}
5863
free(evlog->submithost);
5964
free(evlog->submituser);
6065
free(evlog->submitgroup);

lib/eventlog/parse_json.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ json_array_to_strvec(struct eventlog_json_object *array)
195195
debug_return_ptr(ret);
196196
}
197197

198+
static bool
199+
json_store_submitenv(struct json_item *item, struct eventlog *evlog)
200+
{
201+
size_t i;
202+
debug_decl(json_store_submitenv, SUDO_DEBUG_UTIL);
203+
204+
if (evlog->submitenv != NULL) {
205+
for (i = 0; evlog->submitenv[i] != NULL; i++)
206+
free(evlog->submitenv[i]);
207+
free(evlog->submitenv);
208+
}
209+
evlog->submitenv = json_array_to_strvec(&item->u.child);
210+
211+
debug_return_bool(evlog->submitenv != NULL);
212+
}
213+
198214
static bool
199215
json_store_runargv(struct json_item *item, struct eventlog *evlog)
200216
{
@@ -464,6 +480,7 @@ static struct evlog_json_key {
464480
{ "source", JSON_STRING, json_store_source },
465481
{ "signal", JSON_STRING, json_store_signal },
466482
{ "submitcwd", JSON_STRING, json_store_submitcwd },
483+
{ "submitenv", JSON_ARRAY, json_store_submitenv },
467484
{ "submithost", JSON_STRING, json_store_submithost },
468485
{ "submitgroup", JSON_STRING, json_store_submitgroup },
469486
{ "submituser", JSON_STRING, json_store_submituser },

logsrvd/iolog_writer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ evlog_new(TimeSpec *submit_time, InfoMessage **info_msgs, size_t infolen,
303303
}
304304
continue;
305305
}
306+
if (strcmp(key, "submitenv") == 0) {
307+
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRLISTVAL)) {
308+
evlog->submitenv = strlist_copy(info->u.strlistval);
309+
if (evlog->submitenv == NULL)
310+
goto bad;
311+
}
312+
continue;
313+
}
306314
if (strcmp(key, "submitgroup") == 0) {
307315
if (type_matches(info, source, INFO_MESSAGE__VALUE_STRVAL)) {
308316
if ((evlog->submitgroup = strdup(info->u.strval)) == NULL) {

logsrvd/sendlog.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,22 +539,39 @@ fmt_runenv(const struct eventlog *evlog)
539539
debug_return_ptr(vec_to_stringlist(evlog->runenv));
540540
}
541541

542+
/*
543+
* Build submitenv StringList from env in evlog, if present.
544+
*/
545+
static InfoMessage__StringList *
546+
fmt_submitenv(const struct eventlog *evlog)
547+
{
548+
debug_decl(fmt_submitenv, SUDO_DEBUG_UTIL);
549+
550+
/* Only present in log.json. */
551+
if (evlog->submitenv == NULL || evlog->submitenv[0] == NULL)
552+
debug_return_ptr(NULL);
553+
554+
debug_return_ptr(vec_to_stringlist(evlog->submitenv));
555+
}
556+
542557
static InfoMessage **
543558
fmt_info_messages(const struct eventlog *evlog, char *hostname,
544559
size_t *n_info_msgs)
545560
{
546561
InfoMessage **info_msgs = NULL;
547562
InfoMessage__StringList *runargv = NULL;
548563
InfoMessage__StringList *runenv = NULL;
564+
InfoMessage__StringList *submitenv = NULL;
549565
size_t info_msgs_size, n = 0;
550566
debug_decl(fmt_info_messages, SUDO_DEBUG_UTIL);
551567

552568
runargv = fmt_runargv(evlog);
553569
if (runargv == NULL)
554570
goto oom;
555571

556-
/* runenv is only present in log.json */
572+
/* runenv and submitenv are only present in log.json */
557573
runenv = fmt_runenv(evlog);
574+
submitenv = fmt_submitenv(evlog);
558575

559576
/* The sudo I/O log info file has limited info. */
560577
info_msgs_size = 14;
@@ -596,6 +613,10 @@ fmt_info_messages(const struct eventlog *evlog, char *hostname,
596613
fill_num("lines", evlog->lines);
597614
fill_strlist("runargv", runargv);
598615
runargv = NULL;
616+
if (submitenv != NULL) {
617+
fill_strlist("submitenv", submitenv);
618+
submitenv = NULL;
619+
}
599620
if (runenv != NULL) {
600621
fill_strlist("runenv", runenv);
601622
runenv = NULL;
@@ -637,6 +658,10 @@ fmt_info_messages(const struct eventlog *evlog, char *hostname,
637658
free(runenv->strings);
638659
free(runenv);
639660
}
661+
if (submitenv != NULL) {
662+
free(submitenv->strings);
663+
free(submitenv);
664+
}
640665
*n_info_msgs = 0;
641666
debug_return_ptr(NULL);
642667
}

plugins/sudoers/iolog.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ free_iolog_details(void)
201201
iolog_details.evlog->runargv = NULL;
202202
free(iolog_details.evlog->runenv);
203203
iolog_details.evlog->runenv = NULL;
204+
free(iolog_details.evlog->submitenv);
205+
iolog_details.evlog->submitenv = NULL;
204206
eventlog_free(iolog_details.evlog);
205207
}
206208
str_list_free(iolog_details.log_servers);
@@ -292,6 +294,7 @@ static int
292294
iolog_deserialize_info(struct log_details *details, char * const user_info[],
293295
char * const command_info[], char * const argv[], char * const user_env[])
294296
{
297+
const struct sudoers_context *ctx = sudoers_get_context();
295298
struct eventlog *evlog;
296299
const char *runas_uid_str = "0", *runas_euid_str = NULL;
297300
const char *runas_gid_str = "0", *runas_egid_str = NULL;
@@ -610,6 +613,11 @@ iolog_deserialize_info(struct log_details *details, char * const user_info[],
610613
if (evlog->runenv == NULL)
611614
goto oom;
612615
}
616+
if (ctx->user.envp != NULL) {
617+
evlog->submitenv = copy_vector_shallow(ctx->user.envp);
618+
if (evlog->submitenv == NULL)
619+
goto oom;
620+
}
613621

614622
/*
615623
* Lookup runas user and group, preferring effective over real uid/gid.

plugins/sudoers/log_client.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,11 +818,21 @@ fmt_info_messages(struct client_closure *closure, struct eventlog *evlog,
818818
{
819819
InfoMessage__StringList *runargv = NULL;
820820
InfoMessage__StringList *runenv = NULL;
821+
InfoMessage__StringList *submitenv = NULL;
821822
InfoMessage **info_msgs = NULL;
822823
size_t info_msgs_size, n = 0;
823824
debug_decl(fmt_info_messages, SUDOERS_DEBUG_UTIL);
824825

825826
/* Convert NULL-terminated vectors to StringList. */
827+
if (evlog->submitenv != NULL) {
828+
if ((submitenv = malloc(sizeof(*submitenv))) == NULL)
829+
goto bad;
830+
info_message__string_list__init(submitenv);
831+
submitenv->strings = evlog->submitenv;
832+
while (submitenv->strings[submitenv->n_strings] != NULL)
833+
submitenv->n_strings++;
834+
}
835+
826836
if (evlog->runargv != NULL) {
827837
if ((runargv = malloc(sizeof(*runargv))) == NULL)
828838
goto bad;
@@ -912,7 +922,10 @@ fmt_info_messages(struct client_closure *closure, struct eventlog *evlog,
912922
if (evlog->cwd != NULL) {
913923
fill_str("submitcwd", evlog->cwd);
914924
}
915-
/* TODO - submitenv */
925+
if (submitenv != NULL) {
926+
fill_strlist("submitenv", submitenv);
927+
submitenv = NULL;
928+
}
916929
/* TODO - submitgid */
917930
/* TODO - submitgids */
918931
/* TODO - submitgroup */
@@ -935,6 +948,7 @@ fmt_info_messages(struct client_closure *closure, struct eventlog *evlog,
935948
free_info_messages(info_msgs, n);
936949
free(runargv);
937950
free(runenv);
951+
free(submitenv);
938952

939953
*n_info_msgs = 0;
940954
debug_return_ptr(NULL);

plugins/sudoers/logging.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ should_mail(const struct sudoers_context *ctx, unsigned int status)
969969
*/
970970
void
971971
sudoers_to_eventlog(const struct sudoers_context *ctx, struct eventlog *evlog,
972-
const char *cmnd, char * const argv[], char * const envp[],
972+
const char *cmnd, char * const runargv[], char * const runenv[],
973973
const char *uuid_str)
974974
{
975975
struct group *grp;
@@ -982,7 +982,7 @@ sudoers_to_eventlog(const struct sudoers_context *ctx, struct eventlog *evlog,
982982
memset(evlog, 0, sizeof(*evlog));
983983
evlog->iolog_file = ctx->iolog_file;
984984
evlog->iolog_path = ctx->iolog_path;
985-
evlog->command = cmnd ? (char *)cmnd : (argv ? argv[0] : NULL);
985+
evlog->command = cmnd ? (char *)cmnd : (runargv ? runargv[0] : NULL);
986986
evlog->cwd = ctx->user.cwd;
987987
if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
988988
evlog->runchroot = def_runchroot;
@@ -1001,9 +1001,10 @@ sudoers_to_eventlog(const struct sudoers_context *ctx, struct eventlog *evlog,
10011001
if (grp != NULL)
10021002
evlog->submitgroup = grp->gr_name;
10031003
evlog->ttyname = ctx->user.ttypath;
1004-
evlog->runargv = (char **)argv;
1004+
evlog->runargv = (char **)runargv;
10051005
evlog->env_add = (char **)ctx->user.env_add;
1006-
evlog->runenv = (char **)envp;
1006+
evlog->runenv = (char **)runenv;
1007+
evlog->submitenv = (char **)ctx->user.envp;
10071008
evlog->submit_time = ctx->submit_time;
10081009
evlog->lines = ctx->user.lines;
10091010
evlog->columns = ctx->user.cols;

plugins/sudoers/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ bool log_warningx(const struct sudoers_context *ctx, unsigned int flags, const c
8585
bool gai_log_warning(const struct sudoers_context *ctx, unsigned int flags, int errnum, const char * restrict fmt, ...) sudo_printflike(4, 5);
8686
bool sudoers_initlocale(const char *ulocale, const char *slocale);
8787
bool sudoers_locale_callback(struct sudoers_context *ctx, const char *file, int line, int column, const union sudo_defs_val *sd_un, int op);
88-
void sudoers_to_eventlog(const struct sudoers_context *ctx, struct eventlog *evlog, const char *cmnd, char * const argv[], char *const envp[], const char *uuid_str);
88+
void sudoers_to_eventlog(const struct sudoers_context *ctx, struct eventlog *evlog, const char *cmnd, char * const runargv[], char *const runenv[], const char *uuid_str);
8989
void init_eventlog_config(void);
9090
bool init_log_details(struct log_details *details, struct eventlog *evlog);
9191
bool log_parse_error(const struct sudoers_context *ctx, const char *file, int line, int column, const char * restrict fmt, va_list ap) sudo_printf0like(5, 0);

0 commit comments

Comments
 (0)