/*-
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Test passing of file descriptors and credentials over Unix domain sockets.
*/
if (recvmsg(sock, &msg, 0) == -1)
err(1, "recvmsg");
(void) close(sock);
sock = -1;
if (msg.msg_controllen == 0)
errx(1, "no control messages received");
if (msg.msg_flags & MSG_CTRUNC)
errx(1, "lost control message data");
for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
cmp = CMSG_NXTHDR(&msg, cmp)) {
if (cmp->cmsg_level != SOL_SOCKET)
errx(1, "bad control message level %d",
cmp->cmsg_level);
switch (cmp->cmsg_type) {
case SCM_RIGHTS:
if (cmp->cmsg_len != CMSG_LEN(FDCM_DATASIZE))
errx(1, "bad fd control message "
"length %d", cmp->cmsg_len);
files = (int *)CMSG_DATA(cmp);
break;
case SCM_CREDS:
if (cmp->cmsg_len < CMSG_LEN(SOCKCREDSIZE(1)))
errx(1, "bad cred control message "
"length %d", cmp->cmsg_len);
sc = (struct sockcred *)CMSG_DATA(cmp);
break;
default:
errx(1, "unexpected control message");
/* NOTREACHED */
}
}
/*
* Read the files and print their contents.
*/
if (files == NULL)
warnx("didn't get fd control message");
else {
for (i = 0; i < NFILES; i++) {
struct stat st;
(void) memset(buf, 0, sizeof(buf));
fstat(files[i], &st);
if (S_ISDIR(st.st_mode)) {
printf("file %d is a directory\n", i+1);
} else if (S_ISSOCK(st.st_mode)) {
printf("file %d is a socket\n", i+1);
sock = files[i];
} else {
int c;
c = read (files[i], buf, sizeof(buf));
if (c < 0)
err(1, "read file %d", i + 1);
else if (c == 0)
printf("[eof on %d]\n", i + 1);
else
printf("%s", buf);
}
}
}
/*
* Double-check credentials.
*/
if (sc == NULL)
warnx("didn't get cred control message");
else {
if (sc->sc_uid == getuid() &&
sc->sc_euid == geteuid() &&
sc->sc_gid == getgid() &&
sc->sc_egid == getegid())
printf("Credentials match.\n");
else
printf("Credentials do NOT match.\n");
}
} while (sock != -1);