No they're not illegal. In Austria you see time limited rental contracts all the time, usually 3 years. It's rare to find unlimited rental nowadays as landlords want to make money.
germany doesn't allow time limited contracts. there are only two exceptions. a time limited contract is allowed if the landlord can claim to need to use the property themselves after the end of the contract (which they can only do if they don't have multiple properties to rent), or if they have plans to tear down or renovate the building.
It's nearly impossible as everyone is locked into their rental units and will never move.
You basically have to wait for someone to die.
In Germany, the scarcity is so high that realtors can afford to ghost you left and right despite you having money and then when you finally get to view an apartment there's like 80+ people viewing it and only the one with the best portfolio (full of your private info that they normally shouldn't be allowed to have) gets it. This madness is nowhere near what it's like in the US.
so as long as you pay your rent and don't do anything illegal or damaging, you can't be evicted. the worst that can happen is a rise of the rent, but even that is often limited to not go beyond the average market rate for the area.