db.c
changeset 18 d4ce82194b64
parent 17 bd746609ba46
child 22 822094314703
equal deleted inserted replaced
17:bd746609ba46 18:d4ce82194b64
    78  *
    78  *
    79  */
    79  */
    80 unsigned short int
    80 unsigned short int
    81 db_create_new( char *txt )
    81 db_create_new( char *txt )
    82 {
    82 {
       
    83 	FILE *txt_f  = NULL;
       
    84 	parsed *rule = NULL;
       
    85 	char buf[ LINE_BUFSIZE*10 ], tmpfile[25];
       
    86 	int  tmp_fd, linenum = 0, parsed = 0, error = 0;
    83 	struct cdb_make cdbm;
    87 	struct cdb_make cdbm;
    84 
       
    85 	char buf[ LINE_BUFSIZE*10 ];
       
    86 	char tmpfile[25];
       
    87 	int  tmp_fd;
       
    88 	FILE *txt_f = NULL;
       
    89 	int  linenum = 0, parsed = 0;
       
    90 	struct db_input *dbline;
    88 	struct db_input *dbline;
    91 
    89 
    92 	/* open temporary file */
    90 	/* open temporary file */
    93 	debug( 0, LOC, "Creating/updating database (%s) using rules in \"%s\"\n", v.dbname, txt );
    91 	debug( 0, LOC, "Creating/updating database (%s) using rules in \"%s\"\n", v.dbname, txt );
    94 	sprintf( tmpfile, "volta-db-%d.tmp", getpid() );
    92 	sprintf( tmpfile, "volta-db-%d.tmp", getpid() );
   110 		linenum++;
   108 		linenum++;
   111 
   109 
   112 		/* skip blank lines and comments */
   110 		/* skip blank lines and comments */
   113 		if ( strlen(buf) == 1 || buf[0] == '#' ) continue;
   111 		if ( strlen(buf) == 1 || buf[0] == '#' ) continue;
   114 
   112 
   115 		/* validate and add! */
   113 		/* validate line */
   116 		dbline = parse_dbinput( buf );
   114 		dbline = parse_dbinput( buf );
   117 		if ( dbline == NULL ) {
   115 		if ( dbline == NULL ) {
   118 			debug( 0, LOC, "Invalid rule (line %d), skipping: %s", linenum, buf );
   116 			debug( 0, LOC, "Invalid rule (line %d), stopping: %s", linenum, buf );
   119 			continue;
   117 			error = 1;
   120 		}
   118 			break;
   121 
   119 		}
       
   120 
       
   121 		/* validate rule */
       
   122 		rule = parse_rule( dbline->val );
       
   123 		if ( rule == NULL ||
       
   124 			( rule->negate == 1 && rule->host != NULL ) ||
       
   125 			( rule->negate == 0 && rule->host == NULL )) {
       
   126 
       
   127 			debug( 0, LOC, "Invalid rule (line %d), stopping: %s", linenum, buf );
       
   128 			error = 1;
       
   129 			break;
       
   130 		}
       
   131 
       
   132 		/* looking good, add rule */
   122 		cdb_make_add( &cdbm, dbline->key, dbline->klen, dbline->val, dbline->vlen );
   133 		cdb_make_add( &cdbm, dbline->key, dbline->klen, dbline->val, dbline->vlen );
   123 		parsed++;
   134 		parsed++;
   124 
   135 
   125 		free( dbline->key );
   136 		free( dbline->key );
   126 		free( dbline->val );
   137 		free( dbline->val );
   127 		free( dbline );
   138 		free( dbline ), dbline = NULL;
       
   139 		finish_parsed( rule ), rule = NULL;
   128 	}
   140 	}
   129 
   141 
   130 	/* write indexes */
   142 	/* write indexes */
   131 	fclose( txt_f );
   143 	fclose( txt_f );
   132 	cdb_make_finish( &cdbm );
   144 	cdb_make_finish( &cdbm );
   133 	close( tmp_fd );
   145 	close( tmp_fd );
   134 
   146 
   135 	/* move cdb into place */
   147 	if ( error == 1 ) {
   136 	if ( (rename( tmpfile, v.dbname )) == -1 ) {
   148 		/* delete the tmp db on errors */
   137 		debug( 0, LOC, "Unable to move temp cdb into place: %s", strerror(errno) );
   149 		if ( (unlink( tmpfile )) != 0 ) {
   138 		return( 1 );
   150 			debug( 0, LOC, "Unable to remove temp cdb: %s", strerror(errno) );
   139 	}
   151 			return( 1 );
   140 
   152 		}
   141 	debug( 0, LOC, "Added %d rules to %s.\n", parsed, v.dbname );
   153 	}
       
   154 	else {
       
   155 		/* move cdb into place */
       
   156 		if ( (rename( tmpfile, v.dbname )) == -1 ) {
       
   157 			debug( 0, LOC, "Unable to move temp cdb into place: %s", strerror(errno) );
       
   158 			return( 1 );
       
   159 		}
       
   160 		debug( 0, LOC, "Added %d rules to %s.\n", parsed, v.dbname );
       
   161 	}
       
   162 
   142 	return( 0 );
   163 	return( 0 );
   143 }
   164 }
   144 
   165 
   145 
   166 
   146 /* 
   167 /* 
   167 	while ( cdb_findnext( &cdbf ) > 0 ) {
   188 	while ( cdb_findnext( &cdbf ) > 0 ) {
   168 		vpos = cdb_datapos( &v.db );
   189 		vpos = cdb_datapos( &v.db );
   169 		vlen = cdb_datalen( &v.db );
   190 		vlen = cdb_datalen( &v.db );
   170 
   191 
   171 		/* pull the value from the db */
   192 		/* pull the value from the db */
   172 		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
   193 		if ( (val = calloc( vlen+1, sizeof(char) )) == NULL ) {
   173 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   194 			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
   174 					strerror(errno) );
   195 					strerror(errno) );
   175 			return( NULL );
   196 			return( NULL );
   176 		}
   197 		}
   177 		cdb_read( &v.db, val, vlen, vpos );
   198 		cdb_read( &v.db, val, vlen, vpos );
   178 
   199 
   179 		/* check it against the request */
   200 		/* check it against the request */
   180 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
   201 		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
   181 		rule = parse_rule( val );
   202 		rule = parse_rule( val );
       
   203 
   182 		free( val );
   204 		free( val );
   183 		if ( rule != NULL ) {
   205 		if ( rule != NULL ) {
   184 			if ( check_rule( rule, p_request ) == 0 ) {
   206 			if ( check_rule( rule, p_request ) == 0 ) {
   185 				finish_parsed( rule ), rule = NULL;
   207 				finish_parsed( rule ), rule = NULL;
   186 			}
   208 			}