Beispiel #1
0
/* process raw auth request from node */
int eh_process_node_auth(int is_reply, void * data, int ent_id)
{
    logdebug(_("%s called\n"), __func__);

    int ret;
    AuthInfo * ai = data;
    AuthConfig * ac = ly_entity_auth(ent_id);

    if (is_reply) {
        loginfo(_("auth reply from node %d(tag)\n"), ai->tag);
        ret = lyauth_verify(ac, ai->data, LUOYUN_AUTH_DATA_LEN);
        if (ret < 0) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            return -1;
        }
        if (ret) {
            loginfo(_("node %d(tag) is authenticated\n"), ai->tag);
            if (!ly_entity_is_authenticated(ent_id))
                ly_entity_update(ent_id, -1, LY_ENTITY_FLAG_STATUS_AUTHENTICATED);
        }
        else {
            logwarn(_("chanllenge verification for node %d(tag) failed.\n"),
                      ai->tag);
            return 1;
        }
        return 0;
    }

    loginfo(_("auth request from node %d(tag)\n"), ai->tag);

    LYNodeData * nd = ly_entity_data(ent_id);
    if (nd == NULL) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }
    NodeInfo * nf = &nd->node;

    /* get secret */
    if (ac->secret == NULL) {
        logdebug(_("retrieve auth key for node %d(tag)\n"), ai->tag);
        ret = db_node_find_secret(DB_NODE_FIND_BY_ID,
                                  &ai->tag,
                                  &ac->secret);
        if (ret < 0) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            return -1;
        }
        else if (ret == 0) {
            logerror(_("node(tag: %d) not in db\n"), ai->tag);
            return -1;
        }
        nf->host_tag = ai->tag;
    }

    /* update node status */
    logdebug(_("update node status to %d\n"), NODE_STATUS_AUTHENTICATING);
    ret = db_node_update_status(DB_NODE_FIND_BY_ID, &ai->tag,
                                NODE_STATUS_AUTHENTICATING);
    if (ret < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    /* resolve challenge */
    logdebug(_("answer auth request\n"));
    ret = lyauth_answer(ac, ai->data, LUOYUN_AUTH_DATA_LEN);
    if (ret < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    /* send answer back */
    int fd = ly_entity_fd(ent_id);
    if (ly_packet_send(fd, PKT_TYPE_NODE_AUTH_REPLY,
                       ai, sizeof(AuthInfo)) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    /* request challenging */
    logdebug(_("clc sends out auth request\n"));
    if (lyauth_prepare(ac) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    bzero(ai->data, LUOYUN_AUTH_DATA_LEN);
    strncpy((char *)ai->data, ac->challenge, LUOYUN_AUTH_DATA_LEN);
    if (ly_packet_send(fd, PKT_TYPE_NODE_AUTH_REQUEST,
                       ai, sizeof(AuthInfo)) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }

    loginfo(_("node %d(tag) is online\n"), nf->host_tag);
    ly_entity_update(ent_id, nf->host_tag, LY_ENTITY_FLAG_STATUS_ONLINE);

    return 0;
}
Beispiel #2
0
/* process node register request */
static int __node_xml_register(xmlDoc * doc, xmlNode * node, int ent_id)
{
    if (ly_entity_is_registered(ent_id)) {
        logwarn(_("received node register request again, ignored\n"));
        return -1;
    }

    LYNodeData * nd = ly_entity_data(ent_id);
    if (nd == NULL ) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        return -1;
    }
    NodeInfo * nf = &nd->node;

    /* Create xpath evaluation context */
    xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
    if (xpathCtx == NULL) {
        logerror(_("unable to create new XPath context %s, %d\n"),
                 __func__, __LINE__);
        return -1;
    }
    int ret = -1;

    char *str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/status");
    if (str == NULL)
        goto xml_err;
    nf->status = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/hypervisor");
    if (str == NULL)
        goto xml_err;
    nf->hypervisor = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/tag");
    int tag = -1;
    if (str) {
        tag = atoi(str); /* NULL str is allowed for new node */
        free(str);
    }
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/name");
    if (str == NULL)
        goto xml_err;
    nf->host_name = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/host/ip");
    if (str == NULL)
        goto xml_err;
    nf->host_ip = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/arch");
    if (str == NULL)
        goto xml_err;
    nf->cpu_arch = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/model");
    if (str == NULL)
        goto xml_err;
    nf->cpu_model = str;
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/mhz");
    if (str == NULL)
        goto xml_err;
    nf->cpu_mhz = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/max");
    if (str == NULL)
        goto xml_err;
    nf->cpu_max = atoi(str);
    nf->cpu_vlimit = NODE_SCHEDULE_CPU_LIMIT(nf->cpu_max);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/cpu/commit");
    if (str == NULL)
        goto xml_err;
    nf->cpu_commit = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/total");
    if (str == NULL)
        goto xml_err;
    nf->mem_max = atoi(str);
    nf->mem_vlimit = NODE_SCHEDULE_MEM_LIMIT(nf->mem_max);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/free");
    if (str == NULL)
        goto xml_err;
    nf->mem_free = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/memory/commit");
    if (str == NULL)
        goto xml_err;
    nf->mem_commit = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/storage/total");
    if (str == NULL)
        goto xml_err;
    nf->storage_total = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                         "/" LYXML_ROOT "/request/parameters/storage/free");
    if (str == NULL)
        goto xml_err;
    nf->storage_free = atoi(str);
    free(str);
    str = xml_xpath_text_from_ctx(xpathCtx,
                          "/" LYXML_ROOT "/request/parameters/load/average");
    if (str == NULL)
        goto xml_err;
    nf->load_average = atoi(str);
    free(str);

    if (nf->status >= NODE_STATUS_REGISTERED) {
        logwarn(_("node(%d, %s) tries to register from wrong status(%d)\n"),
                 nf->host_tag, nf->host_ip, nf->status);
        ly_entity_release(ent_id);
        ret = 0; /* no need to continue node registration */
        goto done;
    }
        
    if (ly_entity_node_active(nf->host_ip)) {
        logwarn(_("duplicate node ip received. something is wrong...\n"));
        ly_entity_release(ent_id);
        ret = 0; /* no need to continue node registration */
        goto done;
    }
   
    if (tag > 0) {
        /* check authentication result */
        if (nf->host_tag > 0 && tag != nf->host_tag) {
            logerror(_("node tag changed, %d -> %d. something is wrong.\n"),
                       nf->host_tag, tag);
            goto done;
        }
        nf->host_tag = tag;
        ret = __node_register_auth(nf, ent_id);
        if (ret == LY_S_REGISTERING_DONE_SUCCESS) {
            AuthConfig * ac = ly_entity_auth(ent_id);
            if (db_node_update_secret(DB_NODE_FIND_BY_ID, &tag,
                                      ac->secret) < 0 ||
                db_node_update_status(DB_NODE_FIND_BY_ID, &tag,
                                      NODE_STATUS_REGISTERED) < 0) {
                logerror(_("error in %s(%d)\n"), __func__, __LINE__);
                ret = -1;
                goto done;
            }
            loginfo(_("node(tag:%d) registered\n"), tag);
            ly_entity_update(ent_id, tag, LY_ENTITY_FLAG_STATUS_REGISTERED);
        }
        goto done;
    }

    /* new node */
    DBNodeRegInfo db_nf;
    bzero(&db_nf, sizeof(DBNodeRegInfo));
    ret = db_node_find(DB_NODE_FIND_BY_IP, nf->host_ip, &db_nf);
    if (ret < 0 || ret > 1) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        ret = -1;
        goto done;
    }

    if (ret == 0 || db_nf.secret) {
        /* new node */
        logdebug(_("new node\n"));
        if (nf->status != NODE_STATUS_UNINITIALIZED) {
            logwarn(_("node(%d, %s) tries to register from unexpected status(%d)\n"),
                       nf->host_tag, nf->host_ip, nf->status);
            nf->status = NODE_STATUS_UNINITIALIZED;
        }
        if (db_nf.secret) {
            logwarn(_("new node takes ip(%s) used by tagged node\n"), nf->host_ip);
            db_node_reginfo_free(&db_nf);
            bzero(&db_nf, sizeof(DBNodeRegInfo));
        }
            
        ret = db_node_insert(nf);
        if (ret < 0) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        db_nf.id = ret;
        loginfo(_("new node %s added in db(%d)\n"), nf->host_ip, ret);

        /* enable node if node is control server */
        if (ly_is_clc_ip(nf->host_ip)) {
            if (db_node_enable(ret, 1) != 0 ||
                db_node_find(DB_NODE_FIND_BY_ID, &ret, &db_nf) != 1) {
                logerror(_("error in %s(%d)\n"), __func__, __LINE__);
                ret = -1;
                goto new_done;
            }
        }
    }
    else
        logdebug(_("untagged node for ip(%s) found in db\n"), nf->host_ip);

    ly_entity_update(ent_id, db_nf.id, LY_ENTITY_FLAG_STATUS_ONLINE);
    if (db_nf.enabled) {
        AuthConfig * ac = ly_entity_auth(ent_id);
        ret = -1;
        if (ac->secret) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        ac->secret = lyauth_secret();
        if (ac->secret == NULL) {
            logerror(_("error in %s(%d)\n"), __func__, __LINE__);
            goto new_done;
        }
        nf->host_tag = db_nf.id;
        ret = LY_S_REGISTERING_CONFIG;
    }
    else {
        logdebug(_("register request done, node %s not enabled\n"), nf->host_ip);
        ret = LY_S_REGISTERING_INIT;
    }
    nf->status = NODE_STATUS_ONLINE;
    if (db_node_update(DB_NODE_FIND_BY_ID, &db_nf.id, nf) < 0) {
        logerror(_("error in %s(%d)\n"), __func__, __LINE__);
        ret = -1;
    }

new_done:
    db_node_reginfo_free(&db_nf);
    goto done;
xml_err:
    logerror(_("invalid node xml register request\n"));
done:
    xmlXPathFreeContext(xpathCtx);
    logdebug(_("end of %s, node status %d\n"), __func__, nf->status);
    return ret;
}
Beispiel #3
0
int ly_epoll_entity_recv(int ent_id)
{
    if (ly_entity_type(ent_id) == LY_ENTITY_CLC)
        return __epoll_work_recv(ent_id);

    int fd = ly_entity_fd(ent_id);
    if (fd < 0) {
        logerror(_("fd for entity %d was closed. ignore event.\n"), ent_id);
        return 1;
    }

    LYPacketRecv *pkt = ly_entity_pkt(ent_id);
    if (pkt == NULL)
        return -255;

    int size;
    void * buf = ly_packet_buf(pkt, &size);
    if (buf == NULL) {
        logerror(_("ly_packet_buf returns NULL buffer. close socket\n"));
        return 1;
    }
    if (size == 0) {
        logerror(_("ly_packet_buf returns 0 size buffer. close socket\n"));
        return 1;
    }

    int len = recv(fd, buf, size, 0);
    if (len <= 0) {
        loginfo(_("socket %d recv returns %d. close socket\n"), fd, len);
        if (len < 0)
            loginfo(_("socket %d recv, errno %d\n"), fd, errno);

        int type = ly_entity_type(ent_id);
        int db_id = ly_entity_db_id(ent_id);
        if (type == LY_ENTITY_NODE) {
            logdebug(_("update node %d status in db to offline\n"), db_id);
            db_node_update_status(DB_NODE_FIND_BY_ID, &db_id, NODE_STATUS_OFFLINE);
            return 1;
        }

        if (ly_entity_type(ent_id) != LY_ENTITY_OSM)
            return 1;

        logdebug(_("update instance %d status in db\n"), db_id);
        InstanceInfo ii;
        ii.ip = NULL;
        ii.status = DOMAIN_S_NEED_QUERY;
        db_instance_update_status(db_id, &ii, -1);
        job_internal_query_instance(db_id);
        return 1;
    }
    logdebug(_("socket %d recv %d bytes\n"), fd, len);

    while(1) {
        int ret = ly_packet_recv(pkt, len);
        if (ret < 0) {
            logerror(_("package recv error in %s\n"), __func__);
            __print_recv_buf(buf, len);
            break;
        }

        /* currenly we only support processing a complete packet */
        if (ret == 0) {
            if (pkt->pkt_buf_received > 0) {
                loginfo(_("socket %d recv partial packet(len %d)\n"),
                           fd, pkt->pkt_buf_received);
                __print_recv_buf(buf, len);
            }
            break;
        }

        int type = ly_packet_type(pkt);
        loginfo(_("socket %d recv packet, type %d\n"), fd, type);
        /*
        if (type == PKT_TYPE_UNKNOW)
            break;
        */

        buf = ly_packet_data(pkt, &size);
        if (type == PKT_TYPE_WEB_NEW_JOB_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_WEB);
            ret = __process_web_job(buf, size, ent_id);
            if (ret < 0)
                logerror(_("web packet process error in %s.\n"), __func__);
        }
	else if (type == PKT_TYPE_NODE_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_NODE_AUTH_REQUEST ||
                 type == PKT_TYPE_NODE_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_NODE);
            ret = eh_process_node_auth(type == PKT_TYPE_NODE_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("node auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_AUTH_REQUEST ||
                 type == PKT_TYPE_OSM_AUTH_REPLY) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_auth(type == PKT_TYPE_OSM_AUTH_REPLY ?
                                       1 : 0, buf, ent_id);
            if (ret < 0)
                logerror(_("osm auth packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_CLC_OSM_QUERY_REPLY) {
            ret = eh_process_osm_query(ly_packet_data(pkt, NULL));
            if (ret < 0)
                logerror(_("osm packet process error in %s\n"), __func__);
        }
	else if (PKT_TYPE_ENTITY_GROUP_CLC(type) ||
                 PKT_TYPE_ENTITY_GROUP_NODE(type)) {
            ret = eh_process_node_xml(buf, ent_id);
            if (ret < 0)
                logerror(_("node packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REGISTER_REQUEST) {
            ly_entity_init(ent_id, LY_ENTITY_OSM);
            ret = eh_process_osm_register(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_OSM_REPORT) {
            ret = eh_process_osm_report(buf, size, ent_id);
            if (ret < 0)
                logerror(_("osm packet process error in %s.\n"), __func__);
        }
        else if (type == PKT_TYPE_TEST_ECHO_REQUEST) {
            ret = __process_test_echo(buf, size, ent_id);
            if (ret < 0)
                logerror(_("echo packet process error in %s.\n"), __func__);
        }
        else {
            logerror(_("unrecognized packet type.\n"));
        }

        if (ly_packet_recv_done(pkt) < 0 || ret < 0) {
            logerror(_("%s return error\n"), __func__);
            return -1;
        }

        if (ret > 0)
            return ret;

        len = 0; /* continue processing data in buffer */
    }

    return 0;
}