auth/auth_fb.c
/** return 0 if a subscription or publish is allowed */
return 0 if a subscription or publish is allowed
return 0 if a subscription or publish is allowed
int auth_acl(void *authp, client_st *p,
        auth_access_et access, const unsigned char *filter, int filter_len)
{
    auth_st *auth = authp;
    int rv, srv;
    MYSQL_RES *result;
    MYSQL_ROW row;
    LOG_DEBUG("auth_acl sd %d %d %.*s", p->sd, access, filter_len, filter);
    if (p->is_super) {
        return 0;
    }
    if (access == AUTH_PUB) {
        if (p->auth_info == NULL) {
            p->auth_info = set_create();
        }
        else {
            if (set_exists(p->auth_info, (char *)filter)) {
                return 0;
            }
        }
    }
    rv = 1;
    if (p->username[0] == 'D') {
        if (access == AUTH_SUB) {
            snprintf((char *)auth->topic, sizeof(auth->topic), "flameboss/%s/recv", &p->username[2]);
        }
        else {
            snprintf((char *)auth->topic, sizeof(auth->topic), "flameboss/%s/send/+", &p->username[2]);
        }
        if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
            rv = 0;
        }
        return rv;
    }
    else {
        char q[MAX_QUERY_LEN];
        if (access == AUTH_PUB) {
            if (mqtt_topic_matches((unsigned char *)"flameboss/relay/+", filter, filter_len)) {
                rv = 0;
                goto nocleanup;
            }
        }
        else {
            if (mqtt_topic_matches((unsigned char *)"flameboss/+/send/time", filter, filter_len)) {
                rv = 0;
                goto nocleanup;
            }
            if (mqtt_topic_matches((unsigned char *)"flameboss/+/send/open", filter, filter_len)) {
                rv = 0;
                goto nocleanup;
            }
        }
        srv = snprintf((char *)q, MAX_QUERY_LEN, "SELECT device_id FROM devices_users WHERE user_id = %s", p->user_id_str);
        if (srv == 0 || srv >= MAX_QUERY_LEN) {
            goto nocleanup;
        }
        LOG_DEBUG("%s", q);
        if (mysql_query(&auth->mysql, q)) {
            log_warn("auth_acl query error: %s", mysql_error(&auth->mysql));
            goto nocleanup;
        }
        result = mysql_store_result(&auth->mysql);
        if (result == NULL) {
            goto nocleanup;
        }
        int num_fields = mysql_num_fields(result);
        if (num_fields != 1) {
            goto cleanup;
        }
        char access_str[2];
        sprintf(access_str, "%d", access);
        while ((row = mysql_fetch_row(result))) {
            if (access == AUTH_SUB) {
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/%s/send/+", row[0]);
            }
            else {
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/%s/recv", row[0]);
            }
            LOG_DEBUG("auth_acl sub %s ? %.*s", auth->topic, filter_len, filter);
            if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
                rv = 0;
                goto cleanup;
            }
            if (access == AUTH_SUB) {
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/relay/%s/recv", row[0]);
                LOG_DEBUG("auth_acl sub %s ? %.*s", auth->topic, filter_len, filter);
                if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
                    rv = 0;
                    goto cleanup;
                }
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/mqttr/%s/send", row[0]);
                LOG_DEBUG("auth_acl sub %s ? %.*s", auth->topic, filter_len, filter);
                if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
                    rv = 0;
                    goto cleanup;
                }
            }
            else {
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/%s/recv", row[0]);
                LOG_DEBUG("auth_acl sub %s ? %.*s", auth->topic, filter_len, filter);
                if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
                    rv = 0;
                    goto cleanup;
                }
                srv = snprintf((char *)auth->topic, MAX_TOPIC_LEN, "flameboss/mqttr/%s/recv", row[0]);
                LOG_DEBUG("auth_acl sub %s ? %.*s", auth->topic, filter_len, filter);
                if (mqtt_topic_matches(auth->topic, filter, filter_len)) {
                    rv = 0;
                    goto cleanup;
                }
            }
        }
    cleanup:
        mysql_free_result(result);
        while (mysql_next_result(&auth->mysql) == 0)
            ;
    nocleanup:
        return rv;
    }
}
int auth_acl ( void * authp , client_st * p , auth_access_et access , const unsigned char * filter , int filter_len ) { auth_st * auth = authp ; int rv , srv ; MYSQL_RES * result ; MYSQL_ROW row ; LOG_DEBUG ( " " , p -> sd , access , filter_len , filter ) ; if ( p -> is_super ) { return 0 ; } if ( access == AUTH_PUB ) { if ( p -> auth_info == NULL ) { p -> auth_info = set_create ( ) ; } else { if ( set_exists ( p -> auth_info , ( char * ) filter ) ) { return 0 ; } } } rv = 1 ; if ( p -> username [ 0 ] == ' ' ) { if ( access == AUTH_SUB ) { snprintf ( ( char * ) auth -> topic , sizeof ( auth -> topic ) , " " , & p -> username [ 2 ] ) ; } else { snprintf ( ( char * ) auth -> topic , sizeof ( auth -> topic ) , " " , & p -> username [ 2 ] ) ; } if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; } return rv ; } else { char q [ MAX_QUERY_LEN ] ; if ( access == AUTH_PUB ) { if ( mqtt_topic_matches ( ( unsigned char * ) " " , filter , filter_len ) ) { rv = 0 ; goto nocleanup ; } } else { if ( mqtt_topic_matches ( ( unsigned char * ) " " , filter , filter_len ) ) { rv = 0 ; goto nocleanup ; } if ( mqtt_topic_matches ( ( unsigned char * ) " " , filter , filter_len ) ) { rv = 0 ; goto nocleanup ; } } srv = snprintf ( ( char * ) q , MAX_QUERY_LEN , " " , p -> user_id_str ) ; if ( srv == 0 || srv >= MAX_QUERY_LEN ) { goto nocleanup ; } LOG_DEBUG ( " " , q ) ; if ( mysql_query ( & auth -> mysql , q ) ) { log_warn ( " " , mysql_error ( & auth -> mysql ) ) ; goto nocleanup ; } result = mysql_store_result ( & auth -> mysql ) ; if ( result == NULL ) { goto nocleanup ; } int num_fields = mysql_num_fields ( result ) ; if ( num_fields != 1 ) { goto cleanup ; } char access_str [ 2 ] ; sprintf ( access_str , " " , access ) ; while ( ( row = mysql_fetch_row ( result ) ) ) { if ( access == AUTH_SUB ) { srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; } else { srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; } LOG_DEBUG ( " " , auth -> topic , filter_len , filter ) ; if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; goto cleanup ; } if ( access == AUTH_SUB ) { srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; LOG_DEBUG ( " " , auth -> topic , filter_len , filter ) ; if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; goto cleanup ; } srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; LOG_DEBUG ( " " , auth -> topic , filter_len , filter ) ; if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; goto cleanup ; } } else { srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; LOG_DEBUG ( " " , auth -> topic , filter_len , filter ) ; if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; goto cleanup ; } srv = snprintf ( ( char * ) auth -> topic , MAX_TOPIC_LEN , " " , row [ 0 ] ) ; LOG_DEBUG ( " " , auth -> topic , filter_len , filter ) ; if ( mqtt_topic_matches ( auth -> topic , filter , filter_len ) ) { rv = 0 ; goto cleanup ; } } } cleanup : mysql_free_result ( result ) ; while ( mysql_next_result ( & auth -> mysql ) == 0 ) ; nocleanup : return rv ; } }
return 0 if a subscription or publish is allowed
return 0 if a subscription or publish is allowed
/* Pub ACL caching */ /* authorize device */ /* authorize user */ /* authorize public topics */