]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getpeereid.c
Fix typo in dec to bin conversion
[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-2007 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 #ifdef HAVE_GETPEERUCRED
28 #include <ucred.h>
29 #endif
30
31 #ifdef LDAP_PF_LOCAL_SENDMSG
32 #include <lber.h>
33 #ifdef HAVE_SYS_UIO_H
34 #include <sys/uio.h>
35 #endif
36 #include <sys/stat.h>
37 #endif
38
39 #ifdef HAVE_SYS_UCRED_H
40 #ifdef HAVE_GRP_H
41 #include <grp.h>        /* for NGROUPS on Tru64 5.1 */
42 #endif
43 #include <sys/ucred.h>
44 #endif
45
46 #include <stdlib.h>
47
48 int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
49 #ifdef LDAP_PF_LOCAL_SENDMSG
50         , struct berval *peerbv
51 #endif
52         )
53 {
54 #ifdef LDAP_PF_LOCAL
55 #if defined( HAVE_GETPEERUCRED )
56         ucred_t *uc = NULL;
57         if( getpeerucred( s, &uc ) == 0 )  {
58                 *euid = ucred_geteuid( uc );
59                 *egid = ucred_getegid( uc );
60                 ucred_free( uc );
61         }
62
63 #elif defined( SO_PEERCRED )
64         struct ucred peercred;
65         ber_socklen_t peercredlen = sizeof peercred;
66
67         if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
68                 (void *)&peercred, &peercredlen ) == 0 )
69                 && ( peercredlen == sizeof peercred ))
70         {
71                 *euid = peercred.uid;
72                 *egid = peercred.gid;
73                 return 0;
74         }
75
76 #elif defined( LOCAL_PEERCRED )
77         struct xucred peercred;
78         ber_socklen_t peercredlen = sizeof peercred;
79
80         if(( getsockopt( s, LOCAL_PEERCRED, 1,
81                 (void *)&peercred, &peercredlen ) == 0 )
82                 && ( peercred.cr_version == XUCRED_VERSION ))
83         {
84                 *euid = peercred.cr_uid;
85                 *egid = peercred.cr_gid;
86                 return 0;
87         }
88 #elif defined( LDAP_PF_LOCAL_SENDMSG ) && defined( MSG_WAITALL )
89         int err, fd;
90         struct iovec iov;
91         struct msghdr msg = {0};
92 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
93 # ifndef CMSG_SPACE
94 # define CMSG_SPACE(len)        (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
95 # endif
96 # ifndef CMSG_LEN
97 # define CMSG_LEN(len)          (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
98 # endif
99         struct {
100                 struct cmsghdr cm;
101                 int fd;
102         } control_st;
103         struct cmsghdr *cmsg;
104 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
105         struct stat st;
106         struct sockaddr_un lname, rname;
107         ber_socklen_t llen, rlen;
108
109         rlen = sizeof(rname);
110         llen = sizeof(lname);
111         memset( &lname, 0, sizeof( lname ));
112         getsockname(s, (struct sockaddr *)&lname, &llen);
113
114         iov.iov_base = peerbv->bv_val;
115         iov.iov_len = peerbv->bv_len;
116         msg.msg_iov = &iov;
117         msg.msg_iovlen = 1;
118         peerbv->bv_len = 0;
119
120 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
121         msg.msg_control = &control_st;
122         msg.msg_controllen = sizeof( struct cmsghdr ) + sizeof( int );  /* no padding! */
123
124         cmsg = CMSG_FIRSTHDR( &msg );
125 # else
126         msg.msg_accrights = (char *)&fd;
127         msg.msg_accrightslen = sizeof(fd);
128 # endif
129
130         /*
131          * AIX returns a bogus file descriptor if recvmsg() is
132          * called with MSG_PEEK (is this a bug?). Hence we need
133          * to receive the Abandon PDU.
134          */
135         err = recvmsg( s, &msg, MSG_WAITALL );
136         if( err >= 0 &&
137 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
138             cmsg->cmsg_len == CMSG_LEN( sizeof(int) ) &&
139             cmsg->cmsg_level == SOL_SOCKET &&
140             cmsg->cmsg_type == SCM_RIGHTS
141 # else
142                 msg.msg_accrightslen == sizeof(int)
143 # endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
144         ) {
145                 int mode = S_IFIFO|S_ISUID|S_IRWXU;
146
147                 /* We must receive a valid descriptor, it must be a pipe,
148                  * it must only be accessible by its owner, and it must
149                  * have the name of our socket written on it.
150                  */
151                 peerbv->bv_len = err;
152 # ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
153                 fd = (*(int *)CMSG_DATA( cmsg ));
154 # endif
155                 err = fstat( fd, &st );
156                 if ( err == 0 )
157                         rlen = read(fd, &rname, rlen);
158                 close(fd);
159                 if( err == 0 && st.st_mode == mode &&
160                         llen == rlen && !memcmp(&lname, &rname, llen))
161                 {
162                         *euid = st.st_uid;
163                         *egid = st.st_gid;
164                         return 0;
165                 }
166         }
167 #elif defined(SOCKCREDSIZE)
168         struct msghdr msg;
169         ber_socklen_t crmsgsize;
170         void *crmsg;
171         struct cmsghdr *cmp;
172         struct sockcred *sc;
173
174         memset(&msg, 0, sizeof msg);
175         crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
176         if (crmsgsize == 0) goto sc_err;
177         crmsg = malloc(crmsgsize);
178         if (crmsg == NULL) goto sc_err;
179         memset(crmsg, 0, crmsgsize);
180         
181         msg.msg_control = crmsg;
182         msg.msg_controllen = crmsgsize;
183         
184         if (recvmsg(s, &msg, 0) < 0) {
185                 free(crmsg);
186                 goto sc_err;
187         }       
188
189         if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
190                 free(crmsg);
191                 goto sc_err;
192         }       
193         
194         cmp = CMSG_FIRSTHDR(&msg);
195         if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) {
196                 printf("nocreds\n");
197                 goto sc_err;
198         }       
199         
200         sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
201         
202         *euid = sc->sc_euid;
203         *egid = sc->sc_egid;
204
205         free(crmsg);
206         return 0;
207
208 sc_err: 
209 #endif
210 #endif /* LDAP_PF_LOCAL */
211
212         return -1;
213 }
214
215 #endif /* HAVE_GETPEEREID */