Esempio n. 1
0
void
ndsctl_json(int fd)
{
	t_client *client;
	int indx;
	unsigned long int now, durationsecs = 0;
	unsigned long long int download_bytes, upload_bytes;

	now = time(NULL);

	/* Update the client's counters so info is current */
	iptables_fw_counters_update();

	LOCK_CLIENT_LIST();

	cprintf(fd, "{\n\"client_length\": %d,\n", get_client_list_length());

	client = client_get_first_client();
	indx = 0;

	cprintf(fd, "\"clients\":{\n");

	while (client != NULL) {
		cprintf(fd, "\"%s\":{\n", client->mac);
		cprintf(fd, "\"client_id\":%d,\n", indx);
		cprintf(fd, "\"ip\":\"%s\",\n\"mac\":\"%s\",\n", client->ip, client->mac);
		cprintf(fd, "\"added\":%lld,\n", (long long) client->added_time);
		cprintf(fd, "\"active\":%lld,\n", (long long) client->counters.last_updated);
		cprintf(fd, "\"duration\":%lu,\n", now - client->added_time);
		cprintf(fd, "\"token\":\"%s\",\n", client->token ? client->token : "none");
		cprintf(fd, "\"state\":\"%s\",\n", fw_connection_state_as_string(client->fw_connection_state));

		durationsecs = now - client->added_time;
		download_bytes = client->counters.incoming;
		upload_bytes = client->counters.outgoing;

		cprintf(fd, "\"downloaded\":\"%llu\",\n\"avg_down_speed\":\"%.6g\",\n\"uploaded\":\"%llu\",\n\"avg_up_speed\":\"%.6g\"\n",
				download_bytes/1000, ((double)download_bytes)/125/durationsecs,
				upload_bytes/1000, ((double)upload_bytes)/125/durationsecs);

		indx++;
		client = client->next;

		cprintf(fd, "}");
		if(client) {
			cprintf(fd, ",\n");
		}
	}

	cprintf(fd, "}}" );

	UNLOCK_CLIENT_LIST();
}
Esempio n. 2
0
/** @internal */
int
_iptables_init_marks()
{
	/* Check FW_MARK values are distinct.  */
	if (FW_MARK_BLOCKED == FW_MARK_TRUSTED ||
			FW_MARK_TRUSTED == FW_MARK_AUTHENTICATED ||
			FW_MARK_AUTHENTICATED == FW_MARK_BLOCKED) {
		debug(LOG_ERR, "FW_MARK_BLOCKED, FW_MARK_TRUSTED, FW_MARK_AUTHENTICATED not distinct values.");
		return -1;
	}

	/* Check FW_MARK values nonzero.  */
	if (FW_MARK_BLOCKED == 0 ||
			FW_MARK_TRUSTED == 0 ||
			FW_MARK_AUTHENTICATED == 0) {
		debug(LOG_ERR, "FW_MARK_BLOCKED, FW_MARK_TRUSTED, FW_MARK_AUTHENTICATED not all nonzero.");
		return -1;
	}

	FW_MARK_PREAUTHENTICATED = 0;  /* always 0 */
	/* FW_MARK_MASK is bitwise OR of other marks */
	FW_MARK_MASK = FW_MARK_BLOCKED | FW_MARK_TRUSTED | FW_MARK_AUTHENTICATED;

	debug(LOG_INFO,"Iptables mark %s: 0x%x",
		  fw_connection_state_as_string(FW_MARK_PREAUTHENTICATED),
		  FW_MARK_PREAUTHENTICATED);
	debug(LOG_INFO,"Iptables mark %s: 0x%x",
		  fw_connection_state_as_string(FW_MARK_AUTHENTICATED),
		  FW_MARK_AUTHENTICATED);
	debug(LOG_INFO,"Iptables mark %s: 0x%x",
		  fw_connection_state_as_string(FW_MARK_TRUSTED),
		  FW_MARK_TRUSTED);
	debug(LOG_INFO,"Iptables mark %s: 0x%x",
		  fw_connection_state_as_string(FW_MARK_BLOCKED),
		  FW_MARK_BLOCKED);

	return 0;
}
Esempio n. 3
0
void
ndsctl_clients(int fd)
{
	t_client *client;
	int indx;
	unsigned long int now, durationsecs = 0;
	unsigned long long int download_bytes, upload_bytes;

	now = time(NULL);

	/* Update the client's counters so info is current */
	iptables_fw_counters_update();

	LOCK_CLIENT_LIST();

	cprintf(fd, "%d\n", get_client_list_length());

	client = client_get_first_client();
	if(client) {
		cprintf(fd, "\n");
	}

	indx = 0;
	while (client != NULL) {
		cprintf(fd, "client_id=%d\n", indx);
		cprintf(fd, "ip=%s\nmac=%s\n", client->ip, client->mac);
		cprintf(fd, "added=%lld\n", (long long) client->added_time);
		cprintf(fd, "active=%lld\n", (long long) client->counters.last_updated);
		cprintf(fd, "duration=%lu\n", now - client->added_time);
		cprintf(fd, "token=%s\n", client->token ? client->token : "none");
		cprintf(fd, "state=%s\n", fw_connection_state_as_string(client->fw_connection_state));

		durationsecs = now - client->added_time;
		download_bytes = client->counters.incoming;
		upload_bytes = client->counters.outgoing;

		cprintf(fd, "downloaded=%llu\navg_down_speed=%.6g\nuploaded=%llu\navg_up_speed=%.6g\n\n",
				download_bytes/1000, ((double)download_bytes)/125/durationsecs,
				upload_bytes/1000, ((double)upload_bytes)/125/durationsecs);

		indx++;
		client = client->next;
	}

	UNLOCK_CLIENT_LIST();
}
Esempio n. 4
0
/*
 * @return A string containing json clients list.
 */
char *
get_clients_json(void)
{
	char buffer[STATUS_BUF_SIZ];
	ssize_t len;
	t_client *client;
	int	   indx;
	unsigned long int now, durationsecs = 0;
	unsigned long long int download_bytes, upload_bytes;

	now = time(NULL);
	len = 0;

	/* Update the client's counters so info is current */
	iptables_fw_counters_update();

	LOCK_CLIENT_LIST();

	snprintf((buffer + len), (sizeof(buffer) - len), "{\n\"client_length\": %d,\n", get_client_list_length());
	len = strlen(buffer);

	client = client_get_first_client();
	indx = 0;

	snprintf((buffer + len), (sizeof(buffer) - len), "\"clients\":{\n");
	len = strlen(buffer);
	
	while (client != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\"%s\":{\n", client->mac);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"client_id\":%d,\n", indx);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"ip\":\"%s\",\n\"mac\":\"%s\",\n", client->ip, client->mac);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"added\":%lld,\n", (long long) client->added_time);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"active\":%lld,\n", (long long) client->counters.last_updated);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"duration\":%lu,\n", now - client->added_time);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"token\":\"%s\",\n", client->token ? client->token : "none");
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "\"state\":\"%s\",\n",
				 fw_connection_state_as_string(client->fw_connection_state));
		len = strlen(buffer);

		durationsecs = now - client->added_time;
		download_bytes = client->counters.incoming;
		upload_bytes = client->counters.outgoing;

		snprintf((buffer + len), (sizeof(buffer) - len),
				 "\"downloaded\":\"%llu\",\n\"avg_down_speed\":\"%.6g\",\n\"uploaded\":\"%llu\",\n\"avg_up_speed\":\"%.6g\"\n",
				 download_bytes/1000, ((double)download_bytes)/125/durationsecs,
				 upload_bytes/1000, ((double)upload_bytes)/125/durationsecs);
		len = strlen(buffer);

		indx++;
		client = client->next;

		snprintf((buffer + len), (sizeof(buffer) - len), "}");
		len = strlen(buffer);

		if(client) {
			snprintf((buffer + len), (sizeof(buffer) - len), ",\n");
			len = strlen(buffer);
		}

	}

	snprintf((buffer + len), (sizeof(buffer) - len), "}}" );
	len = strlen(buffer);

	UNLOCK_CLIENT_LIST();

	return safe_strdup(buffer);
}
Esempio n. 5
0
/*
 * @return A string containing human-readable status text.
 * MUST BE free()d by caller
 */
char *
get_status_text()
{
	char buffer[STATUS_BUF_SIZ];
	char timebuf[32];
	char * str;
	ssize_t len;
	s_config *config;
	t_client *client;
	int	   indx;
	unsigned long int now, uptimesecs, durationsecs = 0;
	unsigned long long int download_bytes, upload_bytes;
	t_MAC *trust_mac;
	t_MAC *allow_mac;
	t_MAC *block_mac;

	config = config_get_config();

	len = 0;
	snprintf(buffer, (sizeof(buffer) - len), "==================\nNoDogSplash Status\n====\n");
	len = strlen(buffer);

	now = time(NULL);
	uptimesecs = now - started_time;

	snprintf((buffer + len), (sizeof(buffer) - len), "Version: " VERSION "\n");
	len = strlen(buffer);

	str = format_time(uptimesecs);
	snprintf((buffer + len), (sizeof(buffer) - len), "Uptime: %s\n", str);
	len = strlen(buffer);
	free(str);

	snprintf((buffer + len), (sizeof(buffer) - len), "Gateway Name: %s\n", config->gw_name);
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Managed interface: %s\n", config->gw_interface);
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Managed IP range: %s\n", config->gw_iprange);
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Server listening: %s:%d\n",
			 config->gw_address, config->gw_port);
	len = strlen(buffer);

	if(config->authenticate_immediately) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Authenticate immediately: yes\n");
		len = strlen(buffer);

	} else {
		snprintf((buffer + len), (sizeof(buffer) - len), "Splashpage: %s/%s\n",
				 config->webroot, config->splashpage);
		len = strlen(buffer);
	}

	if(config->redirectURL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Redirect URL: %s\n",
				 config->redirectURL);
		len = strlen(buffer);
	}

	if(config->passwordauth) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Gateway password: %s\n",
				 config->password);
		len = strlen(buffer);
	}

	if(config->usernameauth) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Gateway username: %s\n",
				 config->username);
		len = strlen(buffer);
	}

	snprintf((buffer + len), (sizeof(buffer) - len), "Traffic control: %s\n", config->traffic_control ? "yes" : "no");
	len = strlen(buffer);

	if(config->traffic_control) {
		if(config->download_limit > 0) {
			snprintf((buffer + len), (sizeof(buffer) - len), "Download rate limit: %d kbit/s\n", config->download_limit);
			len = strlen(buffer);
		} else {
			snprintf((buffer + len), (sizeof(buffer) - len), "Download rate limit: none\n");
			len = strlen(buffer);
		}
		if(config->upload_limit > 0) {
			snprintf((buffer + len), (sizeof(buffer) - len), "Upload rate limit: %d kbit/s\n", config->upload_limit);
			len = strlen(buffer);
		} else {
			snprintf((buffer + len), (sizeof(buffer) - len), "Upload rate limit: none\n");
			len = strlen(buffer);
		}
	}

	download_bytes = iptables_fw_total_download();
	snprintf((buffer + len), (sizeof(buffer) - len), "Total download: %llu kByte", download_bytes/1000);
	len = strlen(buffer);
	snprintf((buffer + len), (sizeof(buffer) - len), "; avg: %.6g kbit/s\n", ((double) download_bytes) / 125 / uptimesecs);
	len = strlen(buffer);

	upload_bytes = iptables_fw_total_upload();
	snprintf((buffer + len), (sizeof(buffer) - len), "Total upload: %llu kByte", upload_bytes/1000);
	len = strlen(buffer);
	snprintf((buffer + len), (sizeof(buffer) - len), "; avg: %.6g kbit/s\n", ((double) upload_bytes) / 125 / uptimesecs);
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "====\n");
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Client authentications since start: %u\n", authenticated_since_start);
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Httpd request threads created/current: %d/%d\n", created_httpd_threads, current_httpd_threads);
	len = strlen(buffer);

	if(config->decongest_httpd_threads) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Httpd thread decongest threshold: %d threads\n", config->httpd_thread_threshold);
		len = strlen(buffer);
		snprintf((buffer + len), (sizeof(buffer) - len), "Httpd thread decongest delay: %d ms\n", config->httpd_thread_delay_ms);
		len = strlen(buffer);
	}

	/* Update the client's counters so info is current */
	iptables_fw_counters_update();

	LOCK_CLIENT_LIST();

	snprintf((buffer + len), (sizeof(buffer) - len), "Current clients: %d\n", get_client_list_length());
	len = strlen(buffer);

	client = client_get_first_client();
	if(client) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\n");
		len = strlen(buffer);
	}
	indx = 0;
	while (client != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "Client %d\n", indx);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "  IP: %s MAC: %s\n", client->ip, client->mac);
		len = strlen(buffer);

		ctime_r(&(client->added_time),timebuf);
		snprintf((buffer + len), (sizeof(buffer) - len), "  Added:   %s", timebuf);
		len = strlen(buffer);

		ctime_r(&(client->counters.last_updated),timebuf);
		snprintf((buffer + len), (sizeof(buffer) - len), "  Active:  %s", timebuf);
		len = strlen(buffer);

		str = format_time(client->counters.last_updated - client->added_time);
		snprintf((buffer + len), (sizeof(buffer) - len), "  Active duration: %s\n", str);
		len = strlen(buffer);
		free(str);

		durationsecs = now - client->added_time;

		str = format_time(durationsecs);
		snprintf((buffer + len), (sizeof(buffer) - len), "  Added duration:  %s\n", str);
		len = strlen(buffer);
		free(str);

		snprintf((buffer + len), (sizeof(buffer) - len), "  Token: %s\n", client->token ? client->token : "none");
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "  State: %s\n",
				 fw_connection_state_as_string(client->fw_connection_state));
		len = strlen(buffer);

		download_bytes = client->counters.incoming;
		upload_bytes = client->counters.outgoing;

		snprintf((buffer + len), (sizeof(buffer) - len),
				 "  Download: %llu kByte; avg: %.6g kbit/s\n  Upload:   %llu kByte; avg: %.6g kbit/s\n\n",
				 download_bytes/1000, ((double)download_bytes)/125/durationsecs,
				 upload_bytes/1000, ((double)upload_bytes)/125/durationsecs);
		len = strlen(buffer);

		indx++;
		client = client->next;
	}

	UNLOCK_CLIENT_LIST();

	snprintf((buffer + len), (sizeof(buffer) - len), "====\n");
	len = strlen(buffer);

	snprintf((buffer + len), (sizeof(buffer) - len), "Blocked MAC addresses:");
	len = strlen(buffer);

	if(config->macmechanism == MAC_ALLOW) {
		snprintf((buffer + len), (sizeof(buffer) - len), " N/A\n");
		len = strlen(buffer);
	} else  if (config->blockedmaclist != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\n");
		len = strlen(buffer);
		for (block_mac = config->blockedmaclist; block_mac != NULL; block_mac = block_mac->next) {
			snprintf((buffer + len), (sizeof(buffer) - len), "  %s\n", block_mac->mac);
			len = strlen(buffer);
		}
	} else {
		snprintf((buffer + len), (sizeof(buffer) - len), " none\n");
		len = strlen(buffer);
	}

	snprintf((buffer + len), (sizeof(buffer) - len), "Allowed MAC addresses:");
	len = strlen(buffer);

	if(config->macmechanism == MAC_BLOCK) {
		snprintf((buffer + len), (sizeof(buffer) - len), " N/A\n");
		len = strlen(buffer);
	} else  if (config->allowedmaclist != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\n");
		len = strlen(buffer);
		for (allow_mac = config->allowedmaclist; allow_mac != NULL; allow_mac = allow_mac->next) {
			snprintf((buffer + len), (sizeof(buffer) - len), "  %s\n", allow_mac->mac);
			len = strlen(buffer);
		}
	} else {
		snprintf((buffer + len), (sizeof(buffer) - len), " none\n");
		len = strlen(buffer);
	}

	snprintf((buffer + len), (sizeof(buffer) - len), "Trusted MAC addresses:");
	len = strlen(buffer);

	if (config->trustedmaclist != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\n");
		len = strlen(buffer);
		for (trust_mac = config->trustedmaclist; trust_mac != NULL; trust_mac = trust_mac->next) {
			snprintf((buffer + len), (sizeof(buffer) - len), "  %s\n", trust_mac->mac);
			len = strlen(buffer);
		}
	} else {
		snprintf((buffer + len), (sizeof(buffer) - len), " none\n");
		len = strlen(buffer);
	}

	snprintf((buffer + len), (sizeof(buffer) - len), "========\n");
	len = strlen(buffer);

	return safe_strdup(buffer);
}
Esempio n. 6
0
/*
 * @return A string containing machine-readable clients list.
 * MUST BE free()d by caller
 */
char * get_clients_text()
{
	char buffer[STATUS_BUF_SIZ];
	ssize_t len;
	t_client *client;
	int	   indx;
	unsigned long int now, durationsecs = 0;
	unsigned long long int download_bytes, upload_bytes;

	now = time(NULL);
	len = 0;

	/* Update the client's counters so info is current */
	iptables_fw_counters_update();

	LOCK_CLIENT_LIST();

	snprintf((buffer + len), (sizeof(buffer) - len), "%d\n", get_client_list_length());
	len = strlen(buffer);

	client = client_get_first_client();
	if(client) {
		snprintf((buffer + len), (sizeof(buffer) - len), "\n");
		len = strlen(buffer);
	}
	indx = 0;
	while (client != NULL) {
		snprintf((buffer + len), (sizeof(buffer) - len), "client_id=%d\n", indx);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "ip=%s\nmac=%s\n", client->ip, client->mac);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "added=%d\n", client->added_time);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "active=%d\n", client->counters.last_updated);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "duration=%d\n", now - client->added_time);
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "token=%s\n", client->token ? client->token : "none");
		len = strlen(buffer);

		snprintf((buffer + len), (sizeof(buffer) - len), "state=%s\n",
				 fw_connection_state_as_string(client->fw_connection_state));
		len = strlen(buffer);

		durationsecs = now - client->added_time;
		download_bytes = client->counters.incoming;
		upload_bytes = client->counters.outgoing;

		snprintf((buffer + len), (sizeof(buffer) - len),
				 "downloaded=%llu\navg_down_speed=%.6g\nuploaded=%llu\navg_up_speed=%.6g\n\n",
				 download_bytes/1000, ((double)download_bytes)/125/durationsecs,
				 upload_bytes/1000, ((double)upload_bytes)/125/durationsecs);
		len = strlen(buffer);

		indx++;
		client = client->next;
	}

	UNLOCK_CLIENT_LIST();

	return safe_strdup(buffer);
}