Implement setgid/setuid support.

Fixes #3841