is this legal?

That is retarded

Squatters are thieves and criminals, and should be treated as such
A friend of mine paid one $3k to move out of an adjacent dwelling on his property so he could sell his place. They left a mess too, cost another few k to clean it up. And he had to pay their utilities while they squatted, as it illegal to turn them off. And this is nothing compared to CA laws, where you must cede your property if they have lived their long enough.

Sorry to derail...