/libraries/liblutil/getpeereid.c

https://bitbucket.org/devzero2000/openldap · C · 220 lines · 165 code · 31 blank · 24 comment · 37 complexity · a36e6d70a9970c7c7546e2990a328519 MD5 · raw file

  1. /* getpeereid.c */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 2000-2012 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in the file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. #ifndef _GNU_SOURCE
  17. #define _GNU_SOURCE 1 /* Needed for glibc struct ucred */
  18. #endif
  19. #include "portable.h"
  20. #ifndef HAVE_GETPEEREID
  21. #include <sys/types.h>
  22. #include <ac/unistd.h>
  23. #include <ac/socket.h>
  24. #include <ac/errno.h>
  25. #ifdef HAVE_GETPEERUCRED
  26. #include <ucred.h>
  27. #endif
  28. #ifdef LDAP_PF_LOCAL_SENDMSG
  29. #include <lber.h>
  30. #ifdef HAVE_SYS_UIO_H
  31. #include <sys/uio.h>
  32. #endif
  33. #include <sys/stat.h>
  34. #endif
  35. #ifdef HAVE_SYS_UCRED_H
  36. #ifdef HAVE_GRP_H
  37. #include <grp.h> /* for NGROUPS on Tru64 5.1 */
  38. #endif
  39. #include <sys/ucred.h>
  40. #endif
  41. #include <stdlib.h>
  42. int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
  43. #ifdef LDAP_PF_LOCAL_SENDMSG
  44. , struct berval *peerbv
  45. #endif
  46. )
  47. {
  48. #ifdef LDAP_PF_LOCAL
  49. #if defined( HAVE_GETPEERUCRED )
  50. ucred_t *uc = NULL;
  51. if( getpeerucred( s, &uc ) == 0 ) {
  52. *euid = ucred_geteuid( uc );
  53. *egid = ucred_getegid( uc );
  54. ucred_free( uc );
  55. return 0;
  56. }
  57. #elif defined( SO_PEERCRED )
  58. struct ucred peercred;
  59. ber_socklen_t peercredlen = sizeof peercred;
  60. if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
  61. (void *)&peercred, &peercredlen ) == 0 )
  62. && ( peercredlen == sizeof peercred ))
  63. {
  64. *euid = peercred.uid;
  65. *egid = peercred.gid;
  66. return 0;
  67. }
  68. #elif defined( LOCAL_PEERCRED )
  69. struct xucred peercred;
  70. ber_socklen_t peercredlen = sizeof peercred;
  71. if(( getsockopt( s, LOCAL_PEERCRED, 1,
  72. (void *)&peercred, &peercredlen ) == 0 )
  73. && ( peercred.cr_version == XUCRED_VERSION ))
  74. {
  75. *euid = peercred.cr_uid;
  76. *egid = peercred.cr_gid;
  77. return 0;
  78. }
  79. #elif defined( LDAP_PF_LOCAL_SENDMSG ) && defined( MSG_WAITALL )
  80. int err, fd;
  81. struct iovec iov;
  82. struct msghdr msg = {0};
  83. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  84. # ifndef CMSG_SPACE
  85. # define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
  86. # endif
  87. # ifndef CMSG_LEN
  88. # define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
  89. # endif
  90. struct {
  91. struct cmsghdr cm;
  92. int fd;
  93. } control_st;
  94. struct cmsghdr *cmsg;
  95. # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
  96. struct stat st;
  97. struct sockaddr_un lname, rname;
  98. ber_socklen_t llen, rlen;
  99. rlen = sizeof(rname);
  100. llen = sizeof(lname);
  101. memset( &lname, 0, sizeof( lname ));
  102. getsockname(s, (struct sockaddr *)&lname, &llen);
  103. iov.iov_base = peerbv->bv_val;
  104. iov.iov_len = peerbv->bv_len;
  105. msg.msg_iov = &iov;
  106. msg.msg_iovlen = 1;
  107. peerbv->bv_len = 0;
  108. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  109. msg.msg_control = &control_st;
  110. msg.msg_controllen = sizeof( struct cmsghdr ) + sizeof( int ); /* no padding! */
  111. cmsg = CMSG_FIRSTHDR( &msg );
  112. # else
  113. msg.msg_accrights = (char *)&fd;
  114. msg.msg_accrightslen = sizeof(fd);
  115. # endif
  116. /*
  117. * AIX returns a bogus file descriptor if recvmsg() is
  118. * called with MSG_PEEK (is this a bug?). Hence we need
  119. * to receive the Abandon PDU.
  120. */
  121. err = recvmsg( s, &msg, MSG_WAITALL );
  122. if( err >= 0 &&
  123. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  124. cmsg->cmsg_len == CMSG_LEN( sizeof(int) ) &&
  125. cmsg->cmsg_level == SOL_SOCKET &&
  126. cmsg->cmsg_type == SCM_RIGHTS
  127. # else
  128. msg.msg_accrightslen == sizeof(int)
  129. # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
  130. ) {
  131. int mode = S_IFIFO|S_ISUID|S_IRWXU;
  132. /* We must receive a valid descriptor, it must be a pipe,
  133. * it must only be accessible by its owner, and it must
  134. * have the name of our socket written on it.
  135. */
  136. peerbv->bv_len = err;
  137. # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
  138. fd = (*(int *)CMSG_DATA( cmsg ));
  139. # endif
  140. err = fstat( fd, &st );
  141. if ( err == 0 )
  142. rlen = read(fd, &rname, rlen);
  143. close(fd);
  144. if( err == 0 && st.st_mode == mode &&
  145. llen == rlen && !memcmp(&lname, &rname, llen))
  146. {
  147. *euid = st.st_uid;
  148. *egid = st.st_gid;
  149. return 0;
  150. }
  151. }
  152. #elif defined(SOCKCREDSIZE)
  153. struct msghdr msg;
  154. ber_socklen_t crmsgsize;
  155. void *crmsg;
  156. struct cmsghdr *cmp;
  157. struct sockcred *sc;
  158. memset(&msg, 0, sizeof msg);
  159. crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
  160. if (crmsgsize == 0) goto sc_err;
  161. crmsg = malloc(crmsgsize);
  162. if (crmsg == NULL) goto sc_err;
  163. memset(crmsg, 0, crmsgsize);
  164. msg.msg_control = crmsg;
  165. msg.msg_controllen = crmsgsize;
  166. if (recvmsg(s, &msg, 0) < 0) {
  167. free(crmsg);
  168. goto sc_err;
  169. }
  170. if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
  171. free(crmsg);
  172. goto sc_err;
  173. }
  174. cmp = CMSG_FIRSTHDR(&msg);
  175. if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) {
  176. printf("nocreds\n");
  177. goto sc_err;
  178. }
  179. sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
  180. *euid = sc->sc_euid;
  181. *egid = sc->sc_egid;
  182. free(crmsg);
  183. return 0;
  184. sc_err:
  185. #endif
  186. #endif /* LDAP_PF_LOCAL */
  187. return -1;
  188. }
  189. #endif /* HAVE_GETPEEREID */