...then it was ok?
No. Just because something is legal does not mean it is ethical.
But it is clear Alabama needs to update its law regarding how sheriffs are compensated.
...and at the vey least, he should have shared the money with his deputies.