void wi_test_system(void) {
    wi_array_t  *backtrace;
    
    WI_TEST_ASSERT_TRUE(wi_user_id() >= 0, "");
    WI_TEST_ASSERT_TRUE(wi_string_length(wi_user_name()) > 0, "");
    WI_TEST_ASSERT_TRUE(wi_string_length(wi_user_home()) > 0, "");
    WI_TEST_ASSERT_TRUE(wi_group_id() >= 0, "");
    WI_TEST_ASSERT_TRUE(wi_string_length(wi_group_name()) > 0, "");

    WI_TEST_ASSERT_TRUE(wi_page_size() > 0, "");
    
    backtrace = wi_backtrace();
    
    if(backtrace) {
        WI_TEST_ASSERT_TRUE(wi_array_count(backtrace) > 0, "");
        WI_TEST_ASSERT_NOT_EQUALS(wi_string_index_of_string(wi_array_components_joined_by_string(backtrace, WI_STR("\n")),
                                                            WI_STR("test"), 0),
                                  WI_NOT_FOUND, "");
    }
    
    WI_TEST_ASSERT_TRUE(wi_string_length(wi_getenv(WI_STR("HOME"))) > 0, "");
}
Beispiel #2
0
static wi_string_t * _wi_p7_message_field_string_value(wi_p7_message_t *p7_message, wi_p7_spec_field_t *field) {
	wi_string_t				*field_name, *field_value = NULL;
	wi_uuid_t				*uuid;
	wi_date_t				*date;
	wi_p7_boolean_t			p7_bool;
	wi_p7_enum_t			p7_enum;
	wi_p7_int32_t			p7_int32;
	wi_p7_uint32_t			p7_uint32;
	wi_p7_int64_t			p7_int64;
	wi_p7_uint64_t			p7_uint64;
	wi_p7_double_t			p7_double;
	wi_p7_oobdata_t			p7_oobdata;
	wi_string_t				*string;
	wi_data_t				*data;
	wi_array_t				*list;
	
	field_name = wi_p7_spec_field_name(field);
	
	switch(wi_p7_spec_type_id(wi_p7_spec_field_type(field))) {
		case WI_P7_BOOL:
			if(wi_p7_message_get_bool_for_name(p7_message, &p7_bool, field_name))
				field_value = wi_string_with_format(WI_STR("%@"), p7_bool ? WI_STR("true") : WI_STR("false"));
			break;
			
		case WI_P7_ENUM:
			if(wi_p7_message_get_enum_for_name(p7_message, &p7_enum, field_name))
				field_value = wi_dictionary_data_for_key(wi_p7_spec_field_enums_by_value(field), (void *) (intptr_t) p7_enum);
			break;
			
		case WI_P7_INT32:
			if(wi_p7_message_get_int32_for_name(p7_message, &p7_int32, field_name))
				field_value = wi_string_with_format(WI_STR("%d"), p7_int32);
			break;
			
		case WI_P7_UINT32:
			if(wi_p7_message_get_uint32_for_name(p7_message, &p7_uint32, field_name))
				field_value = wi_string_with_format(WI_STR("%u"), p7_uint32);
			break;
			
		case WI_P7_INT64:
			if(wi_p7_message_get_int64_for_name(p7_message, &p7_int64, field_name))
				field_value = wi_string_with_format(WI_STR("%lld"), p7_int64);
			break;
			
		case WI_P7_UINT64:
			if(wi_p7_message_get_uint64_for_name(p7_message, &p7_uint64, field_name))
				field_value = wi_string_with_format(WI_STR("%llu"), p7_uint64);
			break;
			
		case WI_P7_DOUBLE:
			if(wi_p7_message_get_double_for_name(p7_message, &p7_double, field_name))
				field_value = wi_string_with_format(WI_STR("%0.16f"), p7_double);
			break;
			
		case WI_P7_STRING:
			string = wi_p7_message_string_for_name(p7_message, field_name);
			
			if(string)
				field_value = wi_string_with_format(WI_STR("\"%@\""), string);
			break;
		
		case WI_P7_UUID:
			uuid = wi_p7_message_uuid_for_name(p7_message, field_name);
			
			if(uuid)
				field_value = wi_string_with_format(WI_STR("%@"), wi_uuid_string(uuid));
			break;
		
		case WI_P7_DATE:
			date = wi_p7_message_date_for_name(p7_message, field_name);
			
			if(date)
				field_value = wi_string_with_format(WI_STR("%@"), wi_date_string_with_format(date, WI_STR("%Y-%m-%d %H:%M:%S %z")));
			break;
			
		case WI_P7_DATA:
			data = wi_p7_message_data_for_name(p7_message, field_name);
			
			if(data)
				field_value = wi_string_with_format(WI_STR("%@"), data);
			break;
			
		case WI_P7_OOBDATA:
			if(wi_p7_message_get_oobdata_for_name(p7_message, &p7_oobdata, field_name))
				field_value = wi_string_with_format(WI_STR("%llu"), p7_oobdata);
			break;
		
		case WI_P7_LIST:
			list = wi_p7_message_list_for_name(p7_message, field_name);
			
			if(list)
				field_value = wi_array_components_joined_by_string(list, WI_STR(", "));
			break;
	}
	
	return field_value;
}
Beispiel #3
0
int main(int argc, const char **argv) {
	wi_mutable_array_t		*arguments;
	wi_pool_t				*pool;
	wi_string_t				*string, *root_path;
	int						ch, facility;
	wi_boolean_t			test_config, daemonize, change_directory, switch_user;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_version_init();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	root_path				= WI_STR(WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	test_config				= false;
	daemonize				= true;
	change_directory		= true;
	switch_user				= true;

	/* init reexec argument list */
	arguments				= wi_array_init(wi_mutable_array_alloc());

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVvXx")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				root_path = wi_string_with_cstring(optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0)
					wi_log_fatal(WI_STR("Could not find syslog facility \"%@\": %m"), string);
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				break;

			case 'V':
			case 'v':
				wt_version();
				break;
				
			case 'X':
				daemonize = false;
				break;
				
			case 'x':
				daemonize = false;
				change_directory = false;
				switch_user = false;
				break;
			
			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
		
		wi_mutable_array_add_data(arguments, wi_string_with_format(WI_STR("-%c"), ch));
		
		if(optarg)
			wi_mutable_array_add_data(arguments, wi_string_with_cstring(optarg));
	}
	
	/* detach */
	if(daemonize) {
		wi_mutable_array_add_data(arguments, WI_STR("-X"));
		
		switch(wi_fork()) {
			case -1:
				wi_log_fatal(WI_STR("Could not fork: %m"));
				break;
				
			case 0:
				if(!wi_execv(wi_string_with_cstring(argv[0]), arguments))
					wi_log_fatal(WI_STR("Could not execute %s: %m"), argv[0]);
				break;
				
				default:
				_exit(0);
				break;
		}
	}
	
	wi_release(arguments);
	
	/* change directory */
	if(change_directory) {
		if(!wi_fs_change_directory(root_path))
			wi_log_error(WI_STR("Could not change directory to %@: %m"), root_path);
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_ssl_init();
	wt_clients_init();
	wt_servers_init();

	/* read the config file */
	wt_settings_init();

	if(!wt_settings_read_config())
		exit(1);

	/* apply settings */
	wt_settings_apply_settings();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	wi_log_info(WI_STR("Started as %@ %@"),
		wi_process_path(wi_process()),
		wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_tracker_init();

	/* switch user/group */
	if(switch_user)
		wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_signals_init();
	wt_block_signals();
	wt_servers_schedule();
	wt_tracker_create_threads();
	wt_write_pid();
	wt_write_status(true);
	
	/* clean up pool after startup */
	wi_pool_drain(pool);
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}
Beispiel #4
0
static void wd_tracker_register(wd_tracker_t *tracker) {
	wi_enumerator_t		*enumerator;
	wi_address_t		*address;
	wi_array_t			*arguments;
	wi_string_t			*ip, *string;
	uint32_t			message;
	wi_boolean_t		fatal = false;
	
	if(!wi_lock_trylock(tracker->register_lock))
		return;

	enumerator = wi_array_data_enumerator(tracker->addresses);
	
	while((address = wi_enumerator_next_data(enumerator))) {
		tracker->active		= false;
		tracker->address	= NULL;
		ip					= wi_address_string(address);
		
		wi_log_info(WI_STR("Trying %@ for tracker %@..."),
			ip, tracker->host);
		
		tracker->socket = wi_autorelease(wi_socket_init_with_address(wi_socket_alloc(), address, WI_SOCKET_TCP));

		if(!tracker->socket) {
			wi_log_err(WI_STR("Could not create socket for tracker %@: %m"),
				tracker->host);
			
			continue;
		}

		if(!wi_socket_connect(tracker->socket, 30.0)) {
			wi_log_err(WI_STR("Could not connect to tracker %@: %m"),
				tracker->host);
			
			continue;
		}
		
		if(!wi_socket_connect_tls(tracker->socket, tracker->tls, 30.0)) {
			wi_log_err(WI_STR("Could not connect to tracker %@: %m"),
				tracker->host);
			
			continue;
		}
		
		if(!wd_tracker_write(tracker, WI_STR("HELLO")))
			continue;
		
		if(!wd_tracker_read(tracker, &message, &arguments))
			continue;
		
		if(message != 200) {
			string = wi_array_components_joined_by_string(arguments, WI_STR(" "));
			wi_log_err(WI_STR("Could not register with tracker %@: Unexpected reply \"%u %@\""),
				tracker->host, message, string);
			
			fatal = true;
			continue;
		}
		
		if(!wd_tracker_write(tracker, WI_STR("CLIENT %#@"), wd_server_version_string))
			continue;
		
		if(!wd_tracker_write(tracker, WI_STR("REGISTER %#@%c%#@%c%#@%c%u%c%#@"),
							 tracker->category,			WD_FIELD_SEPARATOR,
							 wd_settings.url,			WD_FIELD_SEPARATOR,
							 wd_settings.name,			WD_FIELD_SEPARATOR,
							 wd_settings.bandwidth,		WD_FIELD_SEPARATOR,
							 wd_settings.description))
			continue;
		
		
		if(!wd_tracker_read(tracker, &message, &arguments))
			continue;
		
		if(message != 700 || wi_array_count(arguments) < 1) {
			string = wi_array_components_joined_by_string(arguments, WI_STR(" "));
			wi_log_err(WI_STR("Could not register with tracker %@: Unexpected reply \"%u %@\""),
				tracker->host, message, string);
			
			break;
		}
		
		wi_release(tracker->key);
		tracker->key = wi_retain(WI_ARRAY(arguments, 0));
		
		tracker->public_key = wi_retain(wi_socket_ssl_public_key(tracker->socket));

		if(!tracker->public_key) {
			wi_log_err(WI_STR("Could not get public key from the tracker %@: %m"),
				tracker->host);
			
			break;
		}
		
		wi_log_info(WI_STR("Registered with the tracker %@"),
			tracker->host);
		
		tracker->active		= true;
		tracker->address	= address;
		
		break;
	}
	
	wi_lock_unlock(tracker->register_lock);
}
Beispiel #5
0
static uint32_t wr_parse_message(wi_string_t *buffer) {
	wi_array_t		*arguments;
	wi_string_t		*string;
	uint32_t		message;
	
	string = wi_string_by_converting_encoding(buffer, wr_server_string_encoding, wr_client_string_encoding);

	wi_parse_wired_message(string, &message, &arguments);

	switch(message) {
		case 200:		wr_msg_200(arguments);		break;
		case 201:		wr_msg_201(arguments);		break;
		case 202:		wr_msg_202(arguments);		break;
		case 300:		wr_msg_300(arguments);		break;
		case 301:		wr_msg_301(arguments);		break;
		case 302:		wr_msg_302(arguments);		break;
		case 303:		wr_msg_303(arguments);		break;
		case 304:		wr_msg_304(arguments);		break;
		case 305:		wr_msg_305(arguments);		break;
		case 306:		wr_msg_306(arguments);		break;
		case 307:		wr_msg_307(arguments);		break;
		case 308:		wr_msg_308(arguments);		break;
		case 309:		wr_msg_309(arguments);		break;
		case 310:		wr_msg_310(arguments);		break;
		case 311:		wr_msg_311(arguments);		break;
		case 320:		wr_msg_320(arguments);		break;
		case 321:		wr_msg_321(arguments);		break;
		case 322:		wr_msg_322(arguments);		break;
		case 330:		wr_msg_330(arguments);		break;
		case 331:		wr_msg_331(arguments);		break;
		case 332:		wr_msg_332(arguments);		break;
		case 340:									break;
		case 341:		wr_msg_341(arguments);		break;
		case 400:		wr_msg_400(arguments);		break;
		case 401:		wr_msg_401(arguments);		break;
		case 402:		wr_msg_402(arguments);		break;
		case 410:		wr_msg_410(arguments);		break;
		case 411:		wr_msg_411(arguments);		break;
		case 420:		wr_msg_420(arguments);		break;
		case 421:		wr_msg_421(arguments);		break;
			
		case 500:
		case 501:
		case 502:
		case 503:
			wr_printf_prefix(WI_STR("%@: Command failed (%d)"),
				wr_last_command, message);
			break;

		case 510:
			wr_printf_prefix(WI_STR("%@: Login failed, wrong login or password"),
				wr_last_command);
			
			wr_disconnect();
			break;

		case 511:
			wr_printf_prefix(WI_STR("%@: Login failed, host is banned"),
				wr_last_command);

			wr_disconnect();
			break;

		case 512:
			wr_printf_prefix(WI_STR("%@: Client not found"),
				wr_last_command);
			break;

		case 513:
			wr_printf_prefix(WI_STR("%@: Account not found"),
				wr_last_command);
			break;

		case 514:
			wr_printf_prefix(WI_STR("%@: Account already exists"),
				wr_last_command);
			break;

		case 515:
			wr_printf_prefix(WI_STR("%@: User cannot be disconnected"),
				wr_last_command);
			break;

		case 516:
			wr_printf_prefix(WI_STR("%@: Permission denied"),
				wr_last_command);
			break;

		case 520:
			wr_printf_prefix(WI_STR("%@: File or directory not found"),
				wr_last_command);
			break;

		case 521:
			if(!wr_transfers_recursive_upload) {
				wr_printf_prefix(WI_STR("%@: File or directory already exists"),
					wr_last_command);
			}
			break;

		case 522:
			wr_printf_prefix(WI_STR("%@: Checksum mismatch"),
				wr_last_command);
			break;

		case 523:
			wr_printf_prefix(WI_STR("%@: Queue limit exceeded"),
				wr_last_command);
			break;
		
		case 602:									break;

		default:
			string = wi_array_components_joined_by_string(arguments, WI_STR(" "));
			
			wr_printf_prefix(WI_STR("%@: Unknown message: %d %@"),
				wr_last_command, message, string);
			break;
	}

	wr_received_bytes += wi_string_length(buffer);
	
	return message;
}
Beispiel #6
0
int main(int argc, const char **argv) {
	wi_pool_t			*pool;
	wi_string_t			*string;
	int					ch, facility;
	wi_boolean_t		no_chroot, test_config;

	/* init libwired */
	wi_initialize();
	wi_load(argc, argv);
	
	pool					= wi_pool_init(wi_pool_alloc());
	wi_log_startup			= true;
	wi_log_syslog			= true;
	wi_log_syslog_facility	= LOG_DAEMON;

	/* init core systems */
	wt_init_version();
	wt_status_lock			= wi_lock_init(wi_lock_alloc());
	wt_start_date			= wi_date_init(wi_date_alloc());
	
	/* set defaults */
	wi_root_path			= wi_string_init_with_cstring(wi_string_alloc(), WT_ROOT);
	wi_settings_config_path	= wi_string_init_with_cstring(wi_string_alloc(), WT_CONFIG_PATH);
	no_chroot				= false;
	test_config				= false;

	/* parse command line switches */
	while((ch = getopt(argc, (char * const *) argv, "46Dd:f:hi:L:ls:tuVv")) != -1) {
		switch(ch) {
			case '4':
				wt_address_family = WI_ADDRESS_IPV4;
				break;

			case '6':
				wt_address_family = WI_ADDRESS_IPV6;
				break;

			case 'D':
				wt_daemonize = false;
				wi_log_stderr = true;
				break;

			case 'd':
				wi_release(wi_root_path);
				wi_root_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'f':
				wi_release(wi_settings_config_path);
				wi_settings_config_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'i':
				wi_log_limit = wi_string_uint32(wi_string_with_cstring(optarg));
				break;

			case 'L':
				wi_log_syslog = false;
				wi_log_file = true;

				wi_release(wi_log_path);
				wi_log_path = wi_string_init_with_cstring(wi_string_alloc(), optarg);
				break;

			case 'l':
				wi_log_level++;
				break;

			case 's':
				string = wi_string_with_cstring(optarg);
				facility = wi_log_syslog_facility_with_name(string);
				
				if(facility < 0) {
					wi_log_err(WI_STR("Could not find syslog facility \"%@\": %m"),
						string);
				}
				
				wi_log_syslog_facility = facility;
				break;

			case 't':
				test_config = true;
				break;

			case 'u':
				no_chroot = true;
				break;

			case 'V':
			case 'v':
				wt_version();
				break;

			case '?':
			case 'h':
			default:
				wt_usage();
				break;
		}
	}
	
	/* open log */
	wi_log_open();

	/* init subsystems */
	wt_init_ssl();
	wt_init_clients();
	wt_init_servers();

	/* read the config file */
	wt_settings_chroot = !no_chroot;
	wt_init_settings();

	if(!wt_read_config())
		exit(1);

	/* change root directory */
	if(!no_chroot) {
		if(!wi_change_root())
			wi_log_err(WI_STR("Could not change root to %@: %m"), wi_root_path);
	}

	/* apply config */
	wt_apply_config();

	if(test_config) {
		printf("Config OK\n");

		exit(0);
	}

	/* dump command line */
	if(wi_log_level >= WI_LOG_DEBUG) {
		wi_log_debug(WI_STR("Started as %@ %@"),
			wi_process_path(wi_process()),
			wi_array_components_joined_by_string(wi_process_arguments(wi_process()), WI_STR(" ")));
	}

	/* init tracker */
	wi_log_info(WI_STR("Starting Wired Tracker version %@"), wt_version_string);
	wt_init_tracker();

	/* detach (don't chdir, don't close i/o channels) */
	if(wt_daemonize) {
		if(!wi_daemon())
			wi_log_err(WI_STR("Could not become a daemon: %m"));
	}

	/* switch user/group */
	wi_switch_user(wt_settings.user, wt_settings.group);
		
	/* create tracker threads after privilege drop */
	wt_init_signals();
	wt_block_signals();
	wt_schedule_servers();
	wt_fork_tracker();
	wt_write_pid();
	wt_write_status(true);
	wi_log_startup = false;
	
	wi_release(pool);
	pool = wi_pool_init(wi_pool_alloc());
	
	/* enter the signal handling thread in the main thread */
	wt_signal_thread(NULL);

	/* dropped out */
	wt_cleanup();
	wi_log_close();
	wi_release(pool);

	return 0;
}