static void do_logfile(char *msg) { char *full_line; size_t len; mode_t oldmask; time_t now; FILE *fp; oldmask = umask(077); fp = fopen(def_logfile, "a"); (void) umask(oldmask); if (fp == NULL) { send_mail(_("unable to open log file: %s: %s"), def_logfile, strerror(errno)); } else if (!lock_file(fileno(fp), SUDO_LOCK)) { send_mail(_("unable to lock log file: %s: %s"), def_logfile, strerror(errno)); } else { #ifdef HAVE_SETLOCALE const char *old_locale = estrdup(setlocale(LC_ALL, NULL)); if (!setlocale(LC_ALL, def_sudoers_locale)) setlocale(LC_ALL, "C"); #endif /* HAVE_SETLOCALE */ now = time(NULL); if (def_loglinelen < sizeof(LOG_INDENT)) { /* Don't pretty-print long log file lines (hard to grep) */ if (def_log_host) (void) fprintf(fp, "%s : %s : HOST=%s : %s\n", get_timestr(now, def_log_year), user_name, user_shost, msg); else (void) fprintf(fp, "%s : %s : %s\n", get_timestr(now, def_log_year), user_name, msg); } else { if (def_log_host) len = easprintf(&full_line, "%s : %s : HOST=%s : %s", get_timestr(now, def_log_year), user_name, user_shost, msg); else len = easprintf(&full_line, "%s : %s : %s", get_timestr(now, def_log_year), user_name, msg); /* * Print out full_line with word wrap around def_loglinelen chars. */ writeln_wrap(fp, full_line, len, def_loglinelen); efree(full_line); } (void) fflush(fp); (void) lock_file(fileno(fp), SUDO_UNLOCK); (void) fclose(fp); #ifdef HAVE_SETLOCALE setlocale(LC_ALL, old_locale); efree((void *)old_locale); #endif /* HAVE_SETLOCALE */ } }
int update_rx_poweroff_info_of_otherform(void) { char str[16]; rt_uint32_t ntime, n_1_time, n_2_time; i2str(str, poweroff_info_data.poi_rx_poweroff_cnt); rtgui_form_set_item(other_form, str, OT_RE_POWEROFF_CNT_ROW, OT_RE_POWEROFF_CNT_COL, 0); get_rx_last3poweroff_time(&ntime, &n_1_time, &n_2_time); get_timestr(ntime, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_RE_N_ROW, OT_RE_N_COL, 0); get_timestr(n_1_time, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_RE_N_1_ROW, OT_RE_N_1_COL, 0); get_timestr(n_2_time, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_RE_N_2_ROW, OT_RE_N_2_COL, 0); return SUCC; }
int update_tx_poweroff_info_of_otherform(void) { char str[16]; rt_uint32_t ntime, n_1_time, n_2_time; if (SUC_FIBER_CHANNEL_2 == get_sys_use_channel()) { i2str(str, poweroff_info_data.poi_tx2_poweroff_cnt); get_tx2_last3poweroff_time(&ntime, &n_1_time, &n_2_time); } else { i2str(str, poweroff_info_data.poi_tx1_poweroff_cnt); get_tx_last3poweroff_time(&ntime, &n_1_time, &n_2_time); } rtgui_form_set_item(other_form, str, OT_SE_POWEROFF_CNT_ROW, OT_SE_POWEROFF_CNT_COL, 0); get_timestr(ntime, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_SE_N_ROW, OT_SE_N_COL, 0); get_timestr(n_1_time, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_SE_N_1_ROW, OT_SE_N_1_COL, 0); get_timestr(n_2_time, str, sizeof(str)); rtgui_form_set_item(other_form, str, OT_SE_N_2_ROW, OT_SE_N_2_COL, 0); return SUCC; }
static bool do_logfile(const char *msg) { static bool warned = false; const char *timestr; int len, oldlocale; bool ret = false; char *full_line; mode_t oldmask; FILE *fp; debug_decl(do_logfile, SUDOERS_DEBUG_LOGGING) sudoers_setlocale(SUDOERS_LOCALE_SUDOERS, &oldlocale); oldmask = umask(S_IRWXG|S_IRWXO); fp = fopen(def_logfile, "a"); (void) umask(oldmask); if (fp == NULL) { if (!warned) { log_warning(SLOG_SEND_MAIL|SLOG_NO_LOG, N_("unable to open log file: %s"), def_logfile); warned = true; } goto done; } if (!sudo_lock_file(fileno(fp), SUDO_LOCK)) { if (!warned) { log_warning(SLOG_SEND_MAIL|SLOG_NO_LOG, N_("unable to lock log file: %s"), def_logfile); warned = true; } goto done; } timestr = get_timestr(time(NULL), def_log_year); if (timestr == NULL) timestr = "invalid date"; if (def_log_host) { len = asprintf(&full_line, "%s : %s : HOST=%s : %s", timestr, user_name, user_srunhost, msg); } else { len = asprintf(&full_line, "%s : %s : %s", timestr, user_name, msg); } if (len == -1) { sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory")); goto done; } if ((size_t)def_loglinelen < sizeof(LOG_INDENT)) { /* Don't pretty-print long log file lines (hard to grep). */ (void) fputs(full_line, fp); (void) fputc('\n', fp); } else { /* Write line with word wrap around def_loglinelen chars. */ writeln_wrap(fp, full_line, len, def_loglinelen); } free(full_line); (void) fflush(fp); if (ferror(fp)) { if (!warned) { log_warning(SLOG_SEND_MAIL|SLOG_NO_LOG, N_("unable to write log file: %s"), def_logfile); warned = true; } goto done; } ret = true; done: if (fp != NULL) (void) fclose(fp); sudoers_setlocale(oldlocale, NULL); debug_return_bool(ret); }
/* * Send a message to MAILTO user */ static void send_mail(const char *fmt, ...) { FILE *mail; char *p; int fd, pfd[2], status; pid_t pid, rv; sigaction_t sa; va_list ap; #ifndef NO_ROOT_MAILER static char *root_envp[] = { "HOME=/", "PATH=/usr/bin:/bin:/usr/sbin:/sbin", "LOGNAME=root", "USERNAME=root", "USER=root", NULL }; #endif /* NO_ROOT_MAILER */ /* Just return if mailer is disabled. */ if (!def_mailerpath || !def_mailto) return; /* Fork and return, child will daemonize. */ switch (pid = fork()) { case -1: /* Error. */ error(1, _("unable to fork")); break; case 0: /* Child. */ switch (pid = fork()) { case -1: /* Error. */ mysyslog(LOG_ERR, _("unable to fork: %m")); _exit(1); case 0: /* Grandchild continues below. */ break; default: /* Parent will wait for us. */ _exit(0); } break; default: /* Parent. */ do { rv = waitpid(pid, &status, 0); } while (rv == -1 && errno == EINTR); return; } /* Daemonize - disassociate from session/tty. */ if (setsid() == -1) warning("setsid"); if (chdir("/") == -1) warning("chdir(/)"); if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) { (void) dup2(fd, STDIN_FILENO); (void) dup2(fd, STDOUT_FILENO); (void) dup2(fd, STDERR_FILENO); } #ifdef HAVE_SETLOCALE if (!setlocale(LC_ALL, def_sudoers_locale)) { setlocale(LC_ALL, "C"); efree(def_sudoers_locale); def_sudoers_locale = estrdup("C"); } #endif /* HAVE_SETLOCALE */ /* Close password, group and other fds so we don't leak. */ sudo_endpwent(); sudo_endgrent(); closefrom(STDERR_FILENO + 1); /* Ignore SIGPIPE in case mailer exits prematurely (or is missing). */ zero_bytes(&sa, sizeof(sa)); sigemptyset(&sa.sa_mask); sa.sa_flags = SA_INTERRUPT; sa.sa_handler = SIG_IGN; (void) sigaction(SIGPIPE, &sa, NULL); if (pipe(pfd) == -1) { mysyslog(LOG_ERR, _("unable to open pipe: %m")); _exit(1); } switch (pid = fork()) { case -1: /* Error. */ mysyslog(LOG_ERR, _("unable to fork: %m")); _exit(1); break; case 0: { char *argv[MAX_MAILFLAGS + 1]; char *mpath, *mflags; int i; /* Child, set stdin to output side of the pipe */ if (pfd[0] != STDIN_FILENO) { if (dup2(pfd[0], STDIN_FILENO) == -1) { mysyslog(LOG_ERR, _("unable to dup stdin: %m")); _exit(127); } (void) close(pfd[0]); } (void) close(pfd[1]); /* Build up an argv based on the mailer path and flags */ mflags = estrdup(def_mailerflags); mpath = estrdup(def_mailerpath); if ((argv[0] = strrchr(mpath, ' '))) argv[0]++; else argv[0] = mpath; i = 1; if ((p = strtok(mflags, " \t"))) { do { argv[i] = p; } while (++i < MAX_MAILFLAGS && (p = strtok(NULL, " \t"))); } argv[i] = NULL; /* * Depending on the config, either run the mailer as root * (so user cannot kill it) or as the user (for the paranoid). */ #ifndef NO_ROOT_MAILER set_perms(PERM_ROOT|PERM_NOEXIT); execve(mpath, argv, root_envp); #else set_perms(PERM_FULL_USER|PERM_NOEXIT); execv(mpath, argv); #endif /* NO_ROOT_MAILER */ mysyslog(LOG_ERR, _("unable to execute %s: %m"), mpath); _exit(127); } break; } (void) close(pfd[0]); mail = fdopen(pfd[1], "w"); /* Pipes are all setup, send message. */ (void) fprintf(mail, "To: %s\nFrom: %s\nAuto-Submitted: %s\nSubject: ", def_mailto, def_mailfrom ? def_mailfrom : user_name, "auto-generated"); for (p = def_mailsub; *p; p++) { /* Expand escapes in the subject */ if (*p == '%' && *(p+1) != '%') { switch (*(++p)) { case 'h': (void) fputs(user_host, mail); break; case 'u': (void) fputs(user_name, mail); break; default: p--; break; } } else (void) fputc(*p, mail); } #ifdef HAVE_NL_LANGINFO if (strcmp(def_sudoers_locale, "C") != 0) (void) fprintf(mail, "\nContent-Type: text/plain; charset=\"%s\"\nContent-Transfer-Encoding: 8bit", nl_langinfo(CODESET)); #endif /* HAVE_NL_LANGINFO */ (void) fprintf(mail, "\n\n%s : %s : %s : ", user_host, get_timestr(time(NULL), def_log_year), user_name); va_start(ap, fmt); (void) vfprintf(mail, fmt, ap); va_end(ap); fputs("\n\n", mail); fclose(mail); do { rv = waitpid(pid, &status, 0); } while (rv == -1 && errno == EINTR); _exit(0); }
static int list_session_dir(char *pathbuf, REGEX_T *re, const char *user, const char *tty) { FILE *fp; DIR *d; struct dirent *dp; char *buf = NULL, *cmd = NULL, *cwd = NULL, idstr[7], *cp; struct log_info li; size_t bufsize = 0, cwdsize = 0, cmdsize = 0, plen; plen = strlen(pathbuf); d = opendir(pathbuf); if (d == NULL && errno != ENOTDIR) { warning("cannot opendir %s", pathbuf); return -1; } while ((dp = readdir(d)) != NULL) { if (NAMLEN(dp) != 2 || !isalnum((unsigned char)dp->d_name[0]) || !isalnum((unsigned char)dp->d_name[1])) continue; /* open log file, print id and command */ pathbuf[plen + 0] = '/'; pathbuf[plen + 1] = dp->d_name[0]; pathbuf[plen + 2] = dp->d_name[1]; pathbuf[plen + 3] = '/'; pathbuf[plen + 4] = 'l'; pathbuf[plen + 5] = 'o'; pathbuf[plen + 6] = 'g'; pathbuf[plen + 7] = '\0'; fp = fopen(pathbuf, "r"); if (fp == NULL) { warning("unable to open %s", pathbuf); continue; } /* * ID file has three lines: * 1) a log info line * 2) cwd * 3) command with args */ if (getline(&buf, &bufsize, fp) == -1 || getline(&cwd, &cwdsize, fp) == -1 || getline(&cmd, &cmdsize, fp) == -1) { fclose(fp); continue; } fclose(fp); /* crack the log line: timestamp:user:runas_user:runas_group:tty */ buf[strcspn(buf, "\n")] = '\0'; if ((li.tstamp = atoi(buf)) == 0) continue; if ((cp = strchr(buf, ':')) == NULL) continue; *cp++ = '\0'; li.user = cp; if ((cp = strchr(cp, ':')) == NULL) continue; *cp++ = '\0'; li.runas_user = cp; if ((cp = strchr(cp, ':')) == NULL) continue; *cp++ = '\0'; li.runas_group = cp; if ((cp = strchr(cp, ':')) == NULL) continue; *cp++ = '\0'; li.tty = cp; cwd[strcspn(cwd, "\n")] = '\0'; li.cwd = cwd; cmd[strcspn(cmd, "\n")] = '\0'; li.cmd = cmd; /* Match on search expression if there is one. */ if (search_expr && !match_expr(search_expr, &li)) continue; /* Convert from /var/log/sudo-sessions/00/00/01 to 000001 */ idstr[0] = pathbuf[plen - 5]; idstr[1] = pathbuf[plen - 4]; idstr[2] = pathbuf[plen - 2]; idstr[3] = pathbuf[plen - 1]; idstr[4] = pathbuf[plen + 1]; idstr[5] = pathbuf[plen + 2]; idstr[6] = '\0'; printf("%s : %s : TTY=%s ; CWD=%s ; USER=%s ; ", get_timestr(li.tstamp, 1), li.user, li.tty, li.cwd, li.runas_user); if (*li.runas_group) printf("GROUP=%s ; ", li.runas_group); printf("TSID=%s ; COMMAND=%s\n", idstr, li.cmd); } return 0; }