forked from eclipse/mosquitto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multistep auth, plus reauth, plus tests.
- Loading branch information
Showing
22 changed files
with
509 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright (c) 2018 Roger Light <[email protected]> | ||
Copyright (c) 2018-2019 Roger Light <[email protected]> | ||
All rights reserved. This program and the accompanying materials | ||
are made available under the terms of the Eclipse Public License v1.0 | ||
|
@@ -21,6 +21,7 @@ and the Eclipse Distribution License is available at | |
|
||
#include "mosquitto_broker_internal.h" | ||
#include "mqtt_protocol.h" | ||
#include "memory_mosq.h" | ||
#include "packet_mosq.h" | ||
#include "property_mosq.h" | ||
#include "send_mosq.h" | ||
|
@@ -31,23 +32,120 @@ int handle__auth(struct mosquitto_db *db, struct mosquitto *context) | |
int rc = 0; | ||
uint8_t reason_code = 0; | ||
mosquitto_property *properties = NULL; | ||
char *auth_method = NULL; | ||
void *auth_data = NULL; | ||
uint16_t auth_data_len = 0; | ||
void *auth_data_out = NULL; | ||
uint16_t auth_data_out_len = 0; | ||
|
||
if(!context) return MOSQ_ERR_INVAL; | ||
log__printf(NULL, MOSQ_LOG_DEBUG, "Received AUTH from %s", context->id); | ||
|
||
if(context->protocol != mosq_p_mqtt5){ | ||
if(context->protocol != mosq_p_mqtt5 || context->auth_method == NULL){ | ||
return MOSQ_ERR_PROTOCOL; | ||
} | ||
|
||
if(context->in_packet.remaining_length > 0){ | ||
if(packet__read_byte(&context->in_packet, &reason_code)) return 1; | ||
if(reason_code != MQTT_RC_CONTINUE_AUTHENTICATION | ||
&& reason_code != MQTT_RC_REAUTHENTICATE){ | ||
|
||
send__disconnect(context, MQTT_RC_PROTOCOL_ERROR, NULL); | ||
return MOSQ_ERR_PROTOCOL; | ||
} | ||
|
||
if((reason_code == MQTT_RC_REAUTHENTICATE && context->state != mosq_cs_connected) | ||
|| (reason_code == MQTT_RC_CONTINUE_AUTHENTICATION | ||
&& context->state != mosq_cs_authenticating && context->state != mosq_cs_reauthenticating)){ | ||
|
||
send__disconnect(context, MQTT_RC_PROTOCOL_ERROR, NULL); | ||
return MOSQ_ERR_PROTOCOL; | ||
} | ||
|
||
rc = property__read_all(CMD_AUTH, &context->in_packet, &properties); | ||
if(rc) return rc; | ||
if(rc){ | ||
send__disconnect(context, MQTT_RC_UNSPECIFIED, NULL); | ||
return rc; | ||
} | ||
|
||
|
||
if(mosquitto_property_read_string(properties, MQTT_PROP_AUTHENTICATION_METHOD, &auth_method, false) == NULL){ | ||
mosquitto_property_free_all(&properties); | ||
send__disconnect(context, MQTT_RC_UNSPECIFIED, NULL); | ||
return MOSQ_ERR_PROTOCOL; | ||
} | ||
|
||
if(!auth_method || strcmp(auth_method, context->auth_method)){ | ||
/* No method, or non-matching method */ | ||
mosquitto__free(auth_method); | ||
mosquitto_property_free_all(&properties); | ||
send__disconnect(context, MQTT_RC_PROTOCOL_ERROR, NULL); | ||
return MOSQ_ERR_PROTOCOL; | ||
} | ||
mosquitto__free(auth_method); | ||
|
||
mosquitto_property_read_binary(properties, MQTT_PROP_AUTHENTICATION_DATA, &auth_data, &auth_data_len, false); | ||
|
||
mosquitto_property_free_all(&properties); /* FIXME - TEMPORARY UNTIL PROPERTIES PROCESSED */ | ||
} | ||
|
||
/* FIXME - Extended auth not currently supported */ | ||
send__disconnect(context, MQTT_RC_NOT_AUTHORIZED, NULL); | ||
return 1; | ||
log__printf(NULL, MOSQ_LOG_DEBUG, "Received AUTH from %s (rc%d, %s)", context->id, reason_code, context->auth_method); | ||
|
||
|
||
if(reason_code == MQTT_RC_REAUTHENTICATE){ | ||
/* This is a re-authentication attempt */ | ||
context__set_state(context, mosq_cs_reauthenticating); | ||
rc = mosquitto_security_auth_start(db, context, true, auth_data, auth_data_len, &auth_data_out, &auth_data_out_len); | ||
}else{ | ||
if(context->state != mosq_cs_reauthenticating){ | ||
context__set_state(context, mosq_cs_authenticating); | ||
} | ||
rc = mosquitto_security_auth_continue(db, context, auth_data, auth_data_len, &auth_data_out, &auth_data_out_len); | ||
} | ||
mosquitto__free(auth_data); | ||
if(rc == MOSQ_ERR_SUCCESS){ | ||
if(context->state == mosq_cs_authenticating){ | ||
return connect__on_authorised(db, context, auth_data_out, auth_data_out_len); | ||
}else{ | ||
context__set_state(context, mosq_cs_connected); | ||
rc = send__auth(db, context, MQTT_RC_SUCCESS, auth_data_out, auth_data_out_len); | ||
free(auth_data_out); | ||
return rc; | ||
} | ||
}else if(rc == MOSQ_ERR_AUTH_CONTINUE){ | ||
rc = send__auth(db, context, MQTT_RC_CONTINUE_AUTHENTICATION, auth_data_out, auth_data_out_len); | ||
free(auth_data_out); | ||
return rc; | ||
}else{ | ||
free(auth_data_out); | ||
if(context->state == mosq_cs_authenticating && context->will){ | ||
/* Free will without sending if this is our first authentication attempt */ | ||
mosquitto_property_free_all(&context->will->properties); | ||
mosquitto__free(context->will->msg.payload); | ||
mosquitto__free(context->will->msg.topic); | ||
mosquitto__free(context->will); | ||
context->will = NULL; | ||
} | ||
if(rc == MOSQ_ERR_AUTH){ | ||
send__connack(db, context, 0, MQTT_RC_NOT_AUTHORIZED, NULL); | ||
if(context->state == mosq_cs_authenticating){ | ||
mosquitto__free(context->id); | ||
context->id = NULL; | ||
} | ||
return MOSQ_ERR_PROTOCOL; | ||
}else if(rc == MOSQ_ERR_NOT_SUPPORTED){ | ||
/* Client has requested extended authentication, but we don't support it. */ | ||
send__connack(db, context, 0, MQTT_RC_BAD_AUTHENTICATION_METHOD, NULL); | ||
if(context->state == mosq_cs_authenticating){ | ||
mosquitto__free(context->id); | ||
context->id = NULL; | ||
} | ||
return MOSQ_ERR_PROTOCOL; | ||
}else{ | ||
if(context->state == mosq_cs_authenticating){ | ||
mosquitto__free(context->id); | ||
context->id = NULL; | ||
} | ||
return rc; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.