auth/auth_fb.c
/** return 0 if a new connection is authenticated */
return 0 if a new connection is authenticated
return 0 if a new connection is authenticated
int auth_auth(void *authp, client_st *p, const unsigned char *password)
{
    auth_st *auth = authp;
    int rv, srv;
    MYSQL_RES *result;
    MYSQL_ROW row;
    int device_0_flag = 0;
    rv = 1;
    if (p->username[0] == '\0' || p->username[1] != '-' || p->username[2] == '\0'
            || *password == '\0') {
        log_warn("auth failed: %s (invalid)", p->username);
        goto nocleanup;
    }
    switch (p->username[0]) {
    case 'U':
        snprintf(auth->q, MAX_QUERY_LEN, "select api_token, admin, id from users where username = '%s'",
                &p->username[2]);
        break;
    case 'D':
        if (p->username[2] == '0' && p->username[3] == '\0') {
            device_0_flag = 1;
            snprintf(auth->q, MAX_QUERY_LEN, "SELECT '%s', 0, 0 FROM production_ips WHERE address = '%s'",
                     DEVICE_0_PASSWORD, p->ip_address);
        }
        else {
            snprintf(auth->q, MAX_QUERY_LEN, "SELECT SUBSTR(aes_key, 1, 12), 0, 0 FROM devices WHERE id = '%s';", &p->username[2]);
        }
        break;
    case 'T':
        snprintf(auth->q, MAX_QUERY_LEN, "SELECT api_token, admin, id FROM users WHERE id = %s", &p->username[2]);
        break;
    }
    LOG_DEBUG("%s", auth->q);
    if (mysql_query(&auth->mysql, auth->q)) {
        log_warn("auth_auth query error: %s", mysql_error(&auth->mysql));
        goto nocleanup;
    }
    result = mysql_store_result(&auth->mysql);
    if (result == NULL) {
        log_warn("auth failed: %s", p->username);
        goto nocleanup;
    }
    int num_fields = mysql_num_fields(result);
    if (num_fields < 2) {
        log_warn("auth failed: %s, %d fields", p->username, num_fields);
        goto cleanup;
    }
    if ((row = mysql_fetch_row(result))) {
        if (row[0] && strcmp(row[0], (char *)password) == 0) {
            p->is_super = (strcmp(row[1], "1") == 0);
            strcpy(p->user_id_str, row[2]);
            rv = 0;
            goto cleanup;
        }
        else {
            log_warn("auth failed %s", p->username);
        }
    }
    else if (device_0_flag) {
        log_warn("%s: provisioning device from unauthorized ip address %s", config.name, p->ip_address);
        rv = 0;
    }
cleanup:
    mysql_free_result(result);
    while (mysql_next_result(&auth->mysql) == 0)
        ;
nocleanup:
    LOG_DEBUG("auth_auth %s %s rv %d", p->username, password, rv);
    return rv;
}
int auth_auth ( void * authp , client_st * p , const unsigned char * password ) { auth_st * auth = authp ; int rv , srv ; MYSQL_RES * result ; MYSQL_ROW row ; int device_0_flag = 0 ; rv = 1 ; if ( p -> username [ 0 ] == ' \0 ' || p -> username [ 1 ] != ' ' || p -> username [ 2 ] == ' \0 ' || * password == ' \0 ' ) { log_warn ( " " , p -> username ) ; goto nocleanup ; } switch ( p -> username [ 0 ] ) { case ' ' : snprintf ( auth -> q , MAX_QUERY_LEN , " " , & p -> username [ 2 ] ) ; break ; case ' ' : if ( p -> username [ 2 ] == ' ' && p -> username [ 3 ] == ' \0 ' ) { device_0_flag = 1 ; snprintf ( auth -> q , MAX_QUERY_LEN , " " , DEVICE_0_PASSWORD , p -> ip_address ) ; } else { snprintf ( auth -> q , MAX_QUERY_LEN , " " , & p -> username [ 2 ] ) ; } break ; case ' ' : snprintf ( auth -> q , MAX_QUERY_LEN , " " , & p -> username [ 2 ] ) ; break ; } LOG_DEBUG ( " " , auth -> q ) ; if ( mysql_query ( & auth -> mysql , auth -> q ) ) { log_warn ( " " , mysql_error ( & auth -> mysql ) ) ; goto nocleanup ; } result = mysql_store_result ( & auth -> mysql ) ; if ( result == NULL ) { log_warn ( " " , p -> username ) ; goto nocleanup ; } int num_fields = mysql_num_fields ( result ) ; if ( num_fields < 2 ) { log_warn ( " " , p -> username , num_fields ) ; goto cleanup ; } if ( ( row = mysql_fetch_row ( result ) ) ) { if ( row [ 0 ] && strcmp ( row [ 0 ] , ( char * ) password ) == 0 ) { p -> is_super = ( strcmp ( row [ 1 ] , " " ) == 0 ) ; strcpy ( p -> user_id_str , row [ 2 ] ) ; rv = 0 ; goto cleanup ; } else { log_warn ( " " , p -> username ) ; } } else if ( device_0_flag ) { log_warn ( " " , config . name , p -> ip_address ) ; rv = 0 ; } cleanup : mysql_free_result ( result ) ; while ( mysql_next_result ( & auth -> mysql ) == 0 ) ; nocleanup : LOG_DEBUG ( " " , p -> username , password , rv ) ; return rv ; }
return 0 if a new connection is authenticated
return 0 if a new connection is authenticated