示例#1
0
/*
	parse_line
	Parses a line from the config file.  Lines must be of the form
	"option-name[whitespace]value[\n]"
	We're kinda restrictive for now, but the options are so simple that
	we shouldn't need anything complex for now.
	Returns -1 on error, 0 otherwise.
*/
static int parse_line(char *line)
{
	char	*opt = line, 
		*val,
		*p;

	p = strchr(line, '\n');	/* Strip the trailing newline. */
	if(p != NULL)
		*p = '\0';

	/* Seperate the value from the option. */
	val = opt;
	while(!isspace(*val)) {
		val++;
		if(*val == '\0') {
			io_err("Missing value for '%s'!'\n", opt);
			return -1;
		}
	}
	
	*val = '\0';
	val++;

	while(isspace(*val)) {
		val++;
		if(*val == '\0') {    
                        io_err("Missing value for '%s'!'\n", opt);
                        return -1;
                }
	}

	if(is_default_opt(opt) && !parsed_yet) 
		return set_opt(opt, val);
	return 0;
}
示例#2
0
void FileCtl::open              // OPEN THE FILE
    ( char const* fname         // - file name
    , char const* def           // - default suffix
    , char const* mode )        // - open mode
{
    make_filename( fname, def );
    _file = fopen( _filename, mode );
    if( 0 == _file ) {
        io_err( "cannot open" );
    }
}
 open_chain&
 recl(
   int const& val)
 {
   if (val <= 0) {
     throw io_err(
       "Invalid OPEN RECL: value is zero or negative,"
       " but must be greater than zero");
   }
   u_ptr->recl = static_cast<unsigned>(val);
   return *this;
 }
示例#4
0
/*
	parse_config
	Reads and config file, removing comments and checking for errors.  Gets
	parse_line() to parse the individual lines.
	Returns 0 for success, -1 for error.
*/
static int parse_config(FILE *stream)
{
	int	lineno;
	char 	line[MAXPATHLEN],	/* should be long enough */
		*idx;
	
	for(lineno = 1; fgets(line, MAXPATHLEN, stream) != NULL; lineno++) {
		idx = strchr(line, '#');
		if(idx != NULL)	
			*idx = '\0';
		if(opt_is_valid(line)) {
			if(parse_line(line) == -1) {
				io_err("Could not parse line %d\n", lineno);
				return -1;
			}
		} else {
			io_err("Unrecognized option on line %d.\n", lineno);
			return -1;
		}
	}
	return 0;
}
示例#5
0
/**
	valid_dsa_sig
	Given some data, the data length, the public key, the public key 
	length, and the 48-byte signature, we verify it and return 1 for
	true and 0 for false, or -1 for error.
*/
int valid_dsa_sig(const void *data, int dlen, const char sig[48])
{
	int	r;
	char	hash[20];

	SHA1(data, dlen, hash);
	r = DSA_verify(0xdecafbad, hash, 20, sig, 48, dispatch);
	if(!r) {
		print_loc();
		io_err("Couldn't verify signature!  Info follows:\n");
		io_debug("Hexdump of signature:\n");
		io_hexdump(stdout, sig, 48);
		io_debug("\nHexdump of data follows:\n");
		io_hexdump(stdout, data, dlen);
	}
	return r;
}
示例#6
0
/**
	read_config
	Reads and parses the specified config file, and sets the appropriate 
	options.
	Returns -1 on error, 0 on success.
*/
int read_config(const char *filename)
{
	FILE *cf;
	int r;

	cf = fopen(filename, "r");
	if(cf == NULL) {
		io_debug("Error opening config file '%s':  %s\n"
			"Skipping config.\n", filename, strerror(errno));
		return 0;
	}

	r = parse_config(cf);
	fclose(cf);
	parsed_yet = 1;

	if(r == -1)
		io_err("Trouble parsing %s.\n", filename);

	return r;
}
示例#7
0
/**
	save_dispatch_key
	Saves a base64 encoded key, as well as keeping it for future use.  Pass
	it a key in the form of a null-terminated string.
	Returns -1 for error, 0 for success.
*/
int save_dispatch_key(const char *key)
{
	int r;
	char *pub;

	if(dispatch != NULL)
		return 0;	/* We have ignored you! */

	dispatch = DSA_generate_parameters(DSA_KEYLEN, NULL, 0, NULL, NULL, 
		NULL, NULL);
	if(dispatch == NULL) {
		io_debug("%s:%s():%d  ", __FILE__, __func__, __LINE__);
		io_err("Not enough free memory!  The walls are closing in!\n");
		return -1;
	}

	pub = fm_abs(FM_KEY_DISPATCH_PUBLIC);

	r = !fm_write(FM_KEY_DISPATCH_PUBLIC, key, strlen(key));
	r |= load_keys(pub, NULL, &dispatch);
	free(pub);

	return r;
}
示例#8
0
int FileCtlInput::getline       // GET A LINE
    ( char* buf                 // - buffer
    , unsigned size )           // - buffer size
{
    int retn;
    if( 0 == fgets( buf, size, _file ) ) {
        if( 1 == eof( fileno( _file ) ) ) {
            retn = 0;
        } else {
            io_err( "error reading input file" );
        }
    } else {
        for( ; ; ++buf ) {
            if( *buf == '\0' )
                break;
            if( *buf == '\n' ) {
                *buf = '\0';
                break;
            }
        }
        retn = 1;
    }
    return( retn );
}