aboutsummaryrefslogtreecommitdiffstats
path: root/main/mqtt-exec/0001-authentication-expose-authentication-with-credential.patch
blob: aba1cee9fa5bbe8481b10fce03df3ad6377650d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
From 5ee7377172dc0f30a64d009210db7efbf5d2219f Mon Sep 17 00:00:00 2001
From: Kevin Daudt <me@ikke.info>
Date: Wed, 14 Mar 2018 22:50:28 +0100
Subject: [PATCH] authentication: expose authentication with credentials

libmosquitto supports authentication with credentials, so allow settings
credentials through parameters.
---
 mqtt-exec.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/mqtt-exec.c b/mqtt-exec.c
index fc5ab03..28251fb 100644
--- a/mqtt-exec.c
+++ b/mqtt-exec.c
@@ -71,8 +71,10 @@ int usage(int retcode)
 " -i,--id ID                  The id to use for this client\n"
 " -k,--keepalive SEC          Set keepalive to SEC. Default is 60\n"
 " -p,--port PORT              Set TCP port to PORT. Default is 1883\n"
+" -P,--password PASSWORD      Set password for authentication\n"
 " -q,--qos QOS                Set Quality of Serive to level. Default is 0\n"
 " -t,--topic TOPIC            Set MQTT topic to TOPIC. May be repeated\n"
+" -u,--username USERNAME      Set username for authentication\n"
 " -v,--verbose                Pass over the topic to application as firs arg\n"
 " --will-topic TOPIC          Set the client Will topic to TOPIC\n"
 " --will-payload MSG          Set the client Will message to MSG\n"
@@ -119,6 +121,8 @@ int main(int argc, char *argv[])
 		{"qos",		required_argument,	0, 'q' },
 		{"topic",	required_argument,	0, 't' },
 		{"verbose",	no_argument,		0, 'v' },
+		{"username",	required_argument,	0, 'u' },
+		{"password",	required_argument,	0, 'P' },
 		{"will-topic",	required_argument,	0, 0x1001 },
 		{"will-payload", required_argument,	0, 0x1002 },
 		{"will-qos",	required_argument,	0, 0x1003 },
@@ -145,6 +149,8 @@ int main(int argc, char *argv[])
 	char hostname[256];
 	static char id[MOSQ_MQTT_ID_MAX_LENGTH+1];
 	struct mosquitto *mosq = NULL;
+	char *username = NULL;
+	char *password = NULL;
 
 	char *will_payload = NULL;
 	int will_qos = 0;
@@ -166,7 +172,7 @@ int main(int argc, char *argv[])
 	memset(hostname, 0, sizeof(hostname));
 	memset(id, 0, sizeof(id));
 
-	while ((c = getopt_long(argc, argv, "cdh:i:k:p:q:t:v", opts, &i)) != -1) {
+	while ((c = getopt_long(argc, argv, "cdh:i:k:p:P:q:t:u:v", opts, &i)) != -1) {
 		switch(c) {
 		case 'c':
 			clean_session = false;
@@ -191,6 +197,8 @@ int main(int argc, char *argv[])
 		case 'p':
 			port = atoi(optarg);
 			break;
+		case 'P':
+			password = optarg;
 		case 'q':
 			ud.qos = atoi(optarg);
 			if (!valid_qos_range(ud.qos, "QoS"))
@@ -202,6 +210,8 @@ int main(int argc, char *argv[])
 					    sizeof(char *) * ud.topic_count);
 			ud.topics[ud.topic_count-1] = optarg;
 			break;
+		case 'u':
+			username = optarg;
 		case 'v':
 			ud.verbose = 1;
 			break;
@@ -286,6 +296,14 @@ int main(int argc, char *argv[])
 		goto cleanup;
 	}
 
+	if (!username != !password) {
+		fprintf(stderr, "Need to set both username and password\n");
+		goto cleanup;
+	}
+
+	if (username && password)
+		mosquitto_username_pw_set(mosq, username, password);
+
 #ifdef WITH_TLS
 	if ((cafile || capath) && mosquitto_tls_set(mosq, cafile, capath, certfile,
 						    keyfile, NULL)) {
-- 
2.18.0