summaryrefslogtreecommitdiffstats
path: root/src/util/pass_accept.c
blob: d06926f8e6be2992d97cb8248c7ce793394c7226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*++
/* NAME
/*	pass_accept 3
/* SUMMARY
/*	start UNIX-domain file descriptor listener
/* SYNOPSIS
/*	#include <listen.h>
/*
/*	int	pass_accept(listen_fd)
/*	int	listen_fd;
/*
/*	int	pass_accept_attr(listen_fd, attr)
/*	int	listen_fd;
/*	HTABLE	**attr;
/* DESCRIPTION
/*	This module implements a listener that receives one attribute list
/*	and file descriptor over each a local connection that is made to it.
/*
/*	Arguments:
/* .IP attr
/*	Pointer to attribute list pointer.  In case of error, or
/*	no attributes, the attribute list pointer is set to null.
/* .IP listen_fd
/*	File descriptor returned by LOCAL_LISTEN().
/* DIAGNOSTICS
/*	Warnings: I/O errors, timeout.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <errno.h>
#include <unistd.h>

/* Utility library. */

#include <msg.h>
#include <listen.h>
#include <attr.h>

#define PASS_ACCEPT_TMOUT	100

/* pass_accept - accept descriptor */

int     pass_accept(int listen_fd)
{
    const char *myname = "pass_accept";
    int     accept_fd;
    int     recv_fd = -1;

    accept_fd = LOCAL_ACCEPT(listen_fd);
    if (accept_fd < 0) {
	if (errno != EAGAIN)
	    msg_warn("%s: cannot accept connection: %m", myname);
	return (-1);
    } else {
	if (read_wait(accept_fd, PASS_ACCEPT_TMOUT) < 0)
	    msg_warn("%s: timeout receiving file descriptor: %m", myname);
	else if ((recv_fd = LOCAL_RECV_FD(accept_fd)) < 0)
	    msg_warn("%s: cannot receive file descriptor: %m", myname);
	if (close(accept_fd) < 0)
	    msg_warn("%s: close: %m", myname);
	return (recv_fd);
    }
}

/* pass_accept_attr - accept descriptor and attribute list */

int     pass_accept_attr(int listen_fd, HTABLE **attr)
{
    const char *myname = "pass_accept_attr";
    int     accept_fd;
    int     recv_fd = -1;

    *attr = 0;
    accept_fd = LOCAL_ACCEPT(listen_fd);
    if (accept_fd < 0) {
	if (errno != EAGAIN)
	    msg_warn("%s: cannot accept connection: %m", myname);
	return (-1);
    } else {
	if (read_wait(accept_fd, PASS_ACCEPT_TMOUT) < 0)
	    msg_warn("%s: timeout receiving file descriptor: %m", myname);
	else if ((recv_fd = LOCAL_RECV_FD(accept_fd)) < 0)
	    msg_warn("%s: cannot receive file descriptor: %m", myname);
	else if (read_wait(accept_fd, PASS_ACCEPT_TMOUT) < 0
	     || recv_pass_attr(accept_fd, attr, PASS_ACCEPT_TMOUT, 0) < 0) {
	    msg_warn("%s: cannot receive connection attributes: %m", myname);
	    if (close(recv_fd) < 0)
		msg_warn("%s: close: %m", myname);
	    recv_fd = -1;
	}
	if (close(accept_fd) < 0)
	    msg_warn("%s: close: %m", myname);
	return (recv_fd);
    }
}