The discussion on this page might be helpful
Check this: getpwnam()
Hook and wrapper scripts are the way to go.