lib/libc/net/minix/getpeereid.c

http://www.minix3.org/ · C · 36 lines · 23 code · 4 blank · 9 comment · 7 complexity · cef4a41faa31bca032c162e6cc5c85a8 MD5 · raw file

  1. #include <errno.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/socket.h>
  5. /*
  6. * get the effective user ID and effective group ID of a peer
  7. * connected through a Unix domain socket.
  8. */
  9. int getpeereid(int sd, uid_t *euid, gid_t *egid) {
  10. int rc;
  11. struct ucred cred;
  12. socklen_t ucred_length;
  13. /* Initialize Data Structures */
  14. ucred_length = sizeof(struct ucred);
  15. memset(&cred, '\0', ucred_length);
  16. /* Validate Input Parameters */
  17. if (euid == NULL || egid == NULL) {
  18. errno = EFAULT;
  19. return -1;
  20. } /* getsockopt will handle validating 'sd' */
  21. /* Get the credentials of the peer at the other end of 'sd' */
  22. rc = getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_length);
  23. if (rc == 0) {
  24. /* Success - return the results */
  25. *euid = cred.uid;
  26. *egid = cred.gid;
  27. return 0;
  28. } else {
  29. /* Failure - getsockopt takes care of setting errno */
  30. return -1;
  31. }
  32. }