Exemple #1
0
int Handler_load(tst_t *settings, tst_t *params)
{
    const char *send_spec = AST_str(settings, params, "send_spec", VAL_QSTRING);
    char *sql = NULL;
    
    sql = sqlite3_mprintf(bdata(&HANDLER_SQL),
            send_spec,
            AST_str(settings, params, "send_ident", VAL_QSTRING),
            AST_str(settings, params, "recv_spec", VAL_QSTRING),
            AST_str(settings, params, "recv_ident", VAL_QSTRING));

    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to load Handler: %s", send_spec);

    if(tst_search(params, bdata(&RAW_PAYLOAD), blength(&RAW_PAYLOAD))) {
        const char *raw_payload = AST_str(settings, params, "raw_payload", VAL_NUMBER);
        if(raw_payload && raw_payload[0] == '1') {
            DB_exec(bdata(&HANDLER_RAW_SQL), NULL, NULL);
        }
    }

    sqlite3_free(sql);
    return DB_lastid();

error:

    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #2
0
static inline int Config_setup(const char *db_file)
{
    DB_init(db_file);
    int rc = DB_exec("begin", NULL, NULL);
    check(rc == 0, "Couldn't start transaction.");

    rc = DB_exec(bdata(&CONFIG_SCHEMA), NULL, NULL);
    check(rc == 0, "Failed to load initial schema.");

    return 0;

error:
    return -1;
}
Exemple #3
0
int Host_load(tst_t *settings, Value *val)
{
    CONFIRM_TYPE("Host");
    Class *cls = val->as.cls;
    char *sql = NULL;
    struct tagbstring ROUTES_VAR = bsStatic("routes");

    const char *name = AST_str(settings, cls->params, "name", VAL_QSTRING);
    check(name, "No name set for Host.");

    sql = sqlite3_mprintf(bdata(&HOST_SQL), SERVER_ID, name, name);
    
    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to store Host: %s", name);

    cls->id = HOST_ID = DB_lastid();

    Value *routes = AST_get(settings, cls->params, &ROUTES_VAR, VAL_HASH);
    check(routes, "Didn't find any routes for %s", name);

    AST_walk_hash(settings, routes, Route_load);

    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #4
0
tns_value_t *default_load_routes(int host_id, int server_id)
{
    const char *ROUTE_QUERY = "SELECT route.id, route.path, route.target_id, route.target_type "
        "FROM route, host WHERE host_id=%d AND "
        "host.server_id=%d AND host.id = route.host_id";

    return DB_exec(ROUTE_QUERY, host_id, server_id);
}
Exemple #5
0
tns_value_t *default_load_server(const char *uuid)
{
    const char *SERVER_QUERY = "SELECT id, uuid, default_host, bind_addr, port, chroot, access_log, error_log, pid_file, use_ssl FROM server WHERE uuid=%Q";

    tns_value_t *ret = DB_exec(SERVER_QUERY, uuid);
    if(ret == NULL || tns_get_type(ret) != tns_tag_list || ret->value.list->end < 1)
        return ret;

    tns_value_t *control_port;
    tns_value_t *ret2 = DB_exec("SELECT control_port FROM server WHERE uuid=%Q", uuid);
    if(ret2 != NULL && tns_get_type(ret2) == tns_tag_list && ret2->value.list->end > 0) {
        tns_value_t *row = (tns_value_t *)darray_first(ret2->value.list);
        control_port = (tns_value_t *)darray_first(row->value.list);
        darray_remove(row->value.list, 0);
        tns_value_destroy(ret2);
    } else {
        control_port = tns_parse_string("", 0);
    }

    tns_insert_to_list((tns_value_t *)darray_first(ret->value.list), 9, control_port);

    return ret;
}
Exemple #6
0
int Server_load(tst_t *settings, Value *val)
{
    CONFIRM_TYPE("Server");
    Class *cls = val->as.cls;
    int rc = 0;
    char *sql = NULL;
    struct tagbstring HOSTS_VAR = bsStatic("hosts");
    const char *bind_addr = NULL;

    if(tst_search(cls->params, bdata(&BIND_ADDR), blength(&BIND_ADDR))) {
        bind_addr = AST_str(settings, cls->params, "bind_addr", VAL_QSTRING);
    } else {
        bind_addr = "0.0.0.0";
    }

    sql = sqlite3_mprintf(bdata(&SERVER_SQL),
            AST_str(settings, cls->params, "uuid", VAL_QSTRING),
            AST_str(settings, cls->params, "access_log", VAL_QSTRING),
            AST_str(settings, cls->params, "error_log", VAL_QSTRING),
            AST_str(settings, cls->params, "pid_file", VAL_QSTRING),
            AST_str(settings, cls->params, "chroot", VAL_QSTRING),
            AST_str(settings, cls->params, "default_host", VAL_QSTRING),
            AST_str(settings, cls->params, "name", VAL_QSTRING),
            bind_addr,
            AST_str(settings, cls->params, "port", VAL_NUMBER));

    rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to exec SQL: %s", sql);

    cls->id = SERVER_ID = DB_lastid();

    Value *hosts = AST_get(settings, cls->params, &HOSTS_VAR, VAL_LIST);
    check(hosts != NULL, "Could not find Server.hosts setting in host %s:%s", 
            AST_str(settings, cls->params, "uuid", VAL_QSTRING),
            AST_str(settings, cls->params, "name", VAL_QSTRING));

    AST_walk_list(settings, hosts->as.list, Host_load);


    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #7
0
int log_action(bstring db_file, bstring what, bstring why, bstring where, bstring how)
{
    int rc = 0;
    tns_value_t *res = NULL;
    bstring who = NULL;
    char *user = getlogin() == NULL ? getenv("LOGNAME") : getlogin();
    check(user != NULL, "getlogin failed and no LOGNAME env variable, how'd you do that?");
    who = bfromcstr(user);

    if(access((const char *)db_file->data, R_OK | W_OK) != 0) {
        // don't log if there's no file available
        return 0;
    }
 
    rc = DB_init(bdata(db_file));
    check(rc == 0, "Failed to open db: %s", bdata(db_file));

    if(!where) {
        char name_buf[128] = {0};
        int rc = gethostname(name_buf, 127);
        check(rc == 0, "Failed to get your machines hostname, use -where to force it.");
        where = bfromcstr(name_buf);
    }

    res = DB_exec("INSERT INTO log (who, what, location, how, why) VALUES (%Q, %Q, %Q, %Q, %Q)",
            bdata(who), bdata(what), bdata(where), bdata(how), bdata(why));
    check(res != NULL, "Failed to add log message, you're flying blind.");
    tns_value_destroy(res);

    if(biseqcstr(who, "root")) {
        log_warn("You shouldn't be running things as %s.  Use a safe user instead.", bdata(who));
    }

    bdestroy(who);
    bdestroy(where);
    DB_close();
    return 0;

error:
    if(who) bdestroy(who);
    if(where) bdestroy(where);
    if(res) tns_value_destroy(res);
    DB_close();
    return -1;
}
Exemple #8
0
int Route_load(tst_t *settings, Pair *pair)
{
    const char *name = bdata(Pair_key(pair));
    char *sql = NULL;
    Value *val = Pair_value(pair);
    bstring type = NULL;
    int rc = 0;

    check(val, "Error loading route: %s", bdata(Pair_key(pair)));
    check(Value_is(val, CLASS), "Expected a Class but got a %s instead.",
            Value_type_name(val->type));
    Class *cls = val->as.cls;
    type = bstrcpy(Class_ident(cls));
    btolower(type);

    if(cls->id == -1) {
        if(biseqcstr(type, "dir")) {
            rc = Dir_load(settings, cls->params);
        } else if(biseqcstr(type, "proxy")) {
            rc = Proxy_load(settings, cls->params);
        } else if(biseqcstr(type, "handler")) {
            rc = Handler_load(settings, cls->params);
        } else {
            sentinel("Invalid type of route target: %s", bdata(Class_ident(cls)));
        }

        check(rc != -1, "Failed to create target for route %s", name);
        cls->id = rc;
    }

    sql = sqlite3_mprintf(bdata(&ROUTE_SQL), name, HOST_ID, cls->id, bdata(type));

    rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to intialize route.");

    sqlite3_free(sql);
    bdestroy(type);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    bdestroy(type);
    return -1;
}
Exemple #9
0
int Proxy_load(tst_t *settings, tst_t *params)
{
    const char *addr = AST_str(settings, params, "addr", VAL_QSTRING);
    const char *port = AST_str(settings, params, "port", VAL_NUMBER);

    char *sql = NULL;
    
    sql = sqlite3_mprintf(bdata(&PROXY_SQL),
            addr, port);

    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to load Proxy: %s:%s", addr, port);
    
    sqlite3_free(sql);
    return DB_lastid();

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #10
0
int Dir_load(tst_t *settings, tst_t *params)
{
    const char *base = AST_str(settings, params, "base", VAL_QSTRING);

    char *sql = NULL;
    
    sql = sqlite3_mprintf(bdata(&DIR_SQL),
            base,
            AST_str(settings, params, "index_file", VAL_QSTRING),
            AST_str(settings, params, "default_ctype", VAL_QSTRING));

    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to load Dir: %s", base);

    sqlite3_free(sql);
    return DB_lastid();

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #11
0
int Settings_load(tst_t *settings, Pair *pair)
{
    const char *name = bdata(Pair_key(pair));
    Value *val = Pair_value(pair);
    check(val, "Error loading Setting %s", bdata(Pair_key(pair)));

    char *sql = NULL;
    
    sql = sqlite3_mprintf(bdata(&SETTING_SQL),
            name, bdata(val->as.string->data));

    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to add setting: %s=%s",
            name, bdata(val->as.string->data));

    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #12
0
int Mimetypes_load(tst_t *settings, Pair *pair)
{
    const char *ext = bdata(Pair_key(pair));
    Value *val = Pair_value(pair);
    check(val, "Error loading Mimetype %s", bdata(Pair_key(pair)));

    char *sql = NULL;
    
    sql = sqlite3_mprintf(bdata(&MIMETYPE_SQL),
            ext, ext, bdata(val->as.string->data));

    int rc = DB_exec(sql, NULL, NULL);
    check(rc == 0, "Failed to add mimetype: %s=%s", 
            ext, bdata(val->as.string->data));

    sqlite3_free(sql);
    return 0;

error:
    if(sql) sqlite3_free(sql);
    return -1;
}
Exemple #13
0
int Mimetypes_import()
{
    return DB_exec(bdata(&MIMETYPES_DEFAULT_SQL), NULL, NULL);
}
Exemple #14
0
tns_value_t *default_load_mimetypes()
{
    const char *MIME_QUERY = "SELECT id, extension, mimetype FROM mimetype";
    return DB_exec(MIME_QUERY);
}
Exemple #15
0
tns_value_t *default_load_handler(int handler_id)
{
    const char *HANDLER_QUERY = "SELECT id, send_spec, send_ident, recv_spec, recv_ident, raw_payload, protocol FROM handler WHERE id=%d";
    return DB_exec(HANDLER_QUERY, handler_id);
}
Exemple #16
0
tns_value_t *default_load_xrequests(int server_id)
{
    const char *FILTER_QUERY = "SELECT id, name, settings FROM xrequest WHERE server_id = %d";
    return DB_exec(FILTER_QUERY, server_id);
}
Exemple #17
0
static inline int Config_commit()
{
    return DB_exec("commit", NULL, NULL);
}
Exemple #18
0
tns_value_t *default_load_proxy(int proxy_id)
{
    const char *PROXY_QUERY = "SELECT id, addr, port FROM proxy WHERE id=%d";
    return DB_exec(PROXY_QUERY, proxy_id);
}
Exemple #19
0
tns_value_t *default_load_dir(int dir_id)
{
    const char *DIR_QUERY = "SELECT id, base, index_file, default_ctype, cache_ttl FROM directory WHERE id=%d";
    return DB_exec(DIR_QUERY, dir_id);
}
Exemple #20
0
tns_value_t *default_load_hosts(int server_id)
{
    const char *HOST_QUERY = "SELECT id, name, matching, server_id FROM host WHERE server_id = %d";
    return DB_exec(HOST_QUERY, server_id);
}
Exemple #21
0
tns_value_t *default_load_settings()
{
    const char *SETTINGS_QUERY = "SELECT id, key, value FROM setting";
    return DB_exec(SETTINGS_QUERY);
}