Anybody knows if this is legal?
This depends very much on the jurisdiction; there is no globally valid answer.
The core point is that software is never sold, always licensed. Software companies generally try to make these licenses as restrictive as possible, but many countries have laws that make such restrictions invalid, especially when they involve end users (because allowing arbitrarily nasty hidden license terms would cripple daily business operations).
I know that, for example, German law basically allows the resale of software licenses, no matter what the license terms say, even if done commercially. I'm pretty sure it's different in the USA, and different again in the UK.