getpeereid.c 4.87 KB
Newer Older
1
2
/* getpeereid.c */
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2008 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
15
16
17
18
19
20
21
22
23
24
 */

#include "portable.h"

#ifndef HAVE_GETPEEREID

#include <sys/types.h>
#include <ac/unistd.h>

#include <ac/socket.h>
25
#include <ac/errno.h>
26

27
28
29
30
#ifdef HAVE_GETPEERUCRED
#include <ucred.h>
#endif

31
32
33
34
35
36
37
38
#ifdef LDAP_PF_LOCAL_SENDMSG
#include <lber.h>
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#include <sys/stat.h>
#endif

39
40
#ifdef HAVE_SYS_UCRED_H
#ifdef HAVE_GRP_H
41
42
#include <grp.h>	/* for NGROUPS on Tru64 5.1 */
#endif
43
44
45
#include <sys/ucred.h>
#endif

46
47
#include <stdlib.h>

48
49
50
51
52
int lutil_getpeereid( int s, uid_t *euid, gid_t *egid
#ifdef LDAP_PF_LOCAL_SENDMSG
	, struct berval *peerbv
#endif
	)
53
54
{
#ifdef LDAP_PF_LOCAL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
#if defined( HAVE_GETPEERUCRED )
56
57
58
59
60
	ucred_t *uc = NULL;
	if( getpeerucred( s, &uc ) == 0 )  {
		*euid = ucred_geteuid( uc );
		*egid = ucred_getegid( uc );
		ucred_free( uc );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
61
		return 0;
62
63
64
	}

#elif defined( SO_PEERCRED )
65
	struct ucred peercred;
66
	ber_socklen_t peercredlen = sizeof peercred;
67
68
69
70
71
72
73
74
75
76
77
78

	if(( getsockopt( s, SOL_SOCKET, SO_PEERCRED,
		(void *)&peercred, &peercredlen ) == 0 )
		&& ( peercredlen == sizeof peercred ))
	{
		*euid = peercred.uid;
		*egid = peercred.gid;
		return 0;
	}

#elif defined( LOCAL_PEERCRED )
	struct xucred peercred;
79
	ber_socklen_t peercredlen = sizeof peercred;
80
81
82
83
84
85
86
87
88

	if(( getsockopt( s, LOCAL_PEERCRED, 1,
		(void *)&peercred, &peercredlen ) == 0 )
		&& ( peercred.cr_version == XUCRED_VERSION ))
	{
		*euid = peercred.cr_uid;
		*egid = peercred.cr_gid;
		return 0;
	}
89
90
#elif defined( LDAP_PF_LOCAL_SENDMSG ) && defined( MSG_WAITALL )
	int err, fd;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
91
	struct iovec iov;
92
	struct msghdr msg = {0};
Kurt Zeilenga's avatar
Kurt Zeilenga committed
93
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
94
95
96
97
98
99
# ifndef CMSG_SPACE
# define CMSG_SPACE(len)	(_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len))
# endif
# ifndef CMSG_LEN
# define CMSG_LEN(len)		(_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
# endif
100
	struct {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
		struct cmsghdr cm;
102
103
		int fd;
	} control_st;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
	struct cmsghdr *cmsg;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
105
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
106
	struct stat st;
107
108
	struct sockaddr_un lname, rname;
	ber_socklen_t llen, rlen;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
109

110
111
112
113
	rlen = sizeof(rname);
	llen = sizeof(lname);
	memset( &lname, 0, sizeof( lname ));
	getsockname(s, (struct sockaddr *)&lname, &llen);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114

115
116
	iov.iov_base = peerbv->bv_val;
	iov.iov_len = peerbv->bv_len;
117
118
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
119
120
	peerbv->bv_len = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
121
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
122
123
	msg.msg_control = &control_st;
	msg.msg_controllen = sizeof( struct cmsghdr ) + sizeof( int );	/* no padding! */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
124
125

	cmsg = CMSG_FIRSTHDR( &msg );
126
127
128
129
# else
	msg.msg_accrights = (char *)&fd;
	msg.msg_accrightslen = sizeof(fd);
# endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
130
131
132
133
134
135

	/*
	 * AIX returns a bogus file descriptor if recvmsg() is
	 * called with MSG_PEEK (is this a bug?). Hence we need
	 * to receive the Abandon PDU.
	 */
136
137
138
	err = recvmsg( s, &msg, MSG_WAITALL );
	if( err >= 0 &&
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
139
140
	    cmsg->cmsg_len == CMSG_LEN( sizeof(int) ) &&
	    cmsg->cmsg_level == SOL_SOCKET &&
141
	    cmsg->cmsg_type == SCM_RIGHTS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
142
# else
143
		msg.msg_accrightslen == sizeof(int)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
144
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
145
146
147
	) {
		int mode = S_IFIFO|S_ISUID|S_IRWXU;

148
		/* We must receive a valid descriptor, it must be a pipe,
149
150
		 * it must only be accessible by its owner, and it must
		 * have the name of our socket written on it.
151
		 */
152
		peerbv->bv_len = err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
153
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
154
		fd = (*(int *)CMSG_DATA( cmsg ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
# endif
156
157
158
159
160
161
		err = fstat( fd, &st );
		if ( err == 0 )
			rlen = read(fd, &rname, rlen);
		close(fd);
		if( err == 0 && st.st_mode == mode &&
			llen == rlen && !memcmp(&lname, &rname, llen))
162
163
164
165
166
167
		{
			*euid = st.st_uid;
			*egid = st.st_gid;
			return 0;
		}
	}
168
#elif defined(SOCKCREDSIZE)
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
169
	struct msghdr msg;
170
	ber_socklen_t crmsgsize;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
171
172
173
	void *crmsg;
	struct cmsghdr *cmp;
	struct sockcred *sc;
174
175
176

	memset(&msg, 0, sizeof msg);
	crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
177
	if (crmsgsize == 0) goto sc_err;
178
	crmsg = malloc(crmsgsize);
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
179
	if (crmsg == NULL) goto sc_err;
180
181
182
183
184
185
	memset(crmsg, 0, crmsgsize);
	
	msg.msg_control = crmsg;
	msg.msg_controllen = crmsgsize;
	
	if (recvmsg(s, &msg, 0) < 0) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
186
187
		free(crmsg);
		goto sc_err;
188
189
190
	}	

	if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
191
192
		free(crmsg);
		goto sc_err;
193
194
195
196
	}	
	
	cmp = CMSG_FIRSTHDR(&msg);
	if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
197
198
		printf("nocreds\n");
		goto sc_err;
199
200
201
202
203
204
205
206
207
	}	
	
	sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
	
	*euid = sc->sc_euid;
	*egid = sc->sc_egid;

	free(crmsg);
	return 0;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
208
209

sc_err:	
210
#endif
211
#endif /* LDAP_PF_LOCAL */
212
213
214
215

	return -1;
}

216
#endif /* HAVE_GETPEEREID */