Beispiel #1
0
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 */
    }
}
Beispiel #2
0
int
main(int argc, char *argv[])
{
    size_t len;
    FILE *fp;
    char *cp, *dash, *line, lines[2][2048];
    int which = 0;

    if (argc != 2)
        usage();

    fp = fopen(argv[1], "r");
    if (fp == NULL)
        errorx(1, "unable to open %s", argv[1]);

    /*
     * Each test record consists of a log entry on one line and a list of
     * line lengths to test it with on the next.  E.g.
     *
     * Jun 30 14:49:51 : millert : TTY=ttypn ; PWD=/usr/src/local/millert/hg/sudo/trunk/plugins/sudoers ; USER=root ; TSID=0004LD ; COMMAND=/usr/local/sbin/visudo
     * 60-80,40
     */
    while ((line = fgets(lines[which], sizeof(lines[which]), fp)) != NULL) {
        len = strcspn(line, "\n");
        line[len] = '\0';

        /* If we read the 2nd line, parse list of line lengths and check. */
        if (which) {
            for (cp = strtok(lines[1], ","); cp != NULL; cp = strtok(NULL, ",")) {
                size_t maxlen;
                /* May be either a number or a range. */
                len = maxlen = atoi(cp);
                dash = strchr(cp, '-');
                if (dash)
                    maxlen = atoi(dash + 1);
                while (len <= maxlen) {
                    printf("# word wrap at %d characters\n", (int)len);
                    writeln_wrap(stdout, lines[0], strlen(lines[0]), len);
                    len++;
                }
            }
        }
        which = !which;
    }

    exit(0);
}
Beispiel #3
0
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);
}