END_TEST

// mp_realloc
START_TEST (test_realloc_ok) {
    char *dest = NULL;

    dest = mp_realloc(dest, 10);

    dest = mp_realloc(dest, 20);

}
Exemple #2
0
static void
mpi_addsub(mpi *rop, mpi *op1, mpi *op2, int sub)
{
    long xlen;				/* maximum result size */

    if (sub ^ (op1->sign == op2->sign)) {
	/* plus one for possible carry */
	xlen = MAX(op1->size, op2->size) + 1;
	if (rop->alloc < xlen) {
	    rop->digs = mp_realloc(rop->digs, sizeof(BNS) * xlen);
	    rop->alloc = xlen;
	}
	rop->size = mp_add(rop->digs, op1->digs, op2->digs,
			   op1->size, op2->size);
	rop->sign = op1->sign;
    }
    else {
	long cmp;			/* check for larger operator */

	cmp = mpi_cmpabs(op1, op2);
	if (cmp == 0) {
	    rop->digs[0] = 0;
	    rop->size = 1;
	    rop->sign = 0;
	}
	else {
	    xlen = MAX(op1->size, op2->size);
	    if (rop->alloc < xlen) {
		rop->digs = mp_realloc(rop->digs, sizeof(BNS) * xlen);
		rop->alloc = xlen;
	    }
	    if (cmp > 0) {
		rop->size = mp_sub(rop->digs, op1->digs, op2->digs,
				   op1->size, op2->size);
		rop->sign = op1->sign;
	    }
	    else {
		rop->size = mp_sub(rop->digs, op2->digs, op1->digs,
				   op2->size, op1->size);
		rop->sign = sub ^ op2->sign;
	    }
	}
    }
}
Exemple #3
0
void mp_strcat(char **target, char *source) {
    if(source == NULL) {
        return;
    } else if(*target == NULL) {
        *target = strdup(source);
    } else {
        *target = mp_realloc(*target, strlen(*target) + strlen(source) + 1);
        strcat(*target, source);
    }
}
Exemple #4
0
void
mpi_setd(mpi *rop, double d)
{
    long i;
    double mantissa;
    int shift, exponent;
    BNI size;

    if (isnan(d))
	d = 0.0;
    else if (!finite(d))
	d = copysign(1.0, d) * DBL_MAX;

    /* check if number is larger than 1 */
    if (fabs(d) < 1.0) {
	rop->digs[0] = 0;
	rop->size = 1;
	rop->sign = d < 0.0;

	return;
    }

    mantissa = frexp(d, &exponent);
    if (mantissa < 0)
	mantissa = -mantissa;

    size = (exponent + (BNSBITS - 1)) / BNSBITS;
    shift = BNSBITS - (exponent & (BNSBITS - 1));

    /* adjust amount of memory */
    if (rop->alloc < size) {
	rop->digs = mp_realloc(rop->digs, sizeof(BNS) * size);
	rop->alloc = size;
    }
    rop->size = size;

    /* adjust the exponent */
    if (shift < BNSBITS)
	mantissa = ldexp(mantissa, -shift);

    /* convert double */
    for (i = size - 1; i >= 0 && mantissa != 0.0; i--) {
	mantissa = ldexp(mantissa, BNSBITS);
	rop->digs[i] = (BNS)mantissa;
	mantissa -= rop->digs[i];
    }
    for (; i >= 0; i--)
	rop->digs[i] = 0;

    /* normalize */
    if (size > 1 && rop->digs[size - 1] == 0)
	--rop->size;

    rop->sign = d < 0.0;
}
Exemple #5
0
bonding_info *parseBond(const char *filename) {
    FILE *input;
    char buffer[256];
    char *key, *value;
    int count = 0;

    bonding_info *info;

    input = fopen(filename, "r");
    if (input == NULL)
        return NULL;

    info = mp_malloc(sizeof(bonding_info));
    info->slave = NULL;

    while (fgets(buffer, 256, input) != NULL) {

        value = buffer;
        key = strsep(&value, ":");
        if(!value)
            continue;

        value++;
        value[strlen(value)-1] = '\0';

        if (strcmp(key, "Ethernet Channel Bonding Driver") == 0) {
            info->version = strdup(value);
        } else if (strcmp(key, "Bonding Mode") == 0) {
            info->mode = strdup(value);
        } else if (strcmp(key, "MII Status") == 0) {
            if (strcmp(value, "up") == 0) {
                if(count)
                    info->slave[count-1]->mii_status = 1;
                else
                    info->mii_status = 1;
            } else {
                if(count)
                    info->slave[count-1]->mii_status = 0;
                else
                    info->mii_status = 0;
            }
        } else if (strcmp(key, "Slave Interface") == 0) {
            count++;
            info->slaves = count;
            info->slave = mp_realloc(info->slave, (count+1)*sizeof(bonding_slave_info *));
            info->slave[count] = NULL;
            info->slave[count-1] = mp_malloc(sizeof(struct bonding_slave_info_s));
            info->slave[count-1]->interface = strdup(value);
        }
    }
    fclose(input);

    return info;
}
void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
    if (buf->free < len) {
        size_t newlen = buf->len+len;

        buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len, newlen*2);
        buf->free = newlen;
    }
    memcpy(buf->b+buf->len,s,len);
    buf->len += len;
    buf->free -= len;
}
mp_buf *mp_buf_new(lua_State *L) {
    mp_buf *buf = NULL;

    /* Old size = 0; new size = sizeof(*buf) */
    buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));

    buf->L = L;
    buf->b = NULL;
    buf->len = buf->free = 0;
    return buf;
}
Exemple #8
0
void
mpi_set(mpi *rop, mpi *op)
{
    if (rop != op) {
	if (rop->alloc < op->size) {
	    rop->digs = mp_realloc(rop->digs, sizeof(BNS) * op->size);
	    rop->alloc = op->size;
	}
	rop->size = op->size;
	memcpy(rop->digs, op->digs, sizeof(BNS) * op->size);
	rop->sign = op->sign;
    }
}
Exemple #9
0
void mp_dhcp_pkt_opt(struct dhcp_pkt *pkt, uint8_t code, uint8_t len, char *data) {
    if(pkt->optlen == 0) {
        pkt->opts = mp_malloc(len + 3);
        pkt->optlen = 1;
    } else {
        pkt->opts = mp_realloc(pkt->opts, pkt->optlen + len + 2);
    }

    pkt->opts[(pkt->optlen)-1] = code;

    if (len) {
        pkt->opts[pkt->optlen] = len;
        memcpy(&pkt->opts[pkt->optlen+1], data, len);
        pkt->optlen += 2 + len;
    }

    pkt->opts[(pkt->optlen)-1] = DHCPOPT_End;
}
Exemple #10
0
void
mpi_seti(mpi *rop, long si)
{
    unsigned long ui;
    int sign = si < 0;
    int size;

    if (si == MINSLONG) {
	ui = MINSLONG;
	size = 2;
    }
    else {
	if (sign)
	    ui = -si;
	else
	    ui = si;
	if (ui < CARRY)
	    size = 1;
	else
	    size = 2;
    }

    if (rop->alloc < size) {
	rop->digs = mp_realloc(rop->digs, sizeof(BNS) * size);
	rop->alloc = size;
    }
    rop->size = size;

    /* store data in small mp integer */
    rop->digs[0] = (BNS)ui;
    if (size > 1)
	rop->digs[1] = (BNS)(ui >> BNSBITS);
    rop->size = size;

    /* adjust result sign */
    rop->sign = sign;
}
Exemple #11
0
void
mpi_mul(mpi *rop, mpi *op1, mpi *op2)
{
    int sign;				/* sign flag */
    BNS *digs;				/* result data */
    long xsize;				/* result size */

    /* get result sign */
    sign = op1->sign ^ op2->sign;

    /* check for special cases */
    if (op1->size == 1) {
	if (*op1->digs == 0) {
	    /* multiply by 0 */
	    mpi_seti(rop, 0);
	    return;
	}
	else if (*op1->digs == 1) {
	    /* multiply by +-1 */
	    if (rop->alloc < op2->size) {
		rop->digs = mp_realloc(rop->digs, sizeof(BNS) * op2->size);
		rop->alloc = op2->size;
	    }
	    rop->size = op2->size;
	    memmove(rop->digs, op2->digs, sizeof(BNS) * op2->size);
	    rop->sign = op2->size > 1 || *op2->digs ? sign : 0;

	    return;
	}
    }
    else if (op2->size == 1) {
	if (*op2->digs == 0) {
	    /* multiply by 0 */
	    mpi_seti(rop, 0);
	    return;
	}
	else if (*op2->digs == 1) {
	    /* multiply by +-1 */
	    if (rop->alloc < op1->size) {
		rop->digs = mp_realloc(rop->digs, sizeof(BNS) * op1->size);
		rop->alloc = op1->size;
	    }
	    rop->size = op1->size;
	    memmove(rop->digs, op1->digs, sizeof(BNS) * op1->size);
	    rop->sign = op1->size > 1 || *op1->digs ? sign : 0;

	    return;
	}
    }

    /* allocate result data and set it to zero */
    xsize = op1->size + op2->size;
    if (rop->digs == op1->digs || rop->digs == op2->digs)
	/* rop is also an operand */
	digs = mp_calloc(1, sizeof(BNS) * xsize);
    else {
	if (rop->alloc < xsize) {
	    rop->digs = mp_realloc(rop->digs, sizeof(BNS) * xsize);
	    rop->alloc = xsize;
	}
	digs = rop->digs;
	memset(digs, '\0', sizeof(BNS) * xsize);
    }

    /* multiply operands */
    xsize = mp_mul(digs, op1->digs, op2->digs, op1->size, op2->size);

    /* store result in rop */
    if (digs != rop->digs) {
	/* if rop was an operand, free old data */
	mp_free(rop->digs);
	rop->digs = digs;
    }
    rop->size = xsize;

    /* set result sign */
    rop->sign = sign;
}
int main (int argc, char **argv) {
    /* Local Vars */
    CURL                *curl;
    char                *url;
    int                 i, j;
    char                *buf;
    struct mp_curl_data answer;
    long int            code;
    struct json_object  *obj;
    struct json_object  *slaveobj;
    unsigned int        slave_connected;
    char                *slave_host;
    char                *slave_version;
    char                *connected = NULL;
    char                *failed = NULL;

    /* Set signal handling and alarm */
    if (signal(SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Build query */
    url = mp_malloc(128);
    if (slaves) {
        mp_snprintf(url, 127, "http://%s:%d/json/slaves?select=%s", hostname, port, slave[0]);
        for(i=1; i<slaves; i++) {
            url = mp_realloc(url, strlen(url) + strlen(slave[i]) + 8 );
            strcat(url, "&select=");
            strcat(url, slave[i]);
        }
    } else {
        mp_snprintf(url, 127, "http://%s:%d/json/slaves", hostname, port);
    }

    if (mp_verbose > 0) {
        printf("CURL Version: %s\n", curl_version());
        printf("Url: %s\n", url);
    }

    /* Init libcurl */
    curl = mp_curl_init();
    answer.data = NULL;
    answer.size = 0;

    /* Setup request */
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mp_curl_recv_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&answer);

    /* Get url */
    code = mp_curl_perform(curl);

    /* Cleanup libcurl */
    curl_easy_cleanup(curl);
    curl_global_cleanup();
    free(url);

    if (code != 200) {
        critical("Buildbot - HTTP Status %ld.", code);
    }

    if (mp_verbose > 1) {
        printf("Answer: '%s'\n", answer.data);
    }

    /* Parse Answer */
    buf = mp_malloc(128);

    obj = json_tokener_parse(answer.data);

    if (slaves) {
        for(i=0; i<slaves; i++) {
            slaveobj = json_object_object_get(obj, slave[i]);
            if(json_object_object_get(slaveobj,"error")) {
                mp_snprintf(buf, 128, "%s - %s", slave[i], json_object_get_string(json_object_object_get(slaveobj,"error")));
                mp_strcat_comma(&failed, buf);
                continue;
            }
            slave_connected = json_object_get_boolean(json_object_object_get(slaveobj, (const char *)"connected"));
            slave_host = (char *)json_object_get_string(json_object_object_get(slaveobj, "host"));
            slave_version = (char *)json_object_get_string(json_object_object_get(slaveobj, "version"));

            for (j = strlen(slave_host) -1; isspace(slave_host[j]); j--) {
                slave_host[j] = '\0';
            }

            mp_snprintf(buf, 128, "%s - %s (v%s)", slave[i], slave_host, slave_version);

            if (slave_connected) {
                mp_strcat_comma(&connected, buf);
            } else {
                mp_strcat_comma(&failed, buf);
            }
        }
    } else {
        json_object_object_foreach(obj, key, val) {
            slaveobj = val;
            slave_connected = json_object_get_boolean(json_object_object_get(slaveobj, "connected"));
            slave_host = (char *)json_object_get_string(json_object_object_get(slaveobj, "host"));
            slave_version = (char *)json_object_get_string(json_object_object_get(slaveobj, "version"));

            for (j = strlen(slave_host) -1; isspace(slave_host[j]); j--) {
                slave_host[j] = '\0';
            }

            mp_snprintf(buf, 128, "%s - %s (v%s)", key, slave_host, slave_version);

            if (slave_connected) {
                mp_strcat_comma(&connected, buf);
            } else {
                mp_strcat_comma(&failed, buf);
            }

        }
    }
Exemple #13
0
/*
 * Could/should be changed to not allocate qdigs if qrop is NULL
 * Performance wouldn't suffer too much with a test on every loop iteration.
 */
void
mpi_divqr(mpi *qrop, mpi *rrop, mpi *num, mpi *den)
{
    long i, j;			/* counters */
    int qsign;			/* sign of quotient */
    int rsign;			/* sign of remainder */
    BNI qsize;			/* size of quotient */
    BNI rsize;			/* size of remainder */
    BNS qest;			/* estimative of quotient value */
    BNS *qdigs, *rdigs;		/* work copy or result */
    BNS *ndigs, *ddigs;		/* work copy or divisor and dividend */
    BNI value;			/* temporary result */
    long svalue;		/* signed temporary result (2's complement) */
    BNS carry, scarry, denorm;	/* carry and normalization */
    BNI dpos, npos;		/* offsets in data */

    /* get signs */
    rsign = num->sign;
    qsign = rsign ^ den->sign;

    /* check for special case */
    if (num->size < den->size) {
	/* quotient is zero and remainder is numerator */
	if (rrop && rrop->digs != num->digs) {
	    if (rrop->alloc < num->size) {
		rrop->digs = mp_realloc(rrop->digs, sizeof(BNS) * num->size);
		rrop->alloc = num->size;
	    }
	    rrop->size = num->size;
	    memcpy(rrop->digs, num->digs, sizeof(BNS) * num->size);
	    rrop->sign = rsign;
	}
	if (qrop)
	    mpi_seti(qrop, 0);

	return;
    }

    /* estimate result sizes */
    rsize = den->size;
    qsize = num->size - den->size + 1;

    /* offsets */
    npos = num->size - 1;
    dpos = den->size - 1;

    /* allocate space for quotient and remainder */
    if (qrop == NULL || qrop->digs == num->digs || qrop->digs == den->digs)
	qdigs = mp_calloc(1, sizeof(BNS) * qsize);
    else {
	if (qrop->alloc < qsize) {
	    qrop->digs = mp_realloc(qrop->digs, sizeof(BNS) * qsize);
	    qrop->alloc = qsize;
	}
	memset(qrop->digs, '\0', sizeof(BNS) * qsize);
	qdigs = qrop->digs;
    }
    if (rrop) {
	if (rrop->digs == num->digs || rrop->digs == den->digs)
	    rdigs = mp_calloc(1, sizeof(BNS) * rsize);
	else {
	    if (rrop->alloc < rsize) {
		rrop->digs = mp_realloc(rrop->digs, sizeof(BNS) * rsize);
		rrop->alloc = rsize;
	    }
	    memset(rrop->digs, '\0', sizeof(BNS) * rsize);
	    rdigs = rrop->digs;
	}
    }
    else
	rdigs = NULL;	/* fix gcc warning */

    /* special case, only one word in divisor */
    if (dpos == 0) {
	for (carry = 0, i = npos; i >= 0; i--) {
	    value = ((BNI)carry << BNSBITS) + num->digs[i];
	    qdigs[i] = (BNS)(value / den->digs[0]);
	    carry = (BNS)(value % den->digs[0]);
	}
	if (rrop)
	    rdigs[0] = carry;

	goto mpi_divqr_done;
    }

    /* make work copy of numerator */
    ndigs = mp_malloc(sizeof(BNS) * (num->size + 1));
    /* allocate one extra word an update offset */
    memcpy(ndigs, num->digs, sizeof(BNS) * num->size);
    ndigs[num->size] = 0;
    ++npos;

    /* normalize */
    denorm = (BNS)((BNI)CARRY / ((BNI)(den->digs[dpos]) + 1));

    if (denorm > 1) {
	/* i <= num->size because ndigs has an extra word */
	for (carry = 0, i = 0; i <= num->size; i++) {
	    value = ndigs[i] * (BNI)denorm + carry;
	    ndigs[i] = (BNS)value;
	    carry = (BNS)(value >> BNSBITS);
	}
	/* make work copy of denominator */
	ddigs = mp_malloc(sizeof(BNS) * den->size);
	memcpy(ddigs, den->digs, sizeof(BNS) * den->size);
	for (carry = 0, i = 0; i < den->size; i++) {
	    value = ddigs[i] * (BNI)denorm + carry;
	    ddigs[i] = (BNS)value;
	    carry = (BNS)(value >> BNSBITS);
	}
    }
    else
Exemple #14
0
void rhcs_clustat_startElement(void *clustat, const char *name, const char **atts) {
    const char **k, **v;
    int i;

    if (strcmp(name, "cluster") == 0) {
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_clustat *)clustat)->name = mp_strdup(*v);
            else if (strcmp(*k, "id") == 0)
                ((rhcs_clustat *)clustat)->id = (unsigned int) strtol(*v, NULL, 10);
        }
        return;
    }

    if (strcmp(name, "node") == 0) {
        ((rhcs_clustat *)clustat)->node = mp_realloc(((rhcs_conf *)clustat)->node, (nodes+2)*sizeof(rhcs_clustat_node));
        ((rhcs_clustat *)clustat)->node[nodes] = mp_calloc(1, sizeof(rhcs_clustat_node));
        ((rhcs_clustat *)clustat)->node[nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->name = mp_strdup(*v);
            else if (strcmp(*k, "state") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->state = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "rgmanager") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->rgmanager = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "nodeid") == 0)
                ((rhcs_clustat *)clustat)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 16);
            if (strcmp(*k, "local") == 0 && strcmp(*v, "1") == 0)
                ((rhcs_clustat *)clustat)->local = ((rhcs_clustat *)clustat)->node[nodes];
        }
        nodes++;
        return;
    }

    if (strcmp(name, "group") == 0) {
        ((rhcs_clustat *)clustat)->group = mp_realloc(((rhcs_clustat *)clustat)->group, (services+2)*sizeof(rhcs_clustat_group));
        ((rhcs_clustat *)clustat)->group[services] = mp_calloc(1, sizeof(rhcs_clustat_group));
        ((rhcs_clustat *)clustat)->group[services+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->name = mp_strdup(*v);
                strsep(&((rhcs_clustat *)clustat)->group[services]->name, ":");
            } else if (strcmp(*k, "state") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->state = (unsigned int) strtol(*v, NULL, 10);
            } else if (strcmp(*k, "owner") == 0) {
                ((rhcs_clustat *)clustat)->group[services]->owner = NULL;
                for(i=0; i < nodes; i++) {
                    if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) {
                        ((rhcs_clustat *)clustat)->group[services]->owner = ((rhcs_clustat *)clustat)->node[i];
                    }
                }
            } else if (strcmp(*k, "last_owner") == 0) {
                for(i=0; i < nodes; i++) {
                    if(strcmp(*v, ((rhcs_clustat *)clustat)->node[i]->name) == 0) {
                        ((rhcs_clustat *)clustat)->group[services]->last = ((rhcs_clustat *)clustat)->node[i];
                    }
                }
            }
        }
        services++;
        return;
    }
}
Exemple #15
0
int mobile_at_command_input(int fd, const char *cmd, const char *opt,
        const char *input, char ***answer, int *answers) {
    char *buf;
    char *ptr;
    char *line;
    size_t len;
    fd_set rfds;
    int retval;
    struct timeval tv;

    buf = mp_malloc(64);

    // Build command string
    if (opt)
        mp_snprintf(buf, 64, "AT%s%s\r", cmd, opt);
    else
        mp_snprintf(buf, 64, "AT%s\r", cmd);

    // Send command
    len = write(fd, buf, strlen(buf));
    if (len != strlen(buf)) {
        if (mp_verbose > 0)
            fprintf(stderr, "Write to device failed. "
                    "Written %d of %d chars.\n", (int)len, (int)strlen(buf));
        return -1;
    }
    if (mp_verbose > 3)
        printf(" >> %s\n", buf);

    if (input) {
        len = write(fd, input, strlen(input));
        if (len != strlen(input)) {
            if (mp_verbose > 0)
                fprintf(stderr, "Write to device failed. "
                        "Written %d of %d chars.\n", (int)len,
                        (int)strlen(buf));
            return -1;
        }
        if (mp_verbose > 3)
            printf(" >> %s\n", input);
        len = write(fd, "\r\x1A", 2);
        if (len != 2) {
            if (mp_verbose > 0)
                fprintf(stderr, "Write to device failed. "
                        "Written %d of 2 chars.\n", (int)len);
            return -1;
        }

    }

    // Read answers
    len = 0;
    ptr = buf;
    tv.tv_sec = 5;
    tv.tv_usec = 0;
    if (answers)
        *answers = 0;
    while (1) {
        // Build select list;
        FD_ZERO(&rfds);
        FD_SET(fd, &rfds);

        // Wait for input
        retval = select(fd+1, &rfds, NULL, NULL, &tv);
        if (retval <= 0)
            return -1;

        // Read from serial
        ptr = buf;
        ptr += len;
        retval = read(fd, buf, 64-len);
        if (retval <= 0)
            return -1;

        // Fetch lines
        ptr = buf;
        while ((line = strsep(&ptr, "\r\n")) && ptr) {
            if (strlen(line) == 0)
                continue;
            if (mp_verbose > 3)
                printf(" << %s\n", line);
            // Status codes
            if (strcmp(line, "OK") == 0)
                return 0;
            if (strcmp(line, "ERROR") == 0)
                return 1;
            if (strncmp(line, "+CME ERROR: ", 12) == 0)
                return 1;
            // Answer
            if (strncmp(line, cmd, strlen(cmd)) == 0) {
                if(!answers)
                    continue;
                line += strlen(cmd) +2;
                *answer = mp_realloc(*answer, (sizeof(char **)*((*answers)+2)));
                (*answer)[*answers] = strdup(line);
                (*answer)[(*answers)+1] = NULL;
                (*answers)++;
            }
        }
        // Move buffer
        if (line) {
            len = strlen(line);
            memmove(buf, line, len+1);
            len = strlen(buf);
        }
    }
    return 0;
}
Exemple #16
0
void
mpi_setstr(mpi *rop, char *str, int base)
{
    long i;			/* counter */
    int sign;			/* result sign */
    BNI carry;			/* carry value */
    BNI value;			/* temporary value */
    BNI size;			/* size of result */
    char *ptr;			/* end of valid input */

    /* initialization */
    sign = 0;
    carry = 0;

    /* skip leading spaces */
    while (isspace(*str))
	++str;

    /* check if sign supplied */
    if (*str == '-') {
	sign = 1;
	++str;
    }
    else if (*str == '+')
	++str;

    /* skip leading zeros */
    while (*str == '0')
	++str;

    ptr = str;
    while (*ptr) {
	if (*ptr >= '0' && *ptr <= '9') {
	    if (*ptr - '0' >= base)
		break;
	}
	else if (*ptr >= 'A' && *ptr <= 'Z') {
	    if (*ptr - 'A' + 10 >= base)
		break;
	}
	else if (*ptr >= 'a' && *ptr <= 'z') {
	    if (*ptr - 'a' + 10 >= base)
		break;
	}
	else
	    break;
	++ptr;
    }

    /* resulting size */
    size = (ptr - str) * str_bases[base] + 1;

    /* make sure rop has enough storage */
    if (rop->alloc < size) {
	rop->digs = mp_realloc(rop->digs, size * sizeof(BNS));
	rop->alloc = size;
    }
    rop->size = size;

    /* initialize rop to zero */
    memset(rop->digs, '\0', size * sizeof(BNS));

    /* set result sign */
    rop->sign = sign;

    /* convert string */
    for (; str < ptr; str++) {
	value = *str;
	if (islower(value))
	    value = toupper(value);
	value = value > '9' ? value - 'A' + 10 : value - '0';
	value += rop->digs[0] * base;
	carry = value >> BNSBITS;
	rop->digs[0] = (BNS)value;
	for (i = 1; i < size; i++) {
	    value = (BNI)rop->digs[i] * base + carry;
	    carry = value >> BNSBITS;
	    rop->digs[i] = (BNS)value;
	}
    }

    /* normalize */
    if (rop->size > 1 && rop->digs[rop->size - 1] == 0)
	--rop->size;
}
int main (int argc, char **argv) {
    /* Local Vars */
    int ret;
    int i;
    unsigned int len;
    gnutls_x509_crt_t cert[1];
    char *subject = NULL;
    size_t subject_len;
    time_t expiration_time, activation_time;


    /* Set signal handling and alarm */
    if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR)
        critical("Setup SIGALRM trap failed!");

    /* Process check arguments */
    if (process_arguments(argc, argv) != OK)
        unknown("Parsing arguments failed!");

    /* Start plugin timeout */
    alarm(mp_timeout);

    /* Init GnuTLS */
    gnutls_global_init();

    for(i = 0; i < cert_files; i++) {
        if (mp_verbose)
            printf("Cert: %s\n", cert_file[i]);

        /* Read the Cert */
        gnutls_datum_t data = { NULL, 0 };
        ret = mp_slurp(cert_file[i], &(data.data));
        data.size = ret;
        if (ret <= 0) {
            set_critical("Error loading cert file '%s'.", cert_file[i]);
            continue;
        }

        /* Load the Cert to a list. */
        len = 1;
        ret = gnutls_x509_crt_list_import(cert, &len, &data,
                                          GNUTLS_X509_FMT_PEM,
                                          GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED);
        if (ret < 0) {
            set_critical("%s error: %s", cert_file[i], gnutls_strerror(ret));
            continue;
        };

        /* Read der Cert CN */
        if (subject == NULL) {
            subject = mp_malloc(128);
            subject_len = 128;
        }
        ret = gnutls_x509_crt_get_dn_by_oid(cert[0],
                                            GNUTLS_OID_X520_COMMON_NAME, 0, 0,
                                            subject, &subject_len);
        if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) {
            subject_len+=1;
            subject = mp_realloc(subject, subject_len);
            ret = gnutls_x509_crt_get_dn_by_oid(cert[0],
                                                GNUTLS_OID_X520_COMMON_NAME, 0, 0,
                                                subject, &subject_len);
        }
        if (ret != 0) {
            set_critical("%s error: %s", cert_file[i], gnutls_strerror(ret));
            continue;
        }
        if (mp_verbose) {
            printf(" * Subject: %s\n", subject);
        }

        /* Check expire time */
        expiration_time = gnutls_x509_crt_get_expiration_time (cert[0]);
        activation_time = gnutls_x509_crt_get_activation_time (cert[0]);

        if (mp_verbose) {
            printf (" * Certificate is valid since: %s", ctime (&activation_time));
            printf (" * Certificate expires: %s", ctime (&expiration_time));
        }

        int days = (int)difftime(expiration_time, time(0))/86400;
        switch (get_status((expiration_time-time(0)), expire_thresholds)) {
        case STATE_OK:
            set_ok(cert_file[i]);
            break;
        case STATE_WARNING:
            set_warning("%s expires in %d day%s", cert_file[i], days, days==1?"":"s");
            break;
        case STATE_CRITICAL:
            set_critical("%s expires in %d day%s", cert_file[i], days, days==1?"":"s");
            break;
        }

        if (activation_time > time(0)) {
            int days = (int)difftime(activation_time, time(0))/86400;
            set_critical("%s activates in %d day%s", cert_file[i], days, days==1?"":"s");
        }
    }

    // Dissconnect
    gnutls_global_deinit ();

    mp_exit("X509");
}
void mp_buf_free(mp_buf *buf) {
    mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */
    mp_realloc(buf->L, buf, sizeof(*buf), 0);
}
int mp_snmp_subtree_query(netsnmp_session *ss,
                           const oid *subtree_oid,
                           const size_t subtree_len,
                           mp_snmp_subtree *subtree) {

    oid last_oid[MAX_OID_LEN];
    size_t last_len;
    netsnmp_pdu *request  = NULL;
    netsnmp_pdu *response = NULL;
    netsnmp_variable_list *var;
    size_t alloc_size = 0;
    int rc;

    /* prepare result */
    memset(subtree, '\0', sizeof(*subtree));
    subtree->vars = NULL;

    memcpy(last_oid, subtree_oid, subtree_len * sizeof(oid));
    last_len = subtree_len;

    for (;;) {
        /*
         * setup request
         */
        if (ss->version == SNMP_VERSION_1) {
            request = snmp_pdu_create(SNMP_MSG_GETNEXT);
        } else {
            request = snmp_pdu_create(SNMP_MSG_GETBULK);
            request->non_repeaters   = 0;
            request->max_repetitions = 16;
        }
        snmp_add_null_var(request, last_oid, last_len);

        /*
         * commence request
         */
        if (response) {
            snmp_free_pdu(response);
            response = NULL;
        }

        if (mp_verbose > 2) {
            char buf[128];

            snprint_objid((char *) &buf, sizeof(buf), last_oid, last_len);
            printf("Fetching next from OID %s\n", buf);
        }

        rc = snmp_synch_response(ss, request, &response);

        if (mp_verbose > 3)
            printf("snmp_synch_response(): rc=%d, errstat=%ld\n",
                   rc, response->errstat);

        if ((rc == STAT_SUCCESS) && response) {
            if (response->errstat == SNMP_ERR_NOERROR) {
                /*
                 * loop over results (may only be one result in case of SNMP v1)
                 */
                for (var = response->variables; var; var = var->next_variable) {

                    /*
                     * check, if OIDs are incresing to prevent infinite
                     * loop with broken SNMP agents
                     */
                    if (snmp_oidtree_compare(var->name, var->name_length,
                                             last_oid, last_len) < 0) {
                        if (response)
                            snmp_free_pdu(response);

                        mp_snmp_deinit();

                        critical("SNMP error: OIDs are not incresing");
                    }

                    /*
                     * terminate, if oid does not belong to subtree anymore
                     */
                    if ((var->type == SNMP_ENDOFMIBVIEW) ||
                        (snmp_oidtree_compare(subtree_oid,
                                              subtree_len,
                                              var->name,
                                              var->name_length) != 0)) {
                        snmp_free_pdu(response);
                        return rc;
                    }

                    if (mp_verbose > 2)
                        print_variable(var->name, var->name_length, var);

                    if (var->type != SNMP_NOSUCHOBJECT ||
                        var->type != SNMP_NOSUCHINSTANCE) {
                        if (alloc_size <= subtree->size) {
                            alloc_size += 16;
                            subtree->vars =
                                mp_realloc(subtree->vars,
                                           alloc_size *
                                           sizeof(netsnmp_variable_list*));
                        }

                        subtree->vars[subtree->size] =
                            mp_malloc(sizeof(netsnmp_variable_list));
                        snmp_clone_var(var, subtree->vars[subtree->size]);
                        subtree->size++;
                    }

                    /*
                     * save last fetched oid
                     */
                    memcpy(last_oid, var->name,
                           var->name_length * sizeof(oid));
                    last_len = var->name_length;
                } /* for */
            } else if ((ss->version == SNMP_VERSION_1) &&
                       (response->errstat == SNMP_ERR_NOSUCHNAME)) {
                if (mp_verbose > 3)
                    printf("SNMP-V1: end of tree\n");
            } else {
                /*
                 * some other error occured
                 */
                if (mp_verbose > 0)
                    printf("SNMP error: respose->errstat = %ld",
                           response->errstat);

                rc = STAT_ERROR;
                //goto done;
                break;
            }
        } else {
            /* no response, assume an error */
            rc = STAT_ERROR;
            break;
        }
    }

    if (response)
        snmp_free_pdu(response);

    return rc;
}
Exemple #20
0
void rhcs_conf_startElement(void *conf, const char *name, const char **atts) {
    const char **k, **v;
    int i;

    if (strcmp(name, "cluster") == 0) {
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->name = mp_strdup(*v);
            else if (strcmp(*k, "alias") == 0)
                ((rhcs_conf *)conf)->alias = mp_strdup(*v);
            else if (strcmp(*k, "config_version") == 0)
                ((rhcs_conf *)conf)->version = (unsigned int) strtol(*v, NULL, 10);
        }
        return;
    }
    if (strcmp(name, "clusternode") == 0) {
        ((rhcs_conf *)conf)->node = mp_realloc(((rhcs_conf *)conf)->node, (nodes+2)*sizeof(rhcs_conf_node));
        ((rhcs_conf *)conf)->node[nodes] = mp_calloc(1, sizeof(rhcs_conf_node));
        ((rhcs_conf *)conf)->node[nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->node[nodes]->name = mp_strdup(*v);
            else if (strcmp(*k, "nodeid") == 0)
                ((rhcs_conf *)conf)->node[nodes]->id = (unsigned int) strtol(*v, NULL, 10);
            else if (strcmp(*k, "votes") == 0)
                ((rhcs_conf *)conf)->node[nodes]->votes = (unsigned int) strtol(*v, NULL, 10);
        }
        nodes++;
        return;
    }
    if (strcmp(name, "failoverdomain") == 0) {
        ((rhcs_conf *)conf)->fodomain = mp_realloc(((rhcs_conf *)conf)->fodomain, (fodomains+2)*sizeof(rhcs_conf_fodom));
        ((rhcs_conf *)conf)->fodomain[fodomains] = mp_calloc(1, sizeof(rhcs_conf_fodom));
        ((rhcs_conf *)conf)->fodomain[fodomains+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->name = mp_strdup(*v);
            else if (strcmp(*k, "nofailback") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->failback = (strcmp(*v, "0") == 0);
            else if (strcmp(*k, "ordered") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->ordered = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "restricted") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->restricted = (strcmp(*v, "1") == 0);
        }
        fodomain_nodes = 0;
        return;
    }
    if (strcmp(name, "failoverdomainnode") == 0) {
        ((rhcs_conf *)conf)->fodomain[fodomains]->node = mp_realloc(((rhcs_conf *)conf)->fodomain[fodomains]->node, (fodomain_nodes+2)*sizeof(rhcs_conf_fodom_node));
        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes] = mp_calloc(1, sizeof(rhcs_conf_fodom_node));
        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                for (i=0; i < nodes; i++) {
                    if (strcmp(*v, ((rhcs_conf *)conf)->node[i]->name) == 0) {
                        ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->node = ((rhcs_conf *)conf)->node[i];
                    }
                }
            else if (strcmp(*k, "priority") == 0)
                ((rhcs_conf *)conf)->fodomain[fodomains]->node[fodomain_nodes]->priority = (unsigned int) strtol(*v, NULL, 10);
        }
        fodomain_nodes++;
        return;
    }
    if (strcmp(name, "service") == 0) {
        ((rhcs_conf *)conf)->service = mp_realloc(((rhcs_conf *)conf)->service, (services+2)*sizeof(rhcs_conf_service));
        ((rhcs_conf *)conf)->service[services] = mp_calloc(1, sizeof(rhcs_conf_service));
        ((rhcs_conf *)conf)->service[services+1] = NULL;
        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2) {
            if (strcmp(*k, "name") == 0)
                ((rhcs_conf *)conf)->service[services]->name = mp_strdup(*v);
            else if (strcmp(*k, "domain") == 0)
                for (i=0; i < fodomains; i++) {
                    if (strcmp(*v, ((rhcs_conf *)conf)->fodomain[i]->name) == 0) {
                        ((rhcs_conf *)conf)->service[services]->fodomain = ((rhcs_conf *)conf)->fodomain[i];
                    }
                }
            else if (strcmp(*k, "autostart") == 0)
                ((rhcs_conf *)conf)->service[services]->autostart = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "exclusive") == 0)
                ((rhcs_conf *)conf)->service[services]->exclusive = (strcmp(*v, "1") == 0);
            else if (strcmp(*k, "recovery") == 0) {
                if (strcmp(*v, "relocate") == 0)
                    ((rhcs_conf *)conf)->service[services]->recovery = RELOCATE;
                else if (strcmp(*v, "restart") == 0)
                    ((rhcs_conf *)conf)->service[services]->recovery = RESTART;
            }
        }
        services++;
        return;
    }
    /*
        printf("%s\n", name);

        for(k = atts, v = atts+1; *k != NULL; k+=2, v+=2)
            printf("  '%s' => '%s'\n", *k, *v);
    */
}