Path: blob/main/tools/regression/sockets/unix_cmsg/t_cmsgcred_sockcred.c
96317 views
/*-1* Copyright (c) 2005 Andrey Simonenko2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/types.h>27#include <sys/socket.h>28#include <sys/un.h>29#include <inttypes.h>30#include <stdarg.h>31#include <stdbool.h>32#include <stdlib.h>3334#include "uc_common.h"35#include "t_generic.h"36#include "t_cmsgcred.h"37#include "t_cmsgcred_sockcred.h"3839static int40t_cmsgcred_sockcred_server(int fd1)41{42struct msghdr msghdr;43struct iovec iov[1];44struct cmsghdr *cmsghdr;45void *cmsg_data, *cmsg1_data, *cmsg2_data;46size_t cmsg_size, cmsg1_size, cmsg2_size;47u_int i;48int fd2, rv, val;4950fd2 = -1;51rv = -2;5253cmsg1_size = CMSG_SPACE(SOCKCREDSIZE(uc_cfg.proc_cred.gid_num));54cmsg2_size = CMSG_SPACE(sizeof(struct cmsgcred));55cmsg1_data = malloc(cmsg1_size);56cmsg2_data = malloc(cmsg2_size);57if (cmsg1_data == NULL || cmsg2_data == NULL) {58uc_logmsg("malloc");59goto done;60}6162uc_dbgmsg("setting LOCAL_CREDS");63val = 1;64if (setsockopt(fd1, 0, LOCAL_CREDS, &val, sizeof(val)) < 0) {65uc_logmsg("setsockopt(LOCAL_CREDS)");66goto done;67}6869if (uc_sync_send() < 0)70goto done;7172if (uc_cfg.sock_type == SOCK_STREAM) {73fd2 = uc_socket_accept(fd1);74if (fd2 < 0)75goto done;76} else77fd2 = fd1;7879cmsg_data = cmsg1_data;80cmsg_size = cmsg1_size;81rv = -1;82for (i = 1; i <= uc_cfg.ipc_msg.msg_num; ++i) {83uc_dbgmsg("message #%u", i);8485uc_msghdr_init_server(&msghdr, iov, cmsg_data, cmsg_size);86if (uc_message_recv(fd2, &msghdr) < 0) {87rv = -2;88break;89}9091if (uc_check_msghdr(&msghdr, sizeof(*cmsghdr)) < 0)92break;9394cmsghdr = CMSG_FIRSTHDR(&msghdr);95if (i == 1 || uc_cfg.sock_type == SOCK_DGRAM) {96if (uc_check_scm_creds_sockcred(cmsghdr) < 0)97break;98} else {99if (uc_check_scm_creds_cmsgcred(cmsghdr) < 0)100break;101}102103cmsg_data = cmsg2_data;104cmsg_size = cmsg2_size;105}106if (i > uc_cfg.ipc_msg.msg_num)107rv = 0;108done:109free(cmsg1_data);110free(cmsg2_data);111if (uc_cfg.sock_type == SOCK_STREAM && fd2 >= 0)112if (uc_socket_close(fd2) < 0)113rv = -2;114return (rv);115}116117int118t_cmsgcred_sockcred(void)119{120return (t_generic(t_cmsgcred_client, t_cmsgcred_sockcred_server));121}122123124