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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 1994-1996, 1998-2024 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.
*
* Sponsored in part by the Defense Advanced Research Projects
* Agency (DARPA) and Air Force Research Laboratory, Air Force
* Materiel Command, USAF, under agreement number F39502-99-1-0512.
*/
/*
* 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
*/
#ifdef __TANDEM
# include <floss.h>
#endif
#include <config.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_NL_LANGINFO
# include <langinfo.h>
#endif /* HAVE_NL_LANGINFO */
#include <netdb.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <syslog.h>
#ifndef HAVE_GETADDRINFO
# include <compat/getaddrinfo.h>
#endif
#include <sudoers.h>
#ifdef SUDOERS_LOG_CLIENT
# include <log_client.h>
# include <strlist.h>
#endif
struct parse_error {
STAILQ_ENTRY(parse_error) entries;
char *errstr;
};
STAILQ_HEAD(parse_error_list, parse_error);
static struct parse_error_list parse_error_list =
STAILQ_HEAD_INITIALIZER(parse_error_list);
static bool should_mail(const struct sudoers_context *ctx, unsigned int);
static bool warned = false;
#ifdef SUDOERS_LOG_CLIENT
/*
* Convert a defaults-style list to a stringlist.
*/
static struct sudoers_str_list *
list_to_strlist(struct list_members *list)
{
struct sudoers_str_list *strlist;
struct sudoers_string *str;
struct list_member *item;
debug_decl(slist_to_strlist, SUDOERS_DEBUG_LOGGING);
if ((strlist = str_list_alloc()) == NULL)
goto oom;
SLIST_FOREACH(item, list, entries) {
if ((str = sudoers_string_alloc(item->value)) == NULL)
goto oom;
/* List is in reverse order, insert at head to fix that. */
STAILQ_INSERT_HEAD(strlist, str, entries);
}
debug_return_ptr(strlist);
oom:
str_list_free(strlist);
debug_return_ptr(NULL);
}
bool
init_log_details(struct log_details *details, struct eventlog *evlog)
{
struct sudoers_str_list *log_servers = NULL;
debug_decl(init_log_details, SUDOERS_DEBUG_LOGGING);
memset(details, 0, sizeof(*details));
if ((log_servers = list_to_strlist(&def_log_servers)) == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_bool(false);
}
details->evlog = evlog;
details->ignore_log_errors = def_ignore_logfile_errors;
details->log_servers = log_servers;
details->server_timeout.tv_sec = def_log_server_timeout;
details->keepalive = def_log_server_keepalive;
#if defined(HAVE_OPENSSL)
details->ca_bundle = def_log_server_cabundle;
details->cert_file = def_log_server_peer_cert;
details->key_file = def_log_server_peer_key;
details->verify_server = def_log_server_verify;
#endif /* HAVE_OPENSSL */
debug_return_bool(true);
}
bool
log_server_reject(const struct sudoers_context *ctx, struct eventlog *evlog,
const char *message)
{
bool ret = false;
debug_decl(log_server_reject, SUDOERS_DEBUG_LOGGING);
if (SLIST_EMPTY(&def_log_servers))
debug_return_bool(true);
if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
/* Older servers don't support multiple commands per session. */
if (!client_closure->subcommands)
debug_return_bool(true);
/* Use existing client closure. */
if (fmt_reject_message(client_closure, evlog)) {
if (client_closure->write_ev->add(client_closure->write_ev,
&client_closure->log_details->server_timeout) == -1) {
sudo_warn("%s", U_("unable to add event to queue"));
goto done;
}
ret = true;
}
} else {
struct log_details details;
if (!init_log_details(&details, evlog))
debug_return_bool(false);
/* Open connection to log server, send hello and reject messages. */
client_closure = log_server_open(&details, NULL, false,
SEND_REJECT, message);
if (client_closure != NULL) {
client_closure_free(client_closure);
client_closure = NULL;
ret = true;
}
/* Only the log_servers string list is dynamically allocated. */
str_list_free(details.log_servers);
}
done:
debug_return_bool(ret);
}
bool
log_server_alert(const struct sudoers_context *ctx, struct eventlog *evlog,
const char *message, const char *errstr)
{
struct log_details details;
char *emessage = NULL;
bool ret = false;
debug_decl(log_server_alert, SUDOERS_DEBUG_LOGGING);
if (SLIST_EMPTY(&def_log_servers))
debug_return_bool(true);
if (errstr != NULL) {
if (asprintf(&emessage, _("%s: %s"), message, errstr) == -1) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
}
if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
/* Older servers don't support multiple commands per session. */
if (!client_closure->subcommands) {
ret = true;
goto done;
}
/* Use existing client closure. */
if (fmt_alert_message(client_closure, evlog)) {
if (client_closure->write_ev->add(client_closure->write_ev,
&client_closure->log_details->server_timeout) == -1) {
sudo_warn("%s", U_("unable to add event to queue"));
goto done;
}
ret = true;
}
} else {
if (!init_log_details(&details, evlog))
goto done;
/* Open connection to log server, send hello and alert messages. */
client_closure = log_server_open(&details, NULL, false,
SEND_ALERT, emessage ? emessage : message);
if (client_closure != NULL) {
client_closure_free(client_closure);
client_closure = NULL;
ret = true;
}
/* Only the log_servers string list is dynamically allocated. */
str_list_free(details.log_servers);
}
done:
free(emessage);
debug_return_bool(ret);
}
#else
bool
log_server_reject(const struct sudoers_context *ctx, struct eventlog *evlog,
const char *message)
{
return true;
}
bool
log_server_alert(const struct sudoers_context *ctx, struct eventlog *evlog,
const char *message, const char *errstr)
{
return true;
}
#endif /* SUDOERS_LOG_CLIENT */
/*
* Log a reject event to syslog, a log file, sudo_logsrvd and/or email.
*/
static bool
log_reject(const struct sudoers_context *ctx, const char *message,
bool logit, bool mailit)
{
const char *uuid_str = NULL;
struct eventlog evlog;
int evl_flags = 0;
bool ret;
debug_decl(log_reject, SUDOERS_DEBUG_LOGGING);
if (!ISSET(ctx->mode, MODE_POLICY_INTERCEPTED))
uuid_str = ctx->uuid_str;
if (mailit) {
SET(evl_flags, EVLOG_MAIL);
if (!logit)
SET(evl_flags, EVLOG_MAIL_ONLY);
}
sudoers_to_eventlog(ctx, &evlog, ctx->runas.cmnd, ctx->runas.argv,
NULL, uuid_str);
ret = eventlog_reject(&evlog, evl_flags, message, NULL, NULL);
if (!log_server_reject(ctx, &evlog, message))
ret = false;
debug_return_bool(ret);
}
/*
* Log, audit and mail the denial message, optionally informing the user.
*/
bool
log_denial(const struct sudoers_context *ctx, unsigned int status,
bool inform_user)
{
const char *message;
int oldlocale;
bool mailit, ret = true;
debug_decl(log_denial, SUDOERS_DEBUG_LOGGING);
/* Send mail based on status. */
mailit = should_mail(ctx, status);
/* Set error message. */
if (ISSET(status, FLAG_NO_USER))
message = N_("user NOT in sudoers");
else if (ISSET(status, FLAG_NO_HOST))
message = N_("user NOT authorized on host");
else if (ISSET(status, FLAG_INTERCEPT_SETID))
message = N_("setid command rejected in intercept mode");
else
message = N_("command not allowed");
/* Do auditing first (audit_failure() handles the locale itself). */
audit_failure(ctx, ctx->runas.argv, "%s", message);
if (def_log_denied || mailit) {
/* Log and mail messages should be in the sudoers locale. */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
if (!log_reject(ctx, message, def_log_denied, mailit))
ret = false;
/* Restore locale. */
sudoers_setlocale(oldlocale, NULL);
}
/* Inform the user of the failure (in their locale). */
if (inform_user) {
sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
if (ISSET(status, FLAG_NO_USER)) {
sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not in the sudoers "
"file.\n"), ctx->user.name);
} else if (ISSET(status, FLAG_NO_HOST)) {
sudo_printf(SUDO_CONV_ERROR_MSG, _("%s is not allowed to run sudo "
"on %s.\n"), ctx->user.name, ctx->runas.shost);
} else if (ISSET(status, FLAG_INTERCEPT_SETID)) {
sudo_printf(SUDO_CONV_ERROR_MSG, _("%s: %s\n"), getprogname(),
_("setid commands are not permitted in intercept mode"));
} else if (ISSET(status, FLAG_NO_CHECK)) {
sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s may not run "
"sudo on %s.\n"), ctx->user.name, ctx->runas.shost);
} else {
const struct passwd *runas_pw =
ctx->runas.list_pw ? ctx->runas.list_pw : ctx->runas.pw;
const char *cmnd1 = ctx->user.cmnd;
const char *cmnd2 = "";
if (ISSET(ctx->mode, MODE_CHECK)) {
/* For "sudo -l command" the command run is in runas.argv[1]. */
cmnd1 = "list ";
cmnd2 = ctx->runas.argv[1];
}
sudo_printf(SUDO_CONV_ERROR_MSG, _("Sorry, user %s is not allowed "
"to execute '%s%s%s%s' as %s%s%s on %s.\n"),
ctx->user.name, cmnd1, cmnd2, ctx->user.cmnd_args ? " " : "",
ctx->user.cmnd_args ? ctx->user.cmnd_args : "",
runas_pw ? runas_pw->pw_name : ctx->user.name,
ctx->runas.gr ? ":" : "",
ctx->runas.gr ? ctx->runas.gr->gr_name : "",
ctx->user.host);
if (def_cmddenial_message != NULL) {
sudo_printf(SUDO_CONV_ERROR_MSG, "%s\n", def_cmddenial_message);
}
}
if (mailit) {
sudo_printf(SUDO_CONV_ERROR_MSG, "%s",
_("This incident has been reported to the administrator.\n"));
}
sudoers_setlocale(oldlocale, NULL);
}
debug_return_bool(ret);
}
/*
* Log and audit that user was not allowed to run the command.
*/
bool
log_failure(const struct sudoers_context *ctx, unsigned int status,
int cmnd_status)
{
bool ret, warn_not_found = false;
debug_decl(log_failure, SUDOERS_DEBUG_LOGGING);
/*
* If the command was not found but the user has a sudoers entry
* for this host, we want to give them a better warning than just
* "Sorry, user foo may not run sudo on bar." Otherwise, a mistyped
* sudo command can make it appear that the user has no sudo
* privileges on the host, even when they do.
*
* This behavior is controlled by the "path_info" setting. We
* only do this when running an actual command since FLAG_NO_USER
* and FLAG_NO_HOST are not set for pseudo-commands like "list".
*/
if (!ISSET(status, FLAG_NO_USER | FLAG_NO_HOST) &&
ISSET(ctx->mode, MODE_RUN) && def_path_info &&
(cmnd_status == NOT_FOUND_DOT || cmnd_status == NOT_FOUND))
warn_not_found = true;
ret = log_denial(ctx, status, !warn_not_found);
if (warn_not_found) {
const char *cmnd = ctx->user.cmnd;
/* This text must be kept in sync with sudoers_check_common() */
if (cmnd_status == NOT_FOUND)
sudo_warnx(U_("%s: command not found"), cmnd);
else if (cmnd_status == NOT_FOUND_DOT)
sudo_warnx(U_("ignoring \"%s\" found in '.'\nUse \"sudo ./%s\" if this is the \"%s\" you wish to run."), cmnd, cmnd, cmnd);
}
debug_return_bool(ret);
}
/*
* Format an authentication failure message, using either
* authfail_message from sudoers or a locale-specific message.
*/
static char *
fmt_authfail_message(unsigned int tries)
{
char numbuf[STRLEN_MAX_UNSIGNED(unsigned int) + 1];
char *dst, *dst_end, *ret = NULL;
const char *src;
size_t len;
debug_decl(fmt_authfail_message, SUDOERS_DEBUG_LOGGING);
if (def_authfail_message == NULL) {
if (asprintf(&ret, ngettext("%u incorrect password attempt",
"%u incorrect password attempts", tries), tries) == -1)
goto oom;
debug_return_ptr(ret);
}
len = (size_t)snprintf(numbuf, sizeof(numbuf), "%u", tries);
if (len >= sizeof(numbuf))
goto overflow;
src = def_authfail_message;
len = strlen(src) + 1;
while (*src != '\0') {
if (src[0] == '%') {
switch (src[1]) {
case '%':
len--;
src++;
break;
case 'd':
len -= 2;
len += strlen(numbuf);
src++;
break;
default:
/* pass through as-is */
break;
}
}
src++;
}
if ((ret = malloc(len)) == NULL)
goto oom;
dst = ret;
dst_end = ret + len;
src = def_authfail_message;
while (*src != '\0') {
/* Always leave space for the terminating NUL. */
if (dst + 1 >= dst_end)
goto overflow;
if (src[0] == '%') {
switch (src[1]) {
case '%':
src++;
break;
case 'd':
len = strlcpy(dst, numbuf, (size_t)(dst_end - dst));
if (len >= (size_t)(dst_end - dst))
goto overflow;
dst += len;
src += 2;
continue;
default:
/* pass through as-is */
break;
}
}
*dst++ = *src++;
}
*dst = '\0';
debug_return_ptr(ret);
oom:
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
debug_return_ptr(NULL);
overflow:
sudo_warnx(U_("internal error, %s overflow"), __func__);
free(ret);
errno = ERANGE;
debug_return_ptr(NULL);
}
/*
* Log and audit that user was not able to authenticate themselves.
*/
bool
log_auth_failure(const struct sudoers_context *ctx, unsigned int status,
unsigned int tries)
{
char *message = NULL;
int oldlocale;
bool ret = true;
bool mailit = false;
bool logit = true;
debug_decl(log_auth_failure, SUDOERS_DEBUG_LOGGING);
/* Do auditing first (audit_failure() handles the locale itself). */
audit_failure(ctx, ctx->runas.argv, "%s", N_("authentication failure"));
/*
* Do we need to send mail?
* We want to avoid sending multiple messages for the same command
* so if we are going to send an email about the denial, that takes
* precedence.
*/
if (ISSET(status, VALIDATE_SUCCESS)) {
/* Command allowed, auth failed; do we need to send mail? */
if (def_mail_badpass || def_mail_always)
mailit = true;
if (!def_log_denied)
logit = false;
} else {
/* Command denied, auth failed; make sure we don't send mail twice. */
if (def_mail_badpass && !should_mail(ctx, status))
mailit = true;
/* Don't log the bad password message, we'll log a denial instead. */
logit = false;
}
/* Special case overrides for logging and mailing. */
if (ISSET(status, FLAG_NO_USER_INPUT)) {
/* For "sudo -n", only log the entry if an actual command was run. */
if (ISSET(ctx->mode, MODE_LIST|MODE_VALIDATE)) {
logit = false;
mailit = false;
}
} else if (!ISSET(status, FLAG_BAD_PASSWORD)) {
/* Authenticated OK, sudoers denials are logged separately. */
logit = false;
}
if (logit || mailit) {
/* Log and mail messages should be in the sudoers locale. */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
if (ISSET(status, FLAG_BAD_PASSWORD)) {
message = fmt_authfail_message(tries);
if (message == NULL) {
ret = false;
} else {
ret = log_reject(ctx, message, logit, mailit);
free(message);
}
} else {
ret = log_reject(ctx, _("a password is required"), logit, mailit);
}
/* Restore locale. */
sudoers_setlocale(oldlocale, NULL);
}
/* Inform the user if they failed to authenticate (in their locale). */
sudoers_setlocale(SUDOERS_LOCALE_USER, &oldlocale);
if (ISSET(status, FLAG_BAD_PASSWORD)) {
message = fmt_authfail_message(tries);
if (message == NULL) {
ret = false;
} else {
sudo_warnx("%s", message);
free(message);
}
} else {
sudo_warnx("%s", _("a password is required"));
}
sudoers_setlocale(oldlocale, NULL);
debug_return_bool(ret);
}
/*
* Log and potentially mail the allowed command.
*/
bool
log_allowed(const struct sudoers_context *ctx, struct eventlog *evlog)
{
int oldlocale;
int evl_flags = 0;
bool mailit, ret = true;
debug_decl(log_allowed, SUDOERS_DEBUG_LOGGING);
/* Send mail based on status. */
mailit = should_mail(ctx, VALIDATE_SUCCESS);
if (def_log_allowed || mailit) {
/* Log and mail messages should be in the sudoers locale. */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
if (mailit) {
SET(evl_flags, EVLOG_MAIL);
if (!def_log_allowed)
SET(evl_flags, EVLOG_MAIL_ONLY);
}
if (!eventlog_accept(evlog, evl_flags, NULL, NULL))
ret = false;
sudoers_setlocale(oldlocale, NULL);
}
debug_return_bool(ret);
}
bool
log_exit_status(const struct sudoers_context *ctx, int status)
{
struct eventlog evlog;
int evl_flags = 0;
int exit_value = 0;
int oldlocale;
struct timespec run_time;
char sigbuf[SIG2STR_MAX];
char *signal_name = NULL;
bool dumped_core = false;
bool ret = true;
debug_decl(log_exit_status, SUDOERS_DEBUG_LOGGING);
if (def_log_exit_status || def_mail_always) {
if (sudo_gettime_awake(&run_time) == -1) {
sudo_warn("%s", U_("unable to get time of day"));
ret = false;
goto done;
}
sudo_timespecsub(&run_time, &ctx->start_time, &run_time);
if (WIFEXITED(status)) {
exit_value = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
int signo = WTERMSIG(status);
if (signo <= 0 || sig2str(signo, sigbuf) == -1)
(void)snprintf(sigbuf, sizeof(sigbuf), "%d", signo);
signal_name = sigbuf;
exit_value = signo | 128;
dumped_core = WCOREDUMP(status);
} else {
sudo_warnx("invalid exit status 0x%x", status);
ret = false;
goto done;
}
/* Log and mail messages should be in the sudoers locale. */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
sudoers_to_eventlog(ctx, &evlog, ctx->runas.cmnd_saved,
ctx->runas.argv_saved, NULL, ctx->uuid_str);
if (def_mail_always) {
SET(evl_flags, EVLOG_MAIL);
if (!def_log_exit_status)
SET(evl_flags, EVLOG_MAIL_ONLY);
}
/*
* eventlog_exit() expects event_time to be the command start time,
* not the current time as set by sudoers_to_eventlog().
*/
sudo_timespecsub(&evlog.event_time, &run_time, &evlog.event_time);
evlog.run_time = run_time;
evlog.exit_value = exit_value;
evlog.signal_name = signal_name;
evlog.dumped_core = dumped_core;
if (!eventlog_exit(&evlog, evl_flags))
ret = false;
sudoers_setlocale(oldlocale, NULL);
}
done:
debug_return_bool(ret);
}
/*
* Add message to the parse error journal, which takes ownership of it.
* The message will be freed once the journal is processed.
* Returns true if message was journaled (and consumed), else false.
*/
static bool
journal_parse_error(char *message)
{
struct parse_error *pe;
debug_decl(journal_parse_error, SUDOERS_DEBUG_LOGGING);
pe = malloc(sizeof(*pe));
if (pe == NULL)
debug_return_bool(false);
pe->errstr = message;
STAILQ_INSERT_TAIL(&parse_error_list, pe, entries);
debug_return_bool(true);
}
/*
* Perform logging for log_warning()/log_warningx().
*/
static bool
vlog_warning(const struct sudoers_context *ctx, unsigned int flags,
int errnum, const char * restrict fmt, va_list ap)
{
struct eventlog evlog;
const char *errstr = NULL;
char *message;
bool ret = true;
int len, oldlocale;
int evl_flags = 0;
va_list ap2;
debug_decl(vlog_warning, SUDOERS_DEBUG_LOGGING);
/* Do auditing first (audit_failure() handles the locale itself). */
if (ISSET(flags, SLOG_AUDIT)) {
va_copy(ap2, ap);
vaudit_failure(ctx, ctx->runas.argv, fmt, ap2);
va_end(ap2);
}
/* Need extra copy of ap for sudo_vwarn()/sudo_vwarnx() below. */
va_copy(ap2, ap);
/* Log messages should be in the sudoers locale. */
sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale);
/* Expand printf-style format + args. */
len = vasprintf(&message, _(fmt), ap);
if (len == -1) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
ret = false;
goto done;
}
if (ISSET(flags, SLOG_USE_ERRNO))
errstr = strerror(errnum);
else if (ISSET(flags, SLOG_GAI_ERRNO))
errstr = gai_strerror(errnum);
/* Log to debug file. */
if (errstr != NULL) {
sudo_debug_printf2(NULL, NULL, 0,
SUDO_DEBUG_WARN|sudo_debug_subsys, "%s: %s", message, errstr);
} else {
sudo_debug_printf2(NULL, NULL, 0,
SUDO_DEBUG_WARN|sudo_debug_subsys, "%s", message);
}
if (ISSET(flags, SLOG_SEND_MAIL) || !ISSET(flags, SLOG_NO_LOG)) {
if (ISSET(flags, SLOG_RAW_MSG))
SET(evl_flags, EVLOG_RAW);
if (ISSET(flags, SLOG_SEND_MAIL)) {
SET(evl_flags, EVLOG_MAIL);
if (ISSET(flags, SLOG_NO_LOG))
SET(evl_flags, EVLOG_MAIL_ONLY);
}
sudoers_to_eventlog(ctx, &evlog, ctx->runas.cmnd, ctx->runas.argv,
NULL, ctx->uuid_str);
if (!eventlog_alert(&evlog, evl_flags, &evlog.event_time, message, errstr))
ret = false;
if (!log_server_alert(ctx, &evlog, message, errstr))
ret = false;
}
if (ISSET(flags, SLOG_PARSE_ERROR)) {
char *copy;
/* Journal parse error for later mailing. */
if (errstr != NULL) {
if (asprintf(©, U_("%s: %s"), message, errstr) == -1)
copy = NULL;
} else {
copy = strdup(message);
}
if (copy != NULL) {
/* journal_parse_error() takes ownership of copy on success. */
if (!journal_parse_error(copy)) {
free(copy);
ret = false;
}
}
}
/*
* Tell the user (in their locale).
*/
if (!ISSET(flags, SLOG_NO_STDERR)) {
sudoers_setlocale(SUDOERS_LOCALE_USER, NULL);
if (ISSET(flags, SLOG_USE_ERRNO)) {
errno = errnum;
sudo_vwarn_nodebug(_(fmt), ap2);
} else if (ISSET(flags, SLOG_GAI_ERRNO)) {
sudo_gai_vwarn_nodebug(errnum, _(fmt), ap2);
} else {
sudo_vwarnx_nodebug(_(fmt), ap2);
}
}
done:
va_end(ap2);
sudoers_setlocale(oldlocale, NULL);
debug_return_bool(ret);
}
bool
log_warning(const struct sudoers_context *ctx, unsigned int flags,
const char * restrict fmt, ...)
{
va_list ap;
bool ret;
debug_decl(log_warning, SUDOERS_DEBUG_LOGGING);
/* Log the error. */
va_start(ap, fmt);
ret = vlog_warning(ctx, flags|SLOG_USE_ERRNO, errno, fmt, ap);
va_end(ap);
debug_return_bool(ret);
}
bool
log_warningx(const struct sudoers_context *ctx, unsigned int flags,
const char * restrict fmt, ...)
{
va_list ap;
bool ret;
debug_decl(log_warningx, SUDOERS_DEBUG_LOGGING);
/* Log the error. */
va_start(ap, fmt);
ret = vlog_warning(ctx, flags, 0, fmt, ap);
va_end(ap);
debug_return_bool(ret);
}
bool
gai_log_warning(const struct sudoers_context *ctx, unsigned int flags,
int errnum, const char * restrict fmt, ...)
{
va_list ap;
bool ret;
debug_decl(gai_log_warning, SUDOERS_DEBUG_LOGGING);
/* Log the error. */
va_start(ap, fmt);
ret = vlog_warning(ctx, flags|SLOG_GAI_ERRNO, errnum, fmt, ap);
va_end(ap);
debug_return_bool(ret);
}
/*
* Send mail about accumulated parser errors.
* Frees the list of parse errors when done.
*/
bool
mail_parse_errors(const struct sudoers_context *ctx)
{
const int evl_flags = EVLOG_RAW;
struct parse_error *pe;
struct eventlog evlog;
char **errors = NULL;
struct timespec now;
bool ret = false;
size_t n;
debug_decl(mail_parse_errors, SUDOERS_DEBUG_LOGGING);
if (STAILQ_EMPTY(&parse_error_list))
debug_return_bool(true);
if (sudo_gettime_real(&now) == -1) {
sudo_warn("%s", U_("unable to get time of day"));
goto done;
}
sudoers_to_eventlog(ctx, &evlog, ctx->runas.cmnd, ctx->runas.argv,
NULL, ctx->uuid_str);
/* Convert parse_error_list to a string vector. */
n = 0;
STAILQ_FOREACH(pe, &parse_error_list, entries) {
n++;
}
errors = reallocarray(NULL, n + 1, sizeof(char *));
if (errors == NULL) {
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
goto done;
}
n = 0;
STAILQ_FOREACH(pe, &parse_error_list, entries) {
errors[n++] = _(pe->errstr);
}
errors[n] = NULL;
ret = eventlog_mail(&evlog, evl_flags, &now, _("problem parsing sudoers"),
NULL, errors);
done:
free(errors);
while ((pe = STAILQ_FIRST(&parse_error_list)) != NULL) {
STAILQ_REMOVE_HEAD(&parse_error_list, entries);
free(pe->errstr);
free(pe);
}
debug_return_bool(ret);
}
/*
* Log a parse error using log_warningx().
* Journals the message to be mailed after parsing is complete.
* Does not write the message to stderr.
*/
bool
log_parse_error(const struct sudoers_context *ctx, const char *file,
int line, int column, const char * restrict fmt, va_list args)
{
const unsigned int flags = SLOG_RAW_MSG|SLOG_NO_STDERR;
char *message, *tofree = NULL;
const char *errstr;
bool ret;
int len;
debug_decl(log_parse_error, SUDOERS_DEBUG_LOGGING);
if (fmt == NULL) {
errstr = _("syntax error");
} else if (strcmp(fmt, "%s") == 0) {
/* Optimize common case, a single string. */
errstr = _(va_arg(args, char *));
} else {
if (vasprintf(&tofree, _(fmt), args) == -1)
debug_return_bool(false);
errstr = tofree;
}
if (line > 0) {
ret = log_warningx(ctx, flags, N_("%s:%d:%d: %s"), file, line, column,
errstr);
} else {
ret = log_warningx(ctx, flags, N_("%s: %s"), file, errstr);
}
/* Journal parse error for later mailing. */
if (line > 0) {
len = asprintf(&message, _("%s:%d:%d: %s"), file, line, column, errstr);
} else {
len = asprintf(&message, _("%s: %s"), file, errstr);
}
if (len != -1) {
if (!journal_parse_error(message)) {
free(message);
ret = false;
}
} else {
ret = false;
}
free(tofree);
debug_return_bool(ret);
}
/*
* Determine whether we should send mail based on "status" and defaults options.
*/
static bool
should_mail(const struct sudoers_context *ctx, unsigned int status)
{
debug_decl(should_mail, SUDOERS_DEBUG_LOGGING);
if (!def_mailto || !def_mailerpath || access(def_mailerpath, X_OK) == -1)
debug_return_bool(false);
debug_return_bool(def_mail_always || ISSET(status, VALIDATE_ERROR) ||
(def_mail_all_cmnds && ISSET(ctx->mode, (MODE_RUN|MODE_EDIT))) ||
(def_mail_no_user && ISSET(status, FLAG_NO_USER)) ||
(def_mail_no_host && ISSET(status, FLAG_NO_HOST)) ||
(def_mail_no_perms && !ISSET(status, VALIDATE_SUCCESS)));
}
/*
* Build a struct eventlog from sudoers data.
* The values in the resulting eventlog struct should not be freed.
*/
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)
{
struct group *grp;
debug_decl(sudoers_to_eventlog, SUDOERS_DEBUG_LOGGING);
/* We rely on the reference held by the group cache. */
if ((grp = sudo_getgrgid(ctx->user.pw->pw_gid)) != NULL)
sudo_gr_delref(grp);
memset(evlog, 0, sizeof(*evlog));
evlog->iolog_file = ctx->iolog_file;
evlog->iolog_path = ctx->iolog_path;
evlog->command = cmnd ? (char *)cmnd : (runargv ? runargv[0] : NULL);
evlog->cwd = ctx->user.cwd;
if (def_runchroot != NULL && strcmp(def_runchroot, "*") != 0) {
evlog->runchroot = def_runchroot;
}
if (def_runcwd && strcmp(def_runcwd, "*") != 0) {
evlog->runcwd = def_runcwd;
} else if (ISSET(ctx->mode, MODE_LOGIN_SHELL) && ctx->runas.pw != NULL) {
evlog->runcwd = ctx->runas.pw->pw_dir;
} else {
evlog->runcwd = ctx->user.cwd;
}
evlog->rungroup = ctx->runas.gr ? ctx->runas.gr->gr_name : ctx->runas.group;
evlog->source = ctx->source;
evlog->submithost = ctx->user.host;
evlog->submituser = ctx->user.name;
if (grp != NULL)
evlog->submitgroup = grp->gr_name;
evlog->ttyname = ctx->user.ttypath;
evlog->runargv = (char **)runargv;
evlog->env_add = (char **)ctx->user.env_add;
evlog->runenv = (char **)runenv;
evlog->submitenv = (char **)ctx->user.envp;
if (sudo_gettime_real(&evlog->event_time) == -1) {
sudo_warn("%s", U_("unable to get time of day"));
}
evlog->lines = ctx->user.lines;
evlog->columns = ctx->user.cols;
if (ctx->runas.pw != NULL) {
evlog->rungid = ctx->runas.pw->pw_gid;
evlog->runuid = ctx->runas.pw->pw_uid;
evlog->runuser = ctx->runas.pw->pw_name;
} else {
evlog->rungid = (gid_t)-1;
evlog->runuid = (uid_t)-1;
evlog->runuser = ctx->runas.user;
}
if (uuid_str == NULL) {
unsigned char uuid[16];
sudo_uuid_create(uuid);
if (sudo_uuid_to_string(uuid, evlog->uuid_str, sizeof(evlog->uuid_str)) == NULL)
sudo_warnx("%s", U_("unable to generate UUID"));
} else {
strlcpy(evlog->uuid_str, uuid_str, sizeof(evlog->uuid_str));
}
if (ISSET(ctx->mode, MODE_POLICY_INTERCEPTED)) {
if (sudo_gettime_awake(&evlog->iolog_offset) == -1) {
sudo_warn("%s", U_("unable to get time of day"));
} else {
sudo_timespecsub(&evlog->iolog_offset, &ctx->start_time,
&evlog->iolog_offset);
}
}
debug_return;
}
static FILE *
sudoers_log_open(int type, const char *log_file)
{
const char *omode;
bool uid_changed;
FILE *fp = NULL;
mode_t oldmask;
int fd, flags;
debug_decl(sudoers_log_open, SUDOERS_DEBUG_LOGGING);
switch (type) {
case EVLOG_SYSLOG:
openlog("sudo", def_syslog_pid ? LOG_PID : 0, def_syslog);
break;
case EVLOG_FILE:
/* Open log file as root, mode 0600 (cannot append to JSON). */
if (def_log_format == json || def_log_format == json_pretty) {
flags = O_RDWR|O_CREAT;
omode = "w";
} else {
flags = O_WRONLY|O_APPEND|O_CREAT;
omode = "a";
}
oldmask = umask(S_IRWXG|S_IRWXO);
uid_changed = set_perms(NULL, PERM_ROOT);
fd = open(log_file, flags, S_IRUSR|S_IWUSR);
if (uid_changed && !restore_perms()) {
if (fd != -1) {
close(fd);
fd = -1;
}
}
(void) umask(oldmask);
if (fd == -1 || (fp = fdopen(fd, omode)) == NULL) {
if (!warned) {
warned = true;
sudo_warn(U_("unable to open log file %s"), log_file);
}
if (fd != -1)
close(fd);
}
break;
default:
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unsupported log type %d", type);
break;
}
debug_return_ptr(fp);
}
static void
sudoers_log_close(int type, FILE *fp)
{
debug_decl(sudoers_log_close, SUDOERS_DEBUG_LOGGING);
switch (type) {
case EVLOG_SYSLOG:
break;
case EVLOG_FILE:
if (fp == NULL) {
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"tried to close NULL log stream");
break;
}
(void)fflush(fp);
if (ferror(fp) && !warned) {
warned = true;
sudo_warn(U_("unable to write log file %s"), def_logfile);
}
fclose(fp);
break;
default:
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO,
"unsupported log type %d", type);
break;
}
debug_return;
}
void
init_eventlog_config(void)
{
int logtype = 0;
debug_decl(init_eventlog_config, SUDOERS_DEBUG_LOGGING);
if (def_syslog)
logtype |= EVLOG_SYSLOG;
if (def_logfile)
logtype |= EVLOG_FILE;
sudoers_set_log_format(def_log_format);
eventlog_set_type(logtype);
eventlog_set_syslog_acceptpri(def_syslog_goodpri);
eventlog_set_syslog_rejectpri(def_syslog_badpri);
eventlog_set_syslog_alertpri(def_syslog_badpri);
eventlog_set_syslog_maxlen(def_syslog_maxlen);
eventlog_set_file_maxlen(def_loglinelen);
eventlog_set_mailuid(ROOT_UID);
eventlog_set_omit_hostname(!def_log_host);
eventlog_set_logpath(def_logfile);
eventlog_set_time_fmt(def_log_year ? "%h %e %T %Y" : "%h %e %T");
eventlog_set_mailerpath(def_mailerpath);
eventlog_set_mailerflags(def_mailerflags);
eventlog_set_mailfrom(def_mailfrom);
eventlog_set_mailto(def_mailto);
eventlog_set_mailsub(def_mailsub);
eventlog_set_open_log(sudoers_log_open);
eventlog_set_close_log(sudoers_log_close);
debug_return;
}
|