Skip to content

Commit

Permalink
Add mosquitto_username_pw_len_set() to allow binary passwords to be…
Browse files Browse the repository at this point in the history
… set.
  • Loading branch information
ralight committed Oct 14, 2021
1 parent e6cca02 commit fffe18c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Client library:
`%u` patterns for client id / username substitution.
- Performance: reduce memory allocations when sending packets.
- Reintroduce threading support for Windows.
- Add `mosquitto_username_pw_len_set()` to allow binary passwords to be set.


2.0.12 - 2021-08-31
Expand Down
24 changes: 24 additions & 0 deletions include/mosquitto.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,30 @@ libmosq_EXPORT int mosquitto_will_clear(struct mosquitto *mosq);
*/
libmosq_EXPORT int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, const char *password);

/*
* Function: mosquitto_username_pw_len_set
*
* Configure username and password for a mosquitto instance, with explicit
* password length for binary passwords. By default, no username or password
* will be sent. For v3.1 and v3.1.1 clients, if username is NULL, the password
* argument is ignored.
*
* This is must be called before calling <mosquitto_connect>.
*
* Parameters:
* mosq - a valid mosquitto instance.
* username - the username to send as a string, or NULL to disable
* authentication.
* password_len - the length in bytes of password.
* password - the password to send as a string. Set to NULL when username is
* valid in order to send just a username.
*
* Returns:
* MOSQ_ERR_SUCCESS - on success.
* MOSQ_ERR_INVAL - if the input parameters were invalid.
* MOSQ_ERR_NOMEM - if an out of memory condition occurred.
*/
libmosq_EXPORT int mosquitto_username_pw_len_set(struct mosquitto *mosq, const char *username, size_t password_len, const char *password);

/* ======================================================================
*
Expand Down
1 change: 1 addition & 0 deletions lib/linker.version
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,5 @@ MOSQ_2.1 {
mosquitto_topic_matches_sub_with_pattern;
mosquitto_sub_matches_acl;
mosquitto_sub_matches_acl_with_pattern;
mosquitto_username_pw_len_set;
} MOSQ_1.7;
15 changes: 13 additions & 2 deletions lib/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ int mosquitto_will_clear(struct mosquitto *mosq)


int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, const char *password)
{
size_t len = 0;

if(password){
len = strlen(password);
}
return mosquitto_username_pw_len_set(mosq, username, len, password);
}

int mosquitto_username_pw_len_set(struct mosquitto *mosq, const char *username, size_t password_len, const char *password)
{
size_t slen;

Expand Down Expand Up @@ -98,13 +108,14 @@ int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, cons
if(!mosq->username) return MOSQ_ERR_NOMEM;
}

if(password){
mosq->password = mosquitto__strdup(password);
if(password && password_len > 0){
mosq->password = mosquitto__calloc(1, password_len+1);
if(!mosq->password){
mosquitto__free(mosq->username);
mosq->username = NULL;
return MOSQ_ERR_NOMEM;
}
memcpy(mosq->password, password, password_len);
}
return MOSQ_ERR_SUCCESS;
}
Expand Down

0 comments on commit fffe18c

Please sign in to comment.