getpeereid.c 4.39 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-2007 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

#if HAVE_SYS_UCRED_H
28
29
30
#if HAVE_GRP_H
#include <grp.h>	/* for NGROUPS on Tru64 5.1 */
#endif
31
32
33
#include <sys/ucred.h>
#endif

34
35
/* Disabled due to ITS#4893, will revisit in release 2.4 */
#if 0 /* !defined(SO_PEERCRED) && !defined(LOCAL_PEERCRED) && \
36
	defined(HAVE_SENDMSG) && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTSLEN) || \
37
		defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL)) */
38
#define DO_SENDMSG
39
40
41
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
42
43
44
#include <sys/stat.h>
#endif

Kurt Zeilenga's avatar
Kurt Zeilenga committed
45
46
47
#include <stdlib.h>


48
49
50
51
52
int getpeereid( int s, uid_t *euid, gid_t *egid )
{
#ifdef LDAP_PF_LOCAL
#if defined( SO_PEERCRED )
	struct ucred peercred;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
53
	socklen_t peercredlen = sizeof peercred;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

	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;
	socklen_t peercredlen = sizeof peercred;

	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;
	}
76
#elif defined( DO_SENDMSG )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
78
	char dummy[8];
	int err, fd[2];
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
	struct iovec iov;
80
	struct msghdr msg = {0};
81
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82
83
84
85
86
87
88
89
90
91
92
# 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
	union {
		struct cmsghdr cm;
		unsigned char control[CMSG_SPACE(sizeof(int))];
	} control_un;
	struct cmsghdr *cmsg;
93
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL */
94
	struct stat st;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
95

Kurt Zeilenga's avatar
Kurt Zeilenga committed
96
97
98
99
100
	msg.msg_name = NULL;
	msg.msg_namelen = 0;

	iov.iov_base = dummy;
	iov.iov_len = sizeof dummy;
101
102
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
103
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
	msg.msg_control = control_un.control;
	msg.msg_controllen = sizeof( control_un.control );

	cmsg = CMSG_FIRSTHDR( &msg );

	/*
	 * 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.
	 */
	if( recvmsg( s, &msg, MSG_WAITALL ) >= 0 &&
	    cmsg->cmsg_len == CMSG_LEN( sizeof(int) ) &&
	    cmsg->cmsg_level == SOL_SOCKET &&
	    cmsg->cmsg_type == SCM_RIGHTS )
# else
119
120
121
	msg.msg_accrights = (char *)fd;
	msg.msg_accrightslen = sizeof(fd);
	if( recvmsg( s, &msg, MSG_PEEK) >= 0 && msg.msg_accrightslen == sizeof(int) )
122
# endif /* HAVE_STRUCT_MSGHDR_MSG_CONTROL*/
123
124
125
126
	{
		/* We must receive a valid descriptor, it must be a pipe,
		 * and it must only be accessible by its owner.
		 */
127
# ifdef HAVE_STRUCT_MSGHDR_MSG_CONTROL
Kurt Zeilenga's avatar
Kurt Zeilenga committed
128
129
130
		fd[0] = (*(int *)CMSG_DATA( cmsg ));
# endif
		err = fstat( fd[0], &st );
131
		close(fd[0]);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
132
		if( err == 0 && S_ISFIFO(st.st_mode) &&
133
134
135
136
137
138
139
			((st.st_mode & (S_IRWXG|S_IRWXO)) == 0))
		{
			*euid = st.st_uid;
			*egid = st.st_gid;
			return 0;
		}
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
140
#elif defined(SOCKCREDSIZE)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
141
142
143
144
145
	struct msghdr msg;
	socklen_t crmsgsize;
	void *crmsg;
	struct cmsghdr *cmp;
	struct sockcred *sc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
147
148

	memset(&msg, 0, sizeof msg);
	crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
	if (crmsgsize == 0) goto sc_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
150
	crmsg = malloc(crmsgsize);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
151
	if (crmsg == NULL) goto sc_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
152
153
154
155
156
157
	memset(crmsg, 0, crmsgsize);
	
	msg.msg_control = crmsg;
	msg.msg_controllen = crmsgsize;
	
	if (recvmsg(s, &msg, 0) < 0) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
158
159
		free(crmsg);
		goto sc_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
160
161
162
	}	

	if (msg.msg_controllen == 0 || (msg.msg_flags & MSG_CTRUNC) != 0) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163
164
		free(crmsg);
		goto sc_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
165
166
167
168
	}	
	
	cmp = CMSG_FIRSTHDR(&msg);
	if (cmp->cmsg_level != SOL_SOCKET || cmp->cmsg_type != SCM_CREDS) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
170
		printf("nocreds\n");
		goto sc_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
171
172
173
174
175
176
177
178
179
	}	
	
	sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
	
	*euid = sc->sc_euid;
	*egid = sc->sc_egid;

	free(crmsg);
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
180
181

sc_err:	
182
#endif
183
#endif /* LDAP_PF_LOCAL */
184
185
186
187

	return -1;
}

188
#endif /* HAVE_GETPEEREID */