ctrl: Ask for login password if the user doesn't provide it.

This commit is contained in:
Roger A. Light 2020-10-29 12:49:51 +00:00
parent 1b3dc5e978
commit 99a544ac73
3 changed files with 27 additions and 4 deletions

View File

@ -3,7 +3,7 @@ if (WITH_TLS AND CJSON_FOUND)
include_directories(${mosquitto_SOURCE_DIR} ${mosquitto_SOURCE_DIR}/include
${mosquitto_SOURCE_DIR}/lib ${mosquitto_SOURCE_DIR}/src
${OPENSSL_INCLUDE_DIR} ${STDBOOL_H_PATH} ${STDINT_H_PATH}
${CJSON_INCLUDE_DIRS})
${CJSON_INCLUDE_DIRS}) ${mosquitto_SOURCE_DIR}/apps/mosquitto_passwd
add_executable(mosquitto_ctrl
mosquitto_ctrl.c mosquitto_ctrl.h
@ -12,6 +12,7 @@ if (WITH_TLS AND CJSON_FOUND)
dynsec_client.c
dynsec_group.c
dynsec_role.c
../mosquitto_passwd/get_password.c ../mosquitto_passwd/get_password.h
../../lib/memory_mosq.c ../../lib/memory_mosq.h
../../src/memory_public.c
options.c

View File

@ -8,7 +8,7 @@ else
LIBMOSQ:=../../lib/libmosquitto.a
endif
LOCAL_CPPFLAGS:=-I/usr/include/cjson -I/usr/local/include/cjson
LOCAL_CPPFLAGS:=-I/usr/include/cjson -I/usr/local/include/cjson -I../mosquitto_passwd
OBJS= mosquitto_ctrl.o \
client.o \
@ -16,6 +16,7 @@ OBJS= mosquitto_ctrl.o \
dynsec_client.o \
dynsec_group.o \
dynsec_role.o \
get_password.o \
memory_mosq.o \
memory_public.o \
options.o \
@ -50,6 +51,9 @@ dynsec_group.o : dynsec_group.c mosquitto_ctrl.h
dynsec_role.o : dynsec_role.c mosquitto_ctrl.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(APP_CPPFLAGS) $(APP_CFLAGS) -c $< -o $@
get_password.o : ../mosquitto_passwd/get_password.c ../mosquitto_passwd/get_password.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(APP_CPPFLAGS) $(APP_CFLAGS) -c $< -o $@
memory_mosq.o : ../../lib/memory_mosq.c
${CROSS_COMPILE}${CC} $(APP_CPPFLAGS) $(APP_CFLAGS) -c $< -o $@

View File

@ -35,6 +35,7 @@ Contributors:
#include <mosquitto.h>
#include <mqtt_protocol.h>
#include "mosquitto_ctrl.h"
#include "get_password.h"
#ifdef WITH_SOCKS
static int mosquitto__parse_socks_url(struct mosq_config *cfg, char *url);
@ -558,12 +559,29 @@ int client_config_load(struct mosq_config *cfg)
int client_opts_set(struct mosquitto *mosq, struct mosq_config *cfg)
{
#if defined(WITH_TLS) || defined(WITH_SOCKS)
int rc;
#endif
char prompt[1000];
char password[1000];
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, cfg->protocol_version);
if(cfg->username && cfg->password == NULL){
/* Ask for password */
snprintf(prompt, sizeof(prompt), "Password for %s: ", cfg->username);
rc = get_password(prompt, NULL, password, sizeof(password));
if(rc){
fprintf(stderr, "Error getting password.\n");
mosquitto_lib_cleanup();
return 1;
}
cfg->password = strdup(password);
if(cfg->password == NULL){
fprintf(stderr, "Error: Out of memory.\n");
mosquitto_lib_cleanup();
return 1;
}
}
if((cfg->username || cfg->password) && mosquitto_username_pw_set(mosq, cfg->username, cfg->password)){
fprintf(stderr, "Error: Problem setting username and/or password.\n");
mosquitto_lib_cleanup();