Exemplo n.º 1
0
ConstSite ConstContext::site() const
{
    const ib_site_t *ctx;
    ib_status_t rc;

    rc = ib_context_site_get(ib(), &ctx);
    if (rc != IB_OK) {
        // TODO
    }

    return ConstSite(ctx);
}
Exemplo n.º 2
0
ConstSite ConstContext::site() const
{
    const ib_site_t *site = NULL;
    ib_status_t rc;

    rc = ib_context_site_get(ib(), &site);
    if ((rc != IB_OK) || (site == NULL)) {
        // TODO
    }

    return ConstSite(site);
}
Exemplo n.º 3
0
static ib_status_t core_audit_get_index_line(ib_engine_t *ib,
                                             ib_auditlog_t *log,
                                             char *line,
                                             size_t line_size,
                                             size_t *line_len)
{
    assert(ib != NULL);
    assert(log != NULL);
    assert(line != NULL);
    assert(line_size > 0);
    assert(line_len != NULL);
    assert(log->tx != NULL);

    ib_core_audit_cfg_t *cfg = (ib_core_audit_cfg_t *)log->cfg_data;
    ib_core_cfg_t *corecfg;
    ib_tx_t *tx = log->tx;
    ib_conn_t *conn = tx->conn;
    const ib_site_t *site;
    const ib_logformat_t *lf;
    ib_status_t rc;
    auditlog_callback_data_t cbdata;

    /* Get the site */
    rc = ib_context_site_get(log->ctx, &site);
    if (rc != IB_OK) {
        return rc;
    }

    /* Retrieve corecfg to get the AuditLogIndexFormat */
    rc = ib_core_context_config(log->ctx, &corecfg);
    if (rc != IB_OK) {
        return rc;
    }

    lf = corecfg->auditlog_index_hp;

    cbdata.cfg = cfg;
    cbdata.log = log;
    cbdata.tx = tx;
    cbdata.conn = conn;
    cbdata.site = site;
    rc = ib_logformat_format(lf, line, line_size, line_len,
                             audit_add_line_item, &cbdata);

    return rc;
}
Exemplo n.º 4
0
ib_status_t core_audit_open_auditfile(ib_engine_t *ib,
                                      ib_auditlog_t *log,
                                      ib_core_audit_cfg_t *cfg,
                                      ib_core_cfg_t *corecfg)
{
    const int dtmp_sz = 64;
    const int dn_sz = 512;
    char *dtmp = (char *)malloc(dtmp_sz);
    char *dn = (char *)malloc(dn_sz);
    char *audit_filename;
    size_t audit_filename_sz;
    char *temp_filename;
    size_t temp_filename_sz;
    int fd;
    const time_t log_seconds = IB_CLOCK_SECS(log->tx->t.logtime);
    int sys_rc;
    ib_status_t ib_rc;
    struct tm gmtime_result;
    const ib_site_t *site;

    if (dtmp == NULL || dn == NULL) {
        if (dtmp != NULL) {
            free(dtmp);
        }
        if (dn != NULL) {
            free(dn);
        }
        return IB_EALLOC;
    }

    gmtime_r(&log_seconds, &gmtime_result);

    /* Generate the audit log filename template. */
    if (*(corecfg->auditlog_sdir_fmt) != 0) {
        size_t ret = strftime(dtmp, dtmp_sz,
                              corecfg->auditlog_sdir_fmt, &gmtime_result);
        if (ret == 0) {
            /// @todo Better error - probably should validate at cfg time
            ib_log_error(log->ib,
                         "Failed to create audit log filename template, "
                         "using default:"
                         " name too long");
            *dtmp = 0;
        }
    }
    else {
        *dtmp = 0;
    }

    /* Generate the full audit log directory name. */
    sys_rc = snprintf(dn, dn_sz, "%s%s%s",
                      corecfg->auditlog_dir, (*dtmp)?"/":"", dtmp);
    if (sys_rc >= dn_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Failed to create audit log directory: name too long");
        free(dtmp);
        free(dn);
        return IB_EINVAL;
    }

    /* Get the site */
    ib_rc = ib_context_site_get(log->ctx, &site);
    if (ib_rc != IB_OK) {
        free(dtmp);
        free(dn);
        return ib_rc;
    }

    /* Generate the full audit log filename. */
    if (site != NULL) {
        audit_filename_sz = strlen(dn) + strlen(cfg->tx->id) +
            strlen(site->id) + 7;
        audit_filename = (char *)ib_mm_alloc(cfg->tx->mm, audit_filename_sz);
        sys_rc = snprintf(audit_filename,
                          audit_filename_sz,
                          "%s/%s_%s.log", dn, cfg->tx->id,site->id);
    }
    else {
        audit_filename_sz = strlen(dn) + strlen(cfg->tx->id) + 6;
        audit_filename = (char *)ib_mm_alloc(cfg->tx->mm, audit_filename_sz);
        sys_rc = snprintf(audit_filename,
                          audit_filename_sz,
                          "%s/%s.log", dn, cfg->tx->id);
    }
    if (sys_rc >= (int)audit_filename_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Failed to create audit log filename: name too long");
        ib_rule_log_add_audit(cfg->tx->rule_exec, audit_filename, true);
        free(dtmp);
        free(dn);
        return IB_EINVAL;
    }

    ib_rc = ib_util_mkpath(dn, corecfg->auditlog_dmode);
    if (ib_rc != IB_OK) {
        ib_log_error(log->ib,
                     "Failed to create audit log dir: %s", dn);
        ib_rule_log_add_audit(cfg->tx->rule_exec, audit_filename, true);
        free(dtmp);
        free(dn);
        return ib_rc;
    }

    // Create temporary filename to use while writing the audit log
    temp_filename_sz = strlen(audit_filename) + 6;
    temp_filename = (char *)ib_mm_alloc(cfg->tx->mm, temp_filename_sz);
    if (temp_filename == NULL) {
      free(dtmp);
      free(dn);
      return IB_EALLOC;
    }
    sys_rc = snprintf(temp_filename,
                      temp_filename_sz,
                      "%s.part", audit_filename);
    if (sys_rc >= (int)temp_filename_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Failed to create temporary audit log filename: name too long");
        ib_rule_log_add_audit(cfg->tx->rule_exec, audit_filename, true);
        free(dtmp);
        free(dn);
        return IB_EINVAL;
    }

    /* Open the file.  Use open() & fdopen() to avoid chmod() */
    fd = open(temp_filename,
              (O_WRONLY|O_APPEND|O_CREAT|O_BINARY),
              corecfg->auditlog_fmode);
    if (fd >= 0) {
        cfg->fp = fdopen(fd, "ab");
        if (cfg->fp == NULL) {
            close(fd);
        }
    }
    if ( (fd < 0) || (cfg->fp == NULL) ) {
        sys_rc = errno;
        ib_log_error(log->ib,
                     "Error opening audit log \"%s\": %s (%d)",
                     temp_filename, strerror(sys_rc), sys_rc);
        ib_rule_log_add_audit(cfg->tx->rule_exec, audit_filename, true);
        free(dtmp);
        free(dn);
        return IB_EINVAL;
    }

    /* Track the relative audit log filename. */
    cfg->fn = audit_filename + (strlen(corecfg->auditlog_dir) + 1);
    cfg->full_path = audit_filename;
    cfg->temp_path = temp_filename;

    /* Log it via the rule logger */
    ib_rule_log_add_audit(cfg->tx->rule_exec, audit_filename, false);

    free(dtmp);
    free(dn);

    return IB_OK;
}
Exemplo n.º 5
0
ib_status_t core_audit_get_index_line(ib_provider_inst_t *lpi,
                                      ib_auditlog_t *log,
                                      char *line,
                                      int *line_size)
{
    IB_FTRACE_INIT();
    core_audit_cfg_t *cfg = (core_audit_cfg_t *)log->cfg_data;
    ib_core_cfg_t *corecfg;
    ib_tx_t *tx = log->tx;
    ib_conn_t *conn = tx->conn;
    ib_site_t *site = ib_context_site_get(log->ctx);
    const ib_logformat_t *lf;
    ib_status_t rc;
    char *ptr = line;
    char *tstamp = NULL;
    uint8_t which;
    int i = 0;
    int l = 0;
    int used = 0;
    const char *aux = NULL;

    /* Retrieve corecfg to get the AuditLogIndexFormat */
    rc = ib_context_module_config(log->ctx, ib_core_module(),
                                  (void *)&corecfg);

    if (rc != IB_OK) {
        IB_FTRACE_RET_STATUS(rc);
    }

    lf = corecfg->auditlog_index_hp;
    which = lf->literal_starts ? 1 : 0;

    for (; (i < lf->field_cnt || l < lf->literal_cnt) &&
            used < IB_LOGFORMAT_MAXLINELEN;)
    {
        if (which++ % 2 == 0) {
            int aux_i = 0;

            switch (lf->fields[i]) {
                case IB_LOG_FIELD_REMOTE_ADDR:
                    aux = tx->er_ipstr;
                    break;
                case IB_LOG_FIELD_LOCAL_ADDR:
                    aux = conn->local_ipstr;
                    break;
                case IB_LOG_FIELD_HOSTNAME:
                     aux = tx->hostname;
                    break;
                case IB_LOG_FIELD_SITE_ID:
                    if (site == NULL) {
                         aux = (char *)"-";
                    }
                    else {
                         aux = site->id_str;
                    }
                    break;
                case IB_LOG_FIELD_SENSOR_ID:
                     aux = log->ib->sensor_id_str;
                    break;
                case IB_LOG_FIELD_TRANSACTION_ID:
                     aux = tx->id;
                    break;
                case IB_LOG_FIELD_TIMESTAMP:
                    /* Prepare timestamp (only if needed) */
                    tstamp = (char *)ib_mpool_alloc(log->mp, 30);
                    if (tstamp == NULL) {
                        IB_FTRACE_RET_STATUS(IB_EALLOC);
                    }

                    ib_clock_timestamp(tstamp, &tx->tv_created);
                    aux = tstamp;
                    break;
                case IB_LOG_FIELD_LOG_FILE:
                    aux = cfg->fn;
                    break;
                default:
                    ptr[used++] = '\n';
                    /* Not understood */
                    IB_FTRACE_RET_STATUS(IB_EINVAL);
                    break;
            }

            for (; aux != NULL && aux[aux_i] != '\0';) {
                if (used < IB_LOGFORMAT_MAXLINELEN) {
                    ptr[used++] = aux[aux_i++];
                }
                else {
                    ptr[used++] = '\n';
                    IB_FTRACE_RET_STATUS(IB_ETRUNC);
                }
            }
            ++i;
        }
        else {
            /* Use literals */
            if (used + lf->literals_len[l] < IB_LOGFORMAT_MAXLINELEN) {
                memcpy(&ptr[used], lf->literals[l], lf->literals_len[l]);
                used += lf->literals_len[l];
                ++l;
            }
            else {
                /* Truncated.. */
                ptr[used++] = '\n';
                IB_FTRACE_RET_STATUS(IB_ETRUNC);
            }
        }
    }
    ptr[used++] = '\n';
    *line_size = used;

    IB_FTRACE_RET_STATUS(IB_OK);
}
Exemplo n.º 6
0
ib_status_t core_audit_open_auditfile(ib_provider_inst_t *lpi,
                                      ib_auditlog_t *log,
                                      core_audit_cfg_t *cfg,
                                      ib_core_cfg_t *corecfg)
{
    IB_FTRACE_INIT();

    const int dtmp_sz = 64;
    const int dn_sz = 512;
    char *dtmp = (char *)malloc(dtmp_sz);
    char *dn = (char *)malloc(dn_sz);
    char *audit_filename;
    int audit_filename_sz;
    char *temp_filename;
    int temp_filename_sz;
    const time_t log_seconds = IB_CLOCK_SECS(log->tx->t.logtime);
    int sys_rc;
    ib_status_t ib_rc;
    struct tm gmtime_result;
    ib_site_t *site;

    if (dtmp == NULL || dn == NULL) {
        ib_log_error(log->ib,  "Failed to allocate internal buffers.");
        if (dtmp != NULL) {
            free(dtmp);
        }
        if (dn != NULL) {
            free(dn);
        }
        IB_FTRACE_RET_STATUS(IB_EALLOC);
    }

    gmtime_r(&log_seconds, &gmtime_result);

    /* Generate the audit log filename template. */
    if (*(corecfg->auditlog_sdir_fmt) != 0) {
        size_t ret = strftime(dtmp, dtmp_sz,
                              corecfg->auditlog_sdir_fmt, &gmtime_result);
        if (ret == 0) {
            /// @todo Better error - probably should validate at cfg time
            ib_log_error(log->ib,
                         "Could not create audit log filename template, "
                         "using default:"
                         " too long");
            *dtmp = 0;
        }
    }
    else {
        *dtmp = 0;
    }

    /* Generate the full audit log directory name. */
    sys_rc = snprintf(dn, dn_sz, "%s%s%s",
                  corecfg->auditlog_dir, (*dtmp)?"/":"", dtmp);
    if (sys_rc >= dn_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Could not create audit log directory: too long");
        free(dtmp);
        free(dn);
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    /* Generate the full audit log filename. */
    site = ib_context_site_get(log->ctx);
    if (site != NULL) {
        audit_filename_sz = strlen(dn) + strlen(cfg->tx->id) +
            strlen(site->id_str) + 7;
        audit_filename = (char *)ib_mpool_alloc(cfg->tx->mp, audit_filename_sz);
        sys_rc = snprintf(audit_filename,
                          audit_filename_sz,
                          "%s/%s_%s.log", dn, cfg->tx->id,site->id_str);
    }
    else {
        audit_filename_sz = strlen(dn) + strlen(cfg->tx->id) + 6;
        audit_filename = (char *)ib_mpool_alloc(cfg->tx->mp, audit_filename_sz);
        sys_rc = snprintf(audit_filename,
                          audit_filename_sz,
                          "%s/%s.log", dn, cfg->tx->id);
    }
    if (sys_rc >= (int)audit_filename_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Could not create audit log filename: too long");
        free(dtmp);
        free(dn);
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    ib_rc = ib_util_mkpath(dn, corecfg->auditlog_dmode);
    if (ib_rc != IB_OK) {
        ib_log_error(log->ib,
                     "Could not create audit log dir: %s", dn);
        free(dtmp);
        free(dn);
        IB_FTRACE_RET_STATUS(ib_rc);
    }

    // Create temporary filename to use while writing the audit log
    temp_filename_sz = strlen(audit_filename) + 6;
    temp_filename = (char *)ib_mpool_alloc(cfg->tx->mp, temp_filename_sz);
    sys_rc = snprintf(temp_filename,
                      temp_filename_sz,
                      "%s.part", audit_filename);
    if (sys_rc >= (int)temp_filename_sz) {
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Could not create temporary audit log filename: too long");
        free(dtmp);
        free(dn);
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    /// @todo Use corecfg->auditlog_fmode as file mode for new file
    cfg->fp = fopen(temp_filename, "ab");
    if (cfg->fp == NULL) {
        sys_rc = errno;
        /// @todo Better error.
        ib_log_error(log->ib,
                     "Could not open audit log \"%s\": %s (%d)",
                     temp_filename, strerror(sys_rc), sys_rc);
        free(dtmp);
        free(dn);
        IB_FTRACE_RET_STATUS(IB_EINVAL);
    }

    /* Track the relative audit log filename. */
    cfg->fn = audit_filename + (strlen(corecfg->auditlog_dir) + 1);
    cfg->full_path = audit_filename;
    cfg->temp_path = temp_filename;

    free(dtmp);
    free(dn);

    IB_FTRACE_RET_STATUS(IB_OK);
}