- Kill some warnings, and enable WARNS=1.
- Use CMSG_SPACE() and CMSG_DATA() properly; don't define structures to hold messages, which will not have the correct alignment after the cmsg header.
This commit is contained in:
parent
934426175d
commit
d20c2ec7d2
@ -1,8 +1,10 @@
|
||||
# $NetBSD: Makefile,v 1.5 2000/01/24 03:01:54 mycroft Exp $
|
||||
# $NetBSD: Makefile,v 1.6 2000/06/05 06:01:42 thorpej Exp $
|
||||
|
||||
PROG= unfdpass
|
||||
MKMAN= no
|
||||
|
||||
WARNS?= 1
|
||||
|
||||
CLEANFILES+=file1 file2 file3 file4 file5 file6 file7 file8 file9 file10 \
|
||||
file11 file12 file13 file14 file15 file16 file17 file18 file19 \
|
||||
file20 file21 file22 file23 file24 output test-sock
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: unfdpass.c,v 1.5 1999/03/22 18:19:54 sommerfe Exp $ */
|
||||
/* $NetBSD: unfdpass.c,v 1.6 2000/06/05 06:01:42 thorpej Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 1998 The NetBSD Foundation, Inc.
|
||||
@ -54,6 +54,7 @@
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define SOCK_NAME "test-sock"
|
||||
@ -67,15 +68,11 @@ void usage __P((char *progname));
|
||||
#define MSG_SIZE -1
|
||||
#define NFILES 24
|
||||
|
||||
struct fdcmessage {
|
||||
struct cmsghdr cm;
|
||||
int files[NFILES];
|
||||
};
|
||||
#define FDCM_DATASIZE (sizeof(int) * NFILES)
|
||||
#define CRCM_DATASIZE (SOCKCREDSIZE(NGROUPS))
|
||||
|
||||
struct crcmessage {
|
||||
struct cmsghdr cm;
|
||||
char creds[SOCKCREDSIZE(NGROUPS)];
|
||||
};
|
||||
#define MESSAGE_SIZE (CMSG_SPACE(FDCM_DATASIZE) + \
|
||||
CMSG_SPACE(CRCM_DATASIZE))
|
||||
|
||||
int chroot_rcvr = 0;
|
||||
int pass_dir = 0;
|
||||
@ -96,21 +93,21 @@ main(argc, argv)
|
||||
#endif
|
||||
char *progname=argv[0];
|
||||
struct msghdr msg;
|
||||
int listensock, sock, fd, i, status;
|
||||
int listensock, sock, fd, i;
|
||||
char fname[16], buf[FILE_SIZE];
|
||||
struct cmsghdr *cmp;
|
||||
struct {
|
||||
struct fdcmessage fdcm;
|
||||
struct crcmessage crcm;
|
||||
} message;
|
||||
void *message;
|
||||
int *files = NULL;
|
||||
struct sockcred *sc = NULL;
|
||||
struct sockaddr_un sun, csun;
|
||||
int csunlen;
|
||||
fd_set oob;
|
||||
pid_t pid;
|
||||
int ch;
|
||||
|
||||
|
||||
message = malloc(CMSG_SPACE(MESSAGE_SIZE));
|
||||
if (message == NULL)
|
||||
err(1, "unable to malloc message buffer");
|
||||
memset(message, 0, CMSG_SPACE(MESSAGE_SIZE));
|
||||
|
||||
while ((ch = getopt(argc, argv, "DESdepr")) != -1) {
|
||||
switch(ch) {
|
||||
@ -228,10 +225,11 @@ main(argc, argv)
|
||||
* Grab the descriptors and credentials passed to us.
|
||||
*/
|
||||
|
||||
/* Expect 2 messages; descriptors and creds. */
|
||||
do {
|
||||
(void) memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_control = (caddr_t) &message;
|
||||
msg.msg_controllen = sizeof(message);
|
||||
msg.msg_control = message;
|
||||
msg.msg_controllen = MESSAGE_SIZE;
|
||||
#if MSG_SIZE >= 0
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = MSG_SIZE;
|
||||
@ -243,7 +241,6 @@ main(argc, argv)
|
||||
err(1, "recvmsg");
|
||||
|
||||
(void) close(sock);
|
||||
|
||||
sock = -1;
|
||||
|
||||
if (msg.msg_controllen == 0)
|
||||
@ -252,7 +249,6 @@ main(argc, argv)
|
||||
if (msg.msg_flags & MSG_CTRUNC)
|
||||
errx(1, "lost control message data");
|
||||
|
||||
cmp = CMSG_FIRSTHDR(&msg);
|
||||
for (cmp = CMSG_FIRSTHDR(&msg); cmp != NULL;
|
||||
cmp = CMSG_NXTHDR(&msg, cmp)) {
|
||||
if (cmp->cmsg_level != SOL_SOCKET)
|
||||
@ -261,15 +257,17 @@ main(argc, argv)
|
||||
|
||||
switch (cmp->cmsg_type) {
|
||||
case SCM_RIGHTS:
|
||||
if (cmp->cmsg_len != sizeof(message.fdcm))
|
||||
errx(1, "bad fd control message length");
|
||||
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 < sizeof(struct sockcred))
|
||||
errx(1, "bad cred control message length");
|
||||
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;
|
||||
@ -279,7 +277,6 @@ main(argc, argv)
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read the files and print their contents.
|
||||
@ -323,7 +320,6 @@ main(argc, argv)
|
||||
printf("Credentials do NOT match.\n");
|
||||
}
|
||||
} while (sock != -1);
|
||||
|
||||
|
||||
/*
|
||||
* All done!
|
||||
@ -355,13 +351,21 @@ child()
|
||||
struct iovec iov;
|
||||
#endif
|
||||
struct msghdr msg;
|
||||
char fname[16], buf[FILE_SIZE];
|
||||
char fname[16];
|
||||
struct cmsghdr *cmp;
|
||||
struct fdcmessage fdcm;
|
||||
int i, fd, sock, nfd;
|
||||
void *fdcm;
|
||||
int i, fd, sock, nfd, *files;
|
||||
struct sockaddr_un sun;
|
||||
int spair[2];
|
||||
|
||||
|
||||
fdcm = malloc(CMSG_SPACE(FDCM_DATASIZE));
|
||||
if (fdcm == NULL)
|
||||
err(1, "unable to malloc fd control message");
|
||||
memset(fdcm, 0, CMSG_SPACE(FDCM_DATASIZE));
|
||||
|
||||
cmp = fdcm;
|
||||
files = (int *)CMSG_DATA(fdcm);
|
||||
|
||||
/*
|
||||
* Create socket and connect to the receiver.
|
||||
*/
|
||||
@ -380,10 +384,8 @@ child()
|
||||
i = 0;
|
||||
|
||||
if (pass_sock) {
|
||||
fdcm.files[i++] = sock;
|
||||
files[i++] = sock;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (pass_dir)
|
||||
nfd--;
|
||||
@ -397,7 +399,7 @@ child()
|
||||
(void) sprintf(fname, "file%d", i + 1);
|
||||
if ((fd = open(fname, O_RDONLY, 0666)) == -1)
|
||||
err(1, "child open %s", fname);
|
||||
fdcm.files[i] = fd;
|
||||
files[i] = fd;
|
||||
}
|
||||
|
||||
if (pass_dir) {
|
||||
@ -407,12 +409,12 @@ child()
|
||||
if ((fd = open(dirname, O_RDONLY, 0)) == -1) {
|
||||
err(1, "child open directory %s", dirname);
|
||||
}
|
||||
fdcm.files[i] = fd;
|
||||
files[i] = fd;
|
||||
}
|
||||
|
||||
(void) memset(&msg, 0, sizeof(msg));
|
||||
msg.msg_control = (caddr_t) &fdcm;
|
||||
msg.msg_controllen = sizeof(fdcm);
|
||||
msg.msg_control = fdcm;
|
||||
msg.msg_controllen = CMSG_LEN(FDCM_DATASIZE);
|
||||
#if MSG_SIZE >= 0
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = MSG_SIZE;
|
||||
@ -421,7 +423,7 @@ child()
|
||||
#endif
|
||||
|
||||
cmp = CMSG_FIRSTHDR(&msg);
|
||||
cmp->cmsg_len = sizeof(fdcm);
|
||||
cmp->cmsg_len = CMSG_LEN(FDCM_DATASIZE);
|
||||
cmp->cmsg_level = SOL_SOCKET;
|
||||
cmp->cmsg_type = SCM_RIGHTS;
|
||||
|
||||
@ -433,14 +435,12 @@ child()
|
||||
if (sendmsg(spair[0], &msg, 0) < 0)
|
||||
err(1, "child prezel sendmsg");
|
||||
|
||||
close(fdcm.files[0]);
|
||||
close(fdcm.files[1]);
|
||||
fdcm.files[0] = spair[0];
|
||||
fdcm.files[1] = spair[1];
|
||||
close(files[0]);
|
||||
close(files[1]);
|
||||
files[0] = spair[0];
|
||||
files[1] = spair[1];
|
||||
make_pretzel--;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (sendmsg(sock, &msg, 0) == -1)
|
||||
err(1, "child sendmsg");
|
||||
|
Loading…
Reference in New Issue
Block a user