示例#1
0
/*
 *	Log the query to a file.
 */
void rlm_sql_query_log(rlm_sql_t *inst, REQUEST *request,
		       sql_acct_section_t *section, char const *query)
{
	int fd;
	char const *filename = NULL;
	char *expanded = NULL;
	size_t len;
	bool failed = false;	/* Write the log message outside of the critical region */

	if (section) {
		filename = section->logfile;
	} else {
		filename = inst->config->logfile;
	}

	if (!filename) {
		return;
	}

	if (radius_axlat(&expanded, request, filename, NULL, NULL) < 0) {
		return;
	}

	fd = fr_logfile_open(inst->lf, filename, 0640);
	if (fd < 0) {
		ERROR("rlm_sql (%s): Couldn't open logfile '%s': %s", inst->config->xlat_name,
		      expanded, fr_syserror(errno));

		talloc_free(expanded);
		return;
	}

	len = strlen(query);
	if ((write(fd, query, len) < 0) || (write(fd, ";\n", 2) < 0)) {
		failed = true;
	}

	if (failed) {
		ERROR("rlm_sql (%s): Failed writing to logfile '%s': %s", inst->config->xlat_name, expanded,
		      fr_syserror(errno));
	}

	talloc_free(expanded);
	fr_logfile_close(inst->lf, fd);
}
示例#2
0
static rlm_rcode_t do_linelog(void *instance, REQUEST *request)
{
    int fd = -1;
    char buffer[4096];
    char *p;
    char line[1024];
    rlm_linelog_t *inst = (rlm_linelog_t*) instance;
    char const *value = inst->line;

#ifdef HAVE_GRP_H
    gid_t gid;
    struct group *grp;
    char *endptr;
#endif

    if (inst->reference) {
        CONF_ITEM *ci;
        CONF_PAIR *cp;

        p = line + 1;

        if (radius_xlat(p, sizeof(line) - 2, request, inst->reference, linelog_escape_func,
                        NULL) < 0) {
            return RLM_MODULE_FAIL;
        }

        line[0] = '.';	/* force to be in current section */

        /*
         *	Don't allow it to go back up
         */
        if (line[1] == '.') goto do_log;

        ci = cf_reference_item(NULL, inst->cs, line);
        if (!ci) {
            RDEBUG2("No such entry \"%s\"", line);
            return RLM_MODULE_NOOP;
        }

        if (!cf_item_is_pair(ci)) {
            RDEBUG2("Entry \"%s\" is not a variable assignment ", line);
            goto do_log;
        }

        cp = cf_itemtopair(ci);
        value = cf_pair_value(cp);
        if (!value) {
            RDEBUG2("Entry \"%s\" has no value", line);
            goto do_log;
        }

        /*
         *	Value exists, but is empty.  Don't log anything.
         */
        if (!*value) return RLM_MODULE_OK;
    }

do_log:
    /*
     *	FIXME: Check length.
     */
    if (strcmp(inst->filename, "syslog") != 0) {
        if (radius_xlat(buffer, sizeof(buffer), request, inst->filename, NULL, NULL) < 0) {
            return RLM_MODULE_FAIL;
        }

        /* check path and eventually create subdirs */
        p = strrchr(buffer,'/');
        if (p) {
            *p = '\0';
            if (rad_mkdir(buffer, 0700) < 0) {
                RERROR("rlm_linelog: Failed to create directory %s: %s", buffer, fr_syserror(errno));
                return RLM_MODULE_FAIL;
            }
            *p = '/';
        }

        fd = fr_logfile_open(inst->lf, buffer, inst->permissions);
        if (fd == -1) {
            ERROR("rlm_linelog: Failed to open %s: %s",
                  buffer, fr_syserror(errno));
            return RLM_MODULE_FAIL;
        }

#ifdef HAVE_GRP_H
        if (inst->group != NULL) {
            gid = strtol(inst->group, &endptr, 10);
            if (*endptr != '\0') {
                grp = getgrnam(inst->group);
                if (!grp) {
                    RDEBUG2("Unable to find system group \"%s\"", inst->group);
                    goto skip_group;
                }
                gid = grp->gr_gid;
            }

            if (chown(buffer, -1, gid) == -1) {
                RDEBUG2("Unable to change system group of \"%s\"", buffer);
            }
        }
#endif
    }

skip_group:

    /*
     *	FIXME: Check length.
     */
    if (radius_xlat(line, sizeof(line) - 1, request, value, linelog_escape_func, NULL) < 0) {
        if (fd > -1) {
            fr_logfile_close(inst->lf, fd);
        }

        return RLM_MODULE_FAIL;
    }

    if (fd >= 0) {
        strcat(line, "\n");

        if (write(fd, line, strlen(line)) < 0) {
            EDEBUG("rlm_linelog: Failed writing: %s", fr_syserror(errno));
            fr_logfile_close(inst->lf, fd);
            return RLM_MODULE_FAIL;
        }

        fr_logfile_close(inst->lf, fd);

#ifdef HAVE_SYSLOG_H
    } else {
        syslog(inst->facility, "%s", line);
#endif
    }

    return RLM_MODULE_OK;
}