Example #1
0
/*#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
*/
int main(int argc, char **argv){

TCHDB *hdb;
char *key, *value; 

/* create the object */
hdb = tchdbnew();

/* open the database */
if(!tchdbopen(hdb, "teloon.hdb", HDBOWRITER|HDBOTRUNC)){
	fprintf(stderr, "open error\n");
}

/* store records */
if(!tchdbput2(hdb, "foo", "hop") ||
!tchdbput2(hdb, "barz", "step") ||
!tchdbput2(hdb, "ssss", "jump")){
fprintf(stderr, "put error\n");
}

/* retrieve records */
value = tchdbget2(hdb, "foo");
if(value){
printf("%s\n", value);
free(value);
} else {
fprintf(stderr, "get error\n");
}

/* traverse records */
tchdbiterinit(hdb);
while((key = tchdbiternext2(hdb)) != NULL){
value = tchdbget2(hdb, key);
if(value){
printf("%s:%s\n", key, value);
free(value);
}
free(key);
}
tchdbiterinit(hdb);
while((key = tchdbiternext2(hdb)) != NULL){
value = tchdbget2(hdb, key);
if(value){
printf("%s:%s\n", key, value);
free(value);
}
free(key);
}

/* close the database */
if(!tchdbclose(hdb)){
fprintf(stderr, "close error\n");
}

/* delete the object */
tchdbdel(hdb);

return 0;
} 
Example #2
0
int unpack_files(char *root_dir, bool verbose, bool test) {

    char *key;
    void *value;
    int fd, count, key_size, val_size;


    /* traverse records */
    count = 0;
    tchdbiterinit(hdb);
    while ((key = tchdbiternext2(hdb)) != NULL) {
        key_size = (int) strlen(key);

        if (verbose || test) fprintf(stdout, "Extracting file: %s\n", key);

        value = tchdbget(hdb, key, key_size, &val_size);
        if (value && !test) {
            if (-1 == (fd = open(key, O_WRONLY | O_CREAT | O_TRUNC, 0644))) {
                fprintf(stdout, "***Failed open file %s\n", key);
                return 1;
            }

            write(fd, value, val_size);
            close(fd);
            free(value);
        }
        free(key);
        count++;
    }

    return count;
}
Example #3
0
void check_orphaned_data_blocks()
{
    char *key;

    /* traverse records */
    tchdbiterinit(dbdta);
    while ((key = tchdbiternext2(dbdta)) != NULL) {
        if ( 0 == check_inuse((unsigned char *)key)) {
           printhash("Deleting orphaned hash",(unsigned char *)key);
           if (!tchdbout(dbdta, key, config->hashlen)) {
              die_syserr();
           }
        }
        free(key);
    }
}
Example #4
0
char *get_host_pubkey(char *host) {
	char *key;
	char value[BUFSIZE];
	char *buffer = (char *) malloc(BUFSIZE *sizeof(char));
	char *keyname = NULL;
	char needle[BUFSIZE];
	TCHDB *hdb;
  hdb = tchdbnew();
	int ecode;
	struct addrinfo *result;
	struct addrinfo *res;

	if(tchdbopen(hdb,LASTSEEN_DB,HDBOREADER) != 1){
		printf("%s for database '%s'\n",tchdberrmsg(tchdbecode(hdb)),LASTSEEN_DB);
		return NULL;
	}
	int error = getaddrinfo(host,NULL,NULL,&result);

	for(res = result; res != NULL; res = res->ai_next) {
		inet_ntop(res->ai_family, get_in_addr((struct sockaddr *)res->ai_addr), needle, sizeof(needle));

		tchdbiterinit(hdb);
		while((key = tchdbiternext2(hdb)) != NULL && keyname == NULL){
			if(strspn(key,"k") == 1){
				tchdbget3(hdb, key, strlen(key) + 1, value, BUFSIZE);
				if(strcmp(value,needle) == 0){
					keyname = ++key;
				}
			}
		}
	}
	tchdbclose(hdb);

	if(keyname) {
		sprintf(buffer,"%s/root-%s.pub",PPKEYS,keyname);
		return buffer;
	}else{
		for(res = result; res != NULL; res = res->ai_next) {
			inet_ntop(res->ai_family, get_in_addr((struct sockaddr *)res->ai_addr), needle, sizeof(needle));
			sprintf(buffer,"%s/root-%s.pub",PPKEYS,needle);
			if(file_exist(buffer)){
				return buffer;
			}
		}
	}
	return NULL;
}