Commit Diff


commit - ed19b3aa6d26aef32ca6bc590c1fc3ebbc2b9fdc
commit + 04e3345d9c84c27f06fa0c736448c1c858172b99
blob - a0362c09395f2cc2b753fafe550703313c6c5934
blob + e9994688ec888a3fab9bafba2e88f57403f1647e
--- thingsctl/parser.c	
+++ thingsctl/parser.c	
@@ -37,6 +37,7 @@
 enum token_type {
 	NOTOKEN,
 	ENDTOKEN,
+	CLIENTNAME,
 	THINGNAME,
 	KEYWORD
 };
@@ -49,16 +50,18 @@ struct token {
 };
 
 static const struct token t_list[];
-static const struct token t_list_things[];
 static const struct token t_log[];
 static const struct token t_main[];
 static const struct token t_show[];
+static const struct token t_client_name[];
+static const struct token t_thing_name[];
 
 static const struct token t_main[] = {
 	{KEYWORD,	"reload",	RELOAD,		NULL},
 	{KEYWORD,	"list",		LIST,		t_list},
 	{KEYWORD,	"show",		SHOW,		t_show},
 	{KEYWORD,	"log",		NONE,		t_log},
+	{KEYWORD,	"kill",		KILL_CLIENT,	t_client_name},
 	{ENDTOKEN,	"",		NONE,		NULL}
 };
 
@@ -70,22 +73,29 @@ static const struct token t_log[] = {
 };
 
 static const struct token t_list[] = {
-	{KEYWORD,	"things",	LIST_THINGS,	t_list_things},
+	{KEYWORD,	"clients",	LIST_CLIENTS,	NULL},
+	{KEYWORD,	"things",	LIST_THINGS,	NULL},
+	{KEYWORD,	"sockets",	LIST_SOCKETS,	NULL},
 	{ENDTOKEN,	"",		NONE,		NULL}
 };
 
 static const struct token t_show[] = {
 	{KEYWORD,	"parent",	SHOW_PARENT,	NULL},
 	{KEYWORD,	"control",	SHOW_CONTROL,	NULL},
+	{KEYWORD,	"thing",	SHOW_PACKETS,	t_thing_name},
 	{ENDTOKEN,	"",		NONE,		NULL}
 };
 
-static const struct token t_list_things[] = {
-	{NOTOKEN,	"",		NONE,		NULL},
+static const struct token t_thing_name[] = {
 	{THINGNAME,	"",		LIST_THINGS,	NULL},
 	{ENDTOKEN,	"",		NONE,		NULL}
 };
 
+static const struct token t_client_name[] = {
+	{CLIENTNAME,	"",		NONE,		NULL},
+	{ENDTOKEN,	"",		NONE,		NULL}
+};
+
 static const struct token *match_token(const char *, const struct token *,
     struct parse_result *);
 static void show_valid_args(const struct token *);
@@ -142,6 +152,7 @@ match_token(const char *word, const struct token *tabl
 			}
 			break;
 		case THINGNAME:
+		case CLIENTNAME:
 			if (!match && word != NULL && strlen(word) > 0) {
 				memset(res->name, 0,
 				    sizeof(res->name));
@@ -193,8 +204,11 @@ show_valid_args(const struct token *table)
 			fprintf(stderr, "  <cr>\n");
 			break;
 		case THINGNAME:
-			fprintf(stderr, " <thing>\n");
+			fprintf(stderr, " <thing_name>\n");
 			break;
+		case CLIENTNAME:
+			fprintf(stderr, " <client_name>\n");
+			break;
 		case KEYWORD:
 			fprintf(stderr, "  %s\n", table[i].keyword);
 			break;
blob - 4b025c82e5266d19c384e4391442c67c32db7ea9
blob + 95c5eb620625000f4901e4c1f75483ce08616a11
--- thingsctl/parser.h	
+++ thingsctl/parser.h	
@@ -17,15 +17,19 @@
 
 enum actions {
 	NONE,
+	KILL_CLIENT,
 	LIST,
+	LIST_CLIENTS,
+	LIST_SOCKETS,
 	LIST_THINGS,
+	LOG_BRIEF,
 	LOG_DEBUG,
 	LOG_VERBOSE,
-	LOG_BRIEF,
+	RELOAD,
 	SHOW,
 	SHOW_CONTROL,
+	SHOW_PACKETS,
 	SHOW_PARENT,
-	RELOAD
 };
 
 struct parse_result {
blob - 2c3c4ffa0cb484c10bf40f20463b54b683bdb4bb
blob + 293dcfd3719603d8f6004d52d3764f78f9f28230
--- thingsctl/thingsctl.c	
+++ thingsctl/thingsctl.c	
@@ -42,6 +42,8 @@
 
 __dead void	 usage(void);
 int		 list_things_msg(struct imsg *);
+int		 list_clients_msg(struct imsg *);
+int		 list_sockets_msg(struct imsg *);
 int		 show_parent_msg(struct imsg *);
 int		 show_control_msg(struct imsg *);
 
@@ -105,8 +107,36 @@ main(int argc, char *argv[])
 	imsg_init(ibuf, ctl_sock);
 	done = 0;
 
+	/* Check for root only actions */
+	switch (res->action) {
+	case LOG_DEBUG:
+	case LOG_VERBOSE:
+	case LOG_BRIEF:
+	case KILL_CLIENT:
+	case SHOW_PACKETS:
+		if (geteuid() != 0)
+			errx(1, "need root privileges");
+		break;
+	default:
+		break;
+	}
+
 	/* Process user request. */
 	switch (res->action) {
+	case KILL_CLIENT:
+		imsg_compose(ibuf, IMSG_KILL_CLIENT, 0, 0, -1,
+		    res->name, strlen(res->name));
+		printf("\nKill request sent for client '%s'.\n", res->name);
+		done = 1;
+		break;
+	case LIST_CLIENTS:
+		imsg_compose(ibuf, IMSG_GET_INFO_CLIENTS_REQUEST, 0,
+		    0, -1, NULL, 0);
+		break;
+	case LIST_SOCKETS:
+		imsg_compose(ibuf, IMSG_GET_INFO_SOCKETS_REQUEST, 0,
+		    0, -1, NULL, 0);
+		break;
 	case LIST_THINGS:
 		imsg_compose(ibuf, IMSG_GET_INFO_THINGS_REQUEST, 0,
 		    0, -1, res->name, sizeof(res->name));
@@ -120,13 +150,17 @@ main(int argc, char *argv[])
 	case LOG_BRIEF:
 		imsg_compose(ibuf, IMSG_CTL_VERBOSE, 0, 0, -1,
 		    &verbose, sizeof(verbose));
-		printf("logging request sent.\n");
+		printf("\nLogging request sent.\n");
 		done = 1;
 		break;
 	case SHOW_CONTROL:
 		imsg_compose(ibuf, IMSG_GET_INFO_CONTROL_REQUEST, 0,
 		    0, -1, NULL, 0);
 		break;
+	case SHOW_PACKETS:
+		imsg_compose(ibuf, IMSG_GET_INFO_PARENT_REQUEST, 0,
+		    0, -1, NULL, 0);
+		break;
 	case SHOW_PARENT:
 		imsg_compose(ibuf, IMSG_GET_INFO_PARENT_REQUEST, 0,
 		    0, -1, NULL, 0);
@@ -167,21 +201,31 @@ main(int argc, char *argv[])
 				break;
 
 			switch (res->action) {
+			case LIST_CLIENTS:
+				done = list_clients_msg(&imsg);
+				break;
+			case LIST_SOCKETS:
+				done = list_sockets_msg(&imsg);
+				break;
 			case LIST_THINGS:
 				done = list_things_msg(&imsg);
 				break;
-			case SHOW_PARENT:
-				done = show_parent_msg(&imsg);
-				break;
 			case SHOW_CONTROL:
 				done = show_control_msg(&imsg);
 				break;
+			case SHOW_PACKETS:
+				done = 1;
+				break;
+			case SHOW_PARENT:
+				done = show_parent_msg(&imsg);
+				break;
 			default:
 				break;
 			}
 			imsg_free(&imsg);
 		}
 	}
+	printf("\n");
 	close(ctl_sock);
 	free(ibuf);
 
@@ -196,14 +240,14 @@ show_parent_msg(struct imsg *imsg)
 	switch (imsg->hdr.type) {
 	case IMSG_GET_INFO_PARENT_DATA:
 		npi = imsg->data;
-		printf("Parent says: Logging level is ");
+		printf("\nParent says: Logging level is ");
 		if (npi->verbose == 2)
 			printf("debug");
 		else if (npi->verbose == 1)
 			printf("verbose");
 		else
 			printf("brief");
-		printf(" (%d)\n", npi->verbose);
+		printf(" (%d).\n", npi->verbose);
 		break;
 	case IMSG_GET_INFO_PARENT_END_DATA:
 	case IMSG_CTL_END:
@@ -223,14 +267,14 @@ show_control_msg(struct imsg *imsg)
 	switch (imsg->hdr.type) {
 	case IMSG_GET_INFO_CONTROL_DATA:
 		nci = imsg->data;
-		printf("Control says: Logging level is ");
+		printf("\nControl says: Logging level is ");
 		if (nci->verbose == 2)
 			printf("debug");
 		else if (nci->verbose == 1)
 			printf("verbose");
 		else
 			printf("brief");
-		printf(" (%d)\n", nci->verbose);
+		printf(" (%d).\n", nci->verbose);
 		break;
 	case IMSG_GET_INFO_CONTROL_END_DATA:
 	case IMSG_CTL_END:
@@ -245,13 +289,12 @@ show_control_msg(struct imsg *imsg)
 int
 list_things_msg(struct imsg *imsg)
 {
-	struct thing *nti;
+	struct thing	*nti;
 
 	switch (imsg->hdr.type) {
 	case IMSG_GET_INFO_THINGS_DATA:
 		nti = (struct thing *) imsg->data;
 
-
 		printf("\nThing Name:\t\t\t%s\n", nti->name);
 		switch(nti->type) {
 		case TCP:
@@ -303,3 +346,66 @@ list_things_msg(struct imsg *imsg)
 
 	return (0);
 }
+
+int
+list_clients_msg(struct imsg *imsg)
+{
+	struct client	*nci;
+
+	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_CLIENTS_DATA:
+		nci = (struct client *) imsg->data;
+
+		if (nci->subscribed == false)
+			break;
+
+		printf("\nClient Name:\t\t\t%s\n", nci->name);
+		printf("\tfd:\t\t\t%d\n", nci->fd);
+		printf("\tPort:\t\t\t%d\n", nci->port);
+		if (nci->tls == true)
+			printf("\tTLS:\t\t\tyes\n");
+		printf("\tSubscriptions:\t\t%zu\n", nci->subs);
+		break;
+	case IMSG_GET_INFO_CLIENTS_END_DATA:
+	case IMSG_CTL_END:
+		return (1);
+	default:
+		break;
+	}
+
+	return (0);
+}
+
+int
+list_sockets_msg(struct imsg *imsg)
+{
+	struct socket	*nsi;
+
+	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_SOCKETS_DATA:
+		nsi = (struct socket *) imsg->data;
+
+		printf("\n");
+		if (strcmp(nsi->name, "") == 0)
+			printf("Socket Name:\t\t\tReceive socket\n");
+		else
+			printf("Socket Name:\t\t\t%s\n", nsi->name);
+		printf("\tfd:\t\t\t%d\n", nsi->fd);
+		printf("\tPort:\t\t\t%d\n", nsi->port);
+		if (nsi->tls == true)
+			printf("\tTLS:\t\t\tyes\n\n");
+		printf("\tClient Count:\t\t%zu\n", nsi->client_cnt);
+		if (nsi->max_clients == 0)
+			printf("\tMax Clients:\t\tunlimited\n");
+		else
+			printf("\tMax Clients:\t\t%zu\n", nsi->max_clients);
+		break;
+	case IMSG_GET_INFO_SOCKETS_END_DATA:
+	case IMSG_CTL_END:
+		return (1);
+	default:
+		break;
+	}
+
+	return (0);
+}
blob - f8720c8bec5591a7b8ae06f39d75f195539a60e1
blob + 39ee502f264439054a6584d7c3fc9bff18ad956b
--- thingsd/client.c	
+++ thingsd/client.c	
@@ -478,3 +478,52 @@ start_client_chk(struct thingsd *env)
 		client_chk((void *) env);
 	}
 }
+
+void
+clients_show_info(struct privsep *ps, struct imsg *imsg)
+{
+	char filter[THINGSD_MAXTHINGNAME];
+	struct client	*client, nci;
+	size_t		 n;
+
+	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_CLIENTS_REQUEST:
+
+		memcpy(filter, imsg->data, sizeof(filter));
+
+		TAILQ_FOREACH(client, thingsd_env->clients, entry) {
+			/* if (filter[0] == '\0' || memcmp(filter, */
+			/*     client->name, sizeof(filter)) == 0) { */
+
+				n = strlcpy(nci.name, client->name,
+				    sizeof(nci.name));
+				if (n >= sizeof(nci.name))
+					fatalx("%s: nci.name too long",
+					    __func__);
+
+				nci.subscribed = client->subscribed;
+				nci.fd = client->fd;
+				nci.port = client->port;
+				nci.tls = client->tls;
+				nci.subs = client->subs;
+
+				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+				    IMSG_GET_INFO_CLIENTS_DATA,
+				    imsg->hdr.peerid, -1, &nci,
+				    sizeof(nci)) == -1)
+					return;
+
+			/* } */
+		}
+
+		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+		    IMSG_GET_INFO_CLIENTS_END_DATA, imsg->hdr.peerid,
+			    -1, &nci, sizeof(nci)) == -1)
+				return;
+
+		break;
+	default:
+		log_debug("%s: error handling imsg", __func__);
+		break;
+	}
+}
blob - 89acbb127f1ddbcd515113dc116d59d99307e79d
blob + c3171e64eba9b219637c50e2c1f4dd3ea060cbc2
--- thingsd/control.c	
+++ thingsd/control.c	
@@ -78,6 +78,10 @@ control_dispatch_parent(int fd, struct privsep_proc *p
 	case IMSG_GET_INFO_PARENT_END_DATA:
 	case IMSG_GET_INFO_THINGS_DATA:
 	case IMSG_GET_INFO_THINGS_END_DATA:
+	case IMSG_GET_INFO_CLIENTS_DATA:
+	case IMSG_GET_INFO_CLIENTS_END_DATA:
+	case IMSG_GET_INFO_SOCKETS_DATA:
+	case IMSG_GET_INFO_SOCKETS_END_DATA:
 		if ((c = control_connbyfd(imsg->hdr.peerid)) == NULL) {
 			log_warnx("%s: fd %d: not found",
 			    __func__, imsg->hdr.peerid);
@@ -328,8 +332,11 @@ control_dispatch_imsg(int fd, short event, void *arg)
 			break;
 
 		switch (imsg.hdr.type) {
+		case IMSG_KILL_CLIENT:
 		case IMSG_GET_INFO_PARENT_REQUEST:
 		case IMSG_GET_INFO_THINGS_REQUEST:
+		case IMSG_GET_INFO_CLIENTS_REQUEST:
+		case IMSG_GET_INFO_SOCKETS_REQUEST:
 		case IMSG_GET_INFO_CONTROL_REQUEST:
 			break;
 		default:
@@ -373,8 +380,11 @@ control_dispatch_imsg(int fd, short event, void *arg)
 			    IMSG_GET_INFO_CONTROL_END_DATA, 0, 0, -1,
 			    &nci, sizeof(struct thingsd_control_info));
 			break;
+		case IMSG_KILL_CLIENT:
 		case IMSG_GET_INFO_PARENT_REQUEST:
 		case IMSG_GET_INFO_THINGS_REQUEST:
+		case IMSG_GET_INFO_CLIENTS_REQUEST:
+		case IMSG_GET_INFO_SOCKETS_REQUEST:
 			imsg.hdr.peerid = fd;
 
 			if (euid == 0 &&
blob - 0170e954c8abca1d82cce61a746889806764678f
blob + 3140454b9eed4292dc449f062cdb5091dc7b7a36
--- thingsd/sockets.c	
+++ thingsd/sockets.c	
@@ -473,3 +473,52 @@ socket_err(struct bufferevent *bev, short error, void 
 		}
 	}
 }
+
+void
+sockets_show_info(struct privsep *ps, struct imsg *imsg)
+{
+	char filter[THINGSD_MAXTHINGNAME];
+	struct socket	*socket, nsi;
+	size_t		 n;
+
+	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_SOCKETS_REQUEST:
+
+		memcpy(filter, imsg->data, sizeof(filter));
+
+		TAILQ_FOREACH(socket, thingsd_env->sockets, entry) {
+			/* if (filter[0] == '\0' || memcmp(filter, */
+			/*     socket->name, sizeof(filter)) == 0) { */
+
+				n = strlcpy(nsi.name, socket->name,
+				    sizeof(nsi.name));
+				if (n >= sizeof(nsi.name))
+					fatalx("%s: nsi.name too long",
+					    __func__);
+
+				nsi.fd = socket->fd;
+				nsi.port = socket->port;
+				nsi.tls = socket->tls;
+				nsi.client_cnt = socket->client_cnt;
+				nsi.max_clients = socket->max_clients;
+
+				if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+				    IMSG_GET_INFO_SOCKETS_DATA,
+				    imsg->hdr.peerid, -1, &nsi,
+				    sizeof(nsi)) == -1)
+					return;
+
+			/* } */
+		}
+
+		if (proc_compose_imsg(ps, PROC_CONTROL, -1,
+		    IMSG_GET_INFO_SOCKETS_END_DATA, imsg->hdr.peerid,
+			    -1, &nsi, sizeof(nsi)) == -1)
+				return;
+
+		break;
+	default:
+		log_debug("%s: error handling imsg", __func__);
+		break;
+	}
+}
blob - 46124396806eaac65b68571ecb63a3e57b262190
blob + 925616b8d926051dcffd040c01144be549cde31f
--- thingsd/thingsd.c	
+++ thingsd/thingsd.c	
@@ -72,12 +72,33 @@ static struct passwd proc_privpw;
 int
 thingsd_dispatch_control(int fd, struct privsep_proc *p, struct imsg *imsg)
 {
-	struct privsep			*ps = p->p_ps;
-	int				 res = 0, cmd = 0, verbose;
-	unsigned int			 v = 0;
+	struct privsep	*ps = p->p_ps;
+	int		 res = 0, cmd = 0, verbose;
+	unsigned int	 v = 0;
+	char		 client_name[THINGSD_MAXTHINGNAME];
+	struct client	*client;
 
 	switch (imsg->hdr.type) {
-	case IMSG_GET_INFO_THINGS_REQUEST:
+	case IMSG_KILL_CLIENT:
+		IMSG_SIZE_CHECK(imsg, &v);
+		if (imsg->data == NULL)
+			break;
+		memcpy(client_name, imsg->data, sizeof(client_name));
+		TAILQ_FOREACH(client, thingsd_env->clients, entry) {
+			if (strcmp(client->name, client_name) == 0) {
+				log_debug("Control killed client: %s",
+				    client_name);
+				client_del(thingsd_env, client);
+				break;
+			}
+		}
+		break;
+	case IMSG_GET_INFO_CLIENTS_REQUEST:
+		clients_show_info(ps, imsg);
+		break;
+	case IMSG_GET_INFO_SOCKETS_REQUEST:
+		sockets_show_info(ps, imsg);
+		break;
 	case IMSG_GET_INFO_THINGS_REQUEST_ROOT:
 		things_show_info(ps, imsg);
 		break;
@@ -120,6 +141,18 @@ thingsd_dispatch_things(int fd, struct privsep_proc *p
 	struct privsep		*ps = p->p_ps;
 
 	switch (imsg->hdr.type) {
+	case IMSG_GET_INFO_CLIENTS_DATA:
+		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
+		break;
+	case IMSG_GET_INFO_CLIENTS_END_DATA:
+		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
+		break;
+	case IMSG_GET_INFO_SOCKETS_DATA:
+		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
+		break;
+	case IMSG_GET_INFO_SOCKETS_END_DATA:
+		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
+		break;
 	case IMSG_GET_INFO_THINGS_DATA:
 		proc_forward_imsg(ps, imsg, PROC_CONTROL, -1);
 		break;
blob - f0e92360b4d7c6e910cf08c5223d666e7a646f79
blob + 53870c81c9376caa219e01ab63dfed5fbc02deac
--- thingsd/thingsd.h	
+++ thingsd/thingsd.h	
@@ -58,14 +58,27 @@ enum imsg_type {
 	IMSG_GET_INFO_PARENT_REQUEST = IMSG_PROC_MAX,
 	IMSG_GET_INFO_PARENT_DATA,
 	IMSG_GET_INFO_PARENT_END_DATA,
+
+	IMSG_GET_INFO_CONTROL_REQUEST,
+	IMSG_GET_INFO_CONTROL_DATA,
+	IMSG_GET_INFO_CONTROL_END_DATA,
+
 	IMSG_GET_INFO_THINGS_REQUEST,
 	IMSG_GET_INFO_THINGS_REQUEST_ROOT,
 	IMSG_GET_INFO_THINGS_DATA,
 	IMSG_GET_INFO_THINGS_END_DATA,
-	IMSG_GET_INFO_CONTROL_REQUEST,
-	IMSG_GET_INFO_CONTROL_DATA,
-	IMSG_GET_INFO_CONTROL_END_DATA,
+
+	IMSG_GET_INFO_CLIENTS_REQUEST,
+	IMSG_GET_INFO_CLIENTS_DATA,
+	IMSG_GET_INFO_CLIENTS_END_DATA,
+
+	IMSG_GET_INFO_SOCKETS_REQUEST,
+	IMSG_GET_INFO_SOCKETS_DATA,
+	IMSG_GET_INFO_SOCKETS_END_DATA,
+
 	IMSG_ADD_THING,
+
+	IMSG_KILL_CLIENT,
 };
 
 enum socktypes {
@@ -243,6 +256,7 @@ void	 client_do_chk(struct thingsd *);
 void	 client_wr_things(struct client *, struct thing *, size_t);
 void	 client_tls_readcb(int, short, void *);
 void	 client_tls_writecb(int, short, void *);
+void	 clients_show_info(struct privsep *, struct imsg *);
 
 /* serial.c */
 void	 open_things(struct thingsd *, bool);
@@ -257,6 +271,7 @@ void	 socket_wr(struct bufferevent *, void *);
 void	 socket_err(struct bufferevent *, short, void *);
 int	 create_socket(int, char *, int);
 int	 open_client_socket(char *, int);
+void	 sockets_show_info(struct privsep *, struct imsg *);
 
 /* thingsd.c */
 void	 thingsd_reload(int);