]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getpeereid.c
fix ITS#3499 (may need further testing)
[openldap] / libraries / liblutil / getpeereid.c
1 /* getpeereid.c */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 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
17 #include "portable.h"
18
19 #ifndef HAVE_GETPEEREID
20
21 #include <sys/types.h>
22 #include <ac/unistd.h>
23
24 #include <ac/socket.h>
25 #include <ac/errno.h>
26
27 #if HAVE_SYS_UCRED_H
28 #if HAVE_GRP_H
29 #include <grp.h>        /* for NGROUPS on Tru64 5.1 */
30 #endif
31 #include <sys/ucred.h>
32 #endif
33
34 #if !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
35         defined(HAVE_SENDMSG) && defined(HAVE_MSGHDR_MSG_ACCRIGHTS)
36 #define DO_SENDMSG
37 #ifdef HAVE_SYS_UIO_H
38 #include <sys/uio.h>
39 #endif
40 #include <sys/stat.h>
41 #endif
42
43 int getpeereid( int s, uid_t *euid, gid_t *egid )
44 {
45 #ifdef LDAP_PF_LOCAL
46 #if defined( SO_PEERCRED )
47         struct ucred peercred;
48         size_t peercredlen = sizeof peercred;
49
50         if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
51                 (void *)&peercred, &peercredlen ) == 0 )
52                 && ( peercredlen == sizeof peercred ))
53         {
54                 *euid = peercred.uid;
55                 *egid = peercred.gid;
56                 return 0;
57         }
58
59 #elif defined( LOCAL_PEERCRED )
60         struct xucred peercred;
61         socklen_t peercredlen = sizeof peercred;
62
63         if(( getsockopt( s, LOCAL_PEERCRED, 1,
64                 (void *)&peercred, &peercredlen ) == 0 )
65                 && ( peercred.cr_version == XUCRED_VERSION ))
66         {
67                 *euid = peercred.cr_uid;
68                 *egid = peercred.cr_gid;
69                 return 0;
70         }
71 #elif defined( DO_SENDMSG )
72         int dummy, fd[2];
73         struct iovec iov;
74         struct msghdr msg = {0};
75         struct stat st;
76
77         iov.iov_base = (char*) &dummy;
78         iov.iov_len = 1;
79         msg.msg_iov = &iov;
80         msg.msg_iovlen = 1;
81         msg.msg_accrights = (char *)fd;
82         msg.msg_accrightslen = sizeof(fd);
83         if( recvmsg( s, &msg, MSG_PEEK) >= 0 && msg.msg_accrightslen == sizeof(int) )
84         {
85                 /* We must receive a valid descriptor, it must be a pipe,
86                  * and it must only be accessible by its owner.
87                  */
88                 dummy = fstat( fd[0], &st );
89                 close(fd[0]);
90                 if( dummy == 0 && S_ISFIFO(st.st_mode) &&
91                         ((st.st_mode & (S_IRWXG|S_IRWXO)) == 0))
92                 {
93                         *euid = st.st_uid;
94                         *egid = st.st_gid;
95                         return 0;
96                 }
97         }
98 #endif
99 #endif /* LDAP_PF_LOCAL */
100
101         return -1;
102 }
103
104 #endif /* HAVE_GETPEEREID */