Exemplo n.º 1
0
int GeoIP_db_avail(int type) {
	const char * filePath;
	if (type < 0 || type >= NUM_DB_TYPES) {
		return 0;
	}
	_GeoIP_setup_dbfilename();
	filePath = GeoIPDBFileName[type];
	if (NULL == filePath) {
		return 0;
	}
	return _file_exists(filePath);
}
Exemplo n.º 2
0
struct btree *btree_new(const char* fname)
{
	char dbname[1024], idxname[1024];
	struct btree *b = malloc(sizeof(*b));

	memset(b, 0, sizeof(*b));
	sprintf(idxname, "%s%s", fname,IDXEXT);
	sprintf(dbname, "%s%s", fname,DBEXT);

	if(_file_exists(idxname))
		_btree_open(b, idxname, dbname);
	else
		_btree_creat(b, idxname, dbname);

	return b;
}
void nx_expr_proc__xm_fileop_file_cycle(nx_expr_eval_ctx_t *eval_ctx,
					nx_module_t *module,
					nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    nx_expr_arg_t *val;
    nx_value_t value;
    apr_status_t rv;
    apr_pool_t *pool = NULL;
    nx_exception_t e;
    volatile int64_t max = 0;
    int32_t i, last;
    nx_string_t *tmpstr = NULL;
    nx_string_t *tmpstr2 = NULL;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }
    if ( file.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	throw_msg("string type required for 'file'");
    }


    val = NX_DLIST_NEXT(arg, link);
    if ( val != NULL )
    {
	ASSERT(val->expr != NULL);
	nx_expr_evaluate(eval_ctx, &value, val->expr);
	if ( value.type != NX_VALUE_TYPE_INTEGER )
	{
	    nx_value_kill(&file);
	    nx_value_kill(&value);
	    throw_msg("integer type required for 'max'");
	}
	if ( value.defined == TRUE )
	{
	    if ( value.integer <= 0 )
	    {
		nx_value_kill(&file);
		nx_value_kill(&value);
		throw_msg("'max' must be a positive integer");
	    }
	    max = value.integer;
	}
    }

    try
    {
	pool = nx_pool_create_core();
	
	if ( _file_exists(file.string->buf, pool) == TRUE )
	{  // check if the file we need to cycle exists
	    tmpstr = nx_string_new();
	    tmpstr2 = nx_string_new();
	    last = 0;
	    for ( i = 1; i < 2147483647 /*APR_INT32_MAX*/; i++ )
	    {
		nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i);
		if ( _file_exists(tmpstr->buf, pool) == FALSE )
		{
		    break;
		}
		if ( (max > 0) && (i >= max) )
		{
		    log_info("removing file %s", tmpstr->buf);
		    if ( (rv = apr_file_remove(tmpstr->buf, NULL)) != APR_SUCCESS )
		    {
			log_aprerror(rv, "failed to remove file '%s'", tmpstr->buf);
		    }
		}
		else
		{
		    last = i;
		}
	    }
	    if ( last > 0 )
	    { // now starting from the last existing file, cycle them
		for ( i = last; i > 0; i-- )
		{
		    nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i);
		    nx_string_sprintf(tmpstr2, "%s.%d", file.string->buf, i + 1);
		    log_debug("cycling %s to %s", tmpstr->buf, tmpstr2->buf);
		    if ( (rv = apr_file_rename(tmpstr->buf, tmpstr2->buf, NULL)) != APR_SUCCESS )
		    {
			log_aprerror(rv, "failed to rename file from '%s' to '%s'", 
				     tmpstr->buf, tmpstr2->buf);
		    }
		}
	    }
	    // finally rename file to file.1
	    nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, 1);
	    if ( (rv = apr_file_rename(file.string->buf, tmpstr->buf, NULL)) != APR_SUCCESS )
	    {
		log_aprerror(rv, "failed to rename file from '%s' to '%s'", 
			     file.string->buf, tmpstr->buf);
	    }
	    _reopen_logfile(file.string->buf);

	    nx_string_free(tmpstr);
	    nx_string_free(tmpstr2);
	}
	apr_pool_destroy(pool);
	nx_value_kill(&file);
    }
    catch(e)
    {
	nx_value_kill(&file);
	if ( pool != NULL )
	{
	    apr_pool_destroy(pool);
	}
	if ( tmpstr != NULL )
	{
	    nx_string_free(tmpstr);
	}
	if ( tmpstr2 != NULL )
	{
	    nx_string_free(tmpstr2);
	}
	log_exception(e);
    }
}
Exemplo n.º 4
0
int writeKeyFile() {
	FILE *fp[3];
	char buf[128];
	int proceed = 1;

	/* create ~/.pppauth if necessary */
	if ( ! _dir_exists(_key_file_dir()) ) {
		mkdir(_key_file_dir(), S_IRWXU);
	}

	/* warn about overwriting an existing key */
	if ( _file_exists(_key_file_name()) ) {
		proceed = 0;
		fprintf(stderr, "\n"
		    "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
			"@    WARNING: YOU ARE ABOUT TO OVERWRITE YOUR KEY FILE!   @\n"
			"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
			"\n"
			"IF YOU PROCEED, YOUR EXISTING PASSCARDS WILL BECOME USELESS!\n"
			"\n"
			"If this is not what you intended to do, type `NO' below.\n"
			"\n"
			"By typing `yes' below, a new sequence key will be generated\n"
			"and you will no longer be able to log in using your existing\n"
			"passcards.  New passcards must be printed.\n"
			"\n"
		);

		proceed = confirm("Are you sure you want to proceed (yes/no)? ");
	}

	if (proceed) {
		_enforce_permissions();

		umask(S_IRWXG|S_IRWXO);
		fp[0] = fopen(_key_file_name(), "w");
		fp[1] = fopen(_cnt_file_name(), "w");
		fp[2] = fopen(_gen_file_name(), "w");
		if (fp[0] && fp[1] && fp[2]) {
			_write_data(seqKey(), fp[0]);
			fclose(fp[0]);

			_write_data(currPasscodeNum(), fp[1]);
			fclose(fp[1]);

			_write_data(lastCardGenerated(), fp[2]);
			fclose(fp[2]);

			memset(buf, 0, 128);

			fprintf(stderr, "\n"
				"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
				"  A new sequence key has been generated and saved.  It is\n"
				"  HIGHLY RECOMMENDED that you IMMEDIATELY print new pass-\n"
				"  cards in order to access your account in the future.\n"
				"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
				"\n"
			);
			return 1;
		}
	} else {
		fprintf(stderr, "\n"
			"===========================================================\n"
			"  A new sequence key WAS NOT generated and your old key\n"
			"  remains intact.  As a result, you can continue to use\n"
			"  your existing passcards to log into your account.\n"
			"===========================================================\n"
			"\n"
		);
	}

	return 0;
}
Exemplo n.º 5
0
int readKeyFile(int lock) {
	FILE *fp;
	char buf[129]; /* 128 bytes + one ensured '\0' character */
	mp_int num;
	int ver[3];

	if ( ! _file_exists(_key_file_name()) )
		return 0;

	if ( ! _file_exists(_cnt_file_name()) )
		return 0;

	if ( ! _file_exists(_gen_file_name()) )
		return 0;


	/*
	 * 1. Reading key file
	 */

	fp = fopen(_key_file_name(), "r");
	if ( ! fp)
		return 0;

	/* If file is opened we can assume that there's a directory
	 * where we can hold lock file */
	if (lock)
		lockingFailed = doLocking();

	/* fread can fail; then strlen used in
	 * _ppp_flags might not work as supposed.
	 * It would be best to check values returned by
	 * fread() each time it's used.
	 */
	memset(buf, 0, sizeof(buf));

	fread(buf, 1, sizeof(buf) - 1, fp);
	buf[sizeof(buf) - 1] = '\0'; /* Ensure \0 existance so we can treat this buffer with scanf */

	fclose(fp);

	ver[0] = _ppp_version(buf);
	if (ver[0] < 0)
		goto error;

	pppSetFlags(_ppp_flags(buf)); /* load flags */

	/* read key */
	mp_init(&num);
	if ( ! _read_data(buf, &num))
		goto error;
	setSeqKey(&num);

	/*
	 * 2. Reading cnt file
	 */
	fp = fopen(_cnt_file_name(), "r");
	if ( ! fp)
		goto error;
	fread(buf, 1, sizeof(buf) - 1, fp);
	buf[sizeof(buf) - 1] = '\0';
	fclose(fp);
	ver[1] = _ppp_version(buf);
	if (ver[1] < 0)
		goto error;

	if ( ! _read_data(buf, &num))
		goto error;

	setCurrPasscodeNum(&num);

	/*
	 * 3. Reading gen file
	 */
	fp = fopen(_gen_file_name(), "r");
	if ( ! fp)
		goto error;
	fread(buf, 1, sizeof(buf) - 1, fp);
	buf[sizeof(buf) - 1] = '\0';
	fclose(fp);

	ver[2] = _ppp_version(buf);
	if (ver[2] < 0)
		goto error;

	if ( ! _read_data(buf, &num))
		goto error;

	setLastCardGenerated(&num);

	memset(buf, 0, sizeof(buf));
	mp_clear(&num);

	if ( (ver[0] != ver[1]) || (ver[1] != ver[2]) ) {
		/* Inconsistency in PPP version among the three data files */
		goto error;
	}

	/* tell PPP code which version the key expects */
	setKeyVersion(ver[0]);
	return 1;

error:
	memset(buf, 0, sizeof(buf));
	mp_clear(&num);
	if (lock)
		doUnlocking();
	return 0;
}
Exemplo n.º 6
0
int keyfileExists() {
	return _file_exists(_key_file_name());
}