Example #1
0
int
main(int argc, char **argv)
{
	/* Init configuration */
	init_config();
	parse_commandline(argc, argv);

	switch(config.command) {
	case NDSCTL_STATUS:
		ndsctl_status();
		break;

	case NDSCTL_CLIENTS:
		ndsctl_clients();
		break;

	case NDSCTL_STOP:
		ndsctl_stop();
		break;

	case NDSCTL_BLOCK:
		ndsctl_block();
		break;

	case NDSCTL_UNBLOCK:
		ndsctl_unblock();
		break;

	case NDSCTL_ALLOW:
		ndsctl_allow();
		break;

	case NDSCTL_UNALLOW:
		ndsctl_unallow();
		break;

	case NDSCTL_TRUST:
		ndsctl_trust();
		break;

	case NDSCTL_UNTRUST:
		ndsctl_untrust();
		break;

	case NDSCTL_AUTH:
		ndsctl_auth();
		break;

	case NDSCTL_DEAUTH:
		ndsctl_deauth();
		break;

	case NDSCTL_LOGLEVEL:
		ndsctl_loglevel();
		break;

	case NDSCTL_PASSWORD:
		ndsctl_password();
		break;

	case NDSCTL_USERNAME:
		ndsctl_username();
		break;

	default:
		/* XXX NEVER REACHED */
		fprintf(stderr, "Unknown opcode: %d\n", config.command);
		exit(1);
		break;
	}
	exit(0);
}
static void *
thread_ndsctl_handler(void *arg)
{
	int done, i;
	char request[MAX_BUF];
	ssize_t read_bytes, len;
	struct ndsctl_args *args = arg;
	pthread_t ndsctl_master = args->ndsctl_master_id;
	int fd = args->fd;
	free(args);

	debug(LOG_DEBUG, "Entering thread_ndsctl_handler....");

	debug(LOG_DEBUG, "Read bytes and stuff from %d", fd);

	/* Init variables */
	read_bytes = 0;
	done = 0;
	memset(request, 0, sizeof(request));

	/* Read.... */
	while (!done && read_bytes < (sizeof(request) - 1)) {
		len = read(fd, request + read_bytes, sizeof(request) - read_bytes);

		/* Have we gotten a command yet? */
		for (i = read_bytes; i < (read_bytes + len); i++) {
			if (request[i] == '\r' || request[i] == '\n') {
				request[i] = '\0';
				done = 1;
			}
		}

		/* Increment position */
		read_bytes += len;
	}

	debug(LOG_DEBUG, "ndsctl request received: [%s]", request);

	if (strncmp(request, "status", 6) == 0) {
		ndsctl_status(fd);
	} else if (strncmp(request, "clients", 7) == 0) {
		ndsctl_clients(fd);
	} else if (strncmp(request, "json", 4) == 0) {
		ndsctl_json(fd);
	} else if (strncmp(request, "stop", 4) == 0) {
		ndsctl_stop(ndsctl_master);
	} else if (strncmp(request, "block", 5) == 0) {
		ndsctl_block(fd, (request + 6));
	} else if (strncmp(request, "unblock", 7) == 0) {
		ndsctl_unblock(fd, (request + 8));
	} else if (strncmp(request, "allow", 5) == 0) {
		ndsctl_allow(fd, (request + 6));
	} else if (strncmp(request, "unallow", 7) == 0) {
		ndsctl_unallow(fd, (request + 8));
	} else if (strncmp(request, "trust", 5) == 0) {
		ndsctl_trust(fd, (request + 6));
	} else if (strncmp(request, "untrust", 7) == 0) {
		ndsctl_untrust(fd, (request + 8));
	} else if (strncmp(request, "auth", 4) == 0) {
		ndsctl_auth(fd, (request + 5));
	} else if (strncmp(request, "deauth", 6) == 0) {
		ndsctl_deauth(fd, (request + 7));
	} else if (strncmp(request, "loglevel", 8) == 0) {
		ndsctl_loglevel(fd, (request + 9));
	} else if (strncmp(request, "password", 8) == 0) {
		ndsctl_password(fd, (request + 9));
	} else if (strncmp(request, "username", 8) == 0) {
		ndsctl_username(fd, (request + 9));
	}

	if (!done) {
		debug(LOG_ERR, "Invalid ndsctl request.");
		shutdown(fd, 2);
		close(fd);
		pthread_exit(NULL);
	}

	debug(LOG_DEBUG, "ndsctl request processed: [%s]", request);

	shutdown(fd, 2);
	close(fd);
	debug(LOG_DEBUG, "Exiting thread_ndsctl_handler....");

	return NULL;
}