1 /* This file is part of gacopyz.
2    Copyright (C) 2005-2021 Sergey Poznyakoff
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 
21 #include <gacopyz.h>
22 
23 typedef struct macro_assoc {
24 	char **argv;
25 	char *buffer;
26 } macro_assoc_t;
27 
28 struct smfi_str {
29 	struct smfiDesc const *desc; /* parent description */
30 	int sd;                /* socket descriptor */
31 	milter_sockaddr_t addr;/* client address */
32 	socklen_t addrlen;     /* length of addr */
33         int state;             /* state; FIXME: should be enum state */
34 	unsigned long version; /* negotiated protocol version */
35         unsigned long pflags;  /* protocol flags */
36 	unsigned long mta_pflags; /* pflags supported by MTA */
37 	unsigned long aflags;  /* milter action flags (from xxfi_flags) */
38         int nmacros;           /* Number of entries in macros */
39         macro_assoc_t macros[gacopyz_stage_max]; /* Macro tables */
40 	char *req_macros[gacopyz_stage_max]; /* Required macros */
41         char *reply;           /* reply code */
42         void *privdata;        /* private data */
43 	void *closure;         /* call data */
44 };
45 
46 struct gacopyz_conn {
47 	int sd;
48 	int foreground;        /* operate in foreground mode */
49  	int stop;              /* */
50 	struct timeval master_timeout;
51 	struct smfiDesc desc;
52 	pid_t *pidtab;
53 	size_t pidcount;
54 	void (*cleanup) (gacopyz_conn_t, void*);
55 	void *cleanup_data;
56 };
57 
58 #define GACOPYZ_TIMEOUT  7210
59 
60 #ifdef GACOPYZ_UINT32_T
61 typedef GACOPYZ_UINT32_T gacopyz_uint32_t;
62 #else
63 # warning "assuming 32-bit unsigned long"
64 typedef unsigned long gacopyz_uint32_t;
65 #endif
66 
67 #define GACOPYZ_OPTLEN  (sizeof(gacopyz_uint32_t) * 3) /* length of options */
68 
69 extern void (*__gacopyz_log_printer)(int, char *, va_list);
70 
71 #define GACOPYZ_DESC_LOG_MATCH(dsc, level) \
72   ((dsc)->logmask & SMI_LOG_MASK(level))
73 #define GACOPYZ_CTX_LOG_MATCH(ctx, level) \
74   GACOPYZ_DESC_LOG_MATCH((ctx)->desc, level)
75 #define GACOPYZ_CONN_LOG_MATCH(conn, level) \
76   GACOPYZ_DESC_LOG_MATCH(&(conn)->desc, level)
77 
78 #define GACOPYZ_IPV6PREFIX_STR "IPv6:"
79 #define GACOPYZ_IPV6PREFIX_LEN (sizeof(GACOPYZ_IPV6PREFIX_STR)-1)
80 
81 
82 #ifndef timersub
83 # define timersub(a, b, result)						\
84 	do {								\
85 		(result)->tv_sec = (a)->tv_sec - (b)->tv_sec;		\
86 		(result)->tv_usec = (a)->tv_usec - (b)->tv_usec;        \
87 		if ((result)->tv_usec < 0) {                            \
88 			--(result)->tv_sec;				\
89 			(result)->tv_usec += 1000000;			\
90 		}                                                       \
91 	} while (0)
92 #endif
93 
94 
95 
96 struct gacopyz_iod {
97 	int sd;
98 	struct timeval timeout[GACOPYZ_TO_COUNT];
99 	int logmask;
100 };
101 
102 int _gacopyz_read (gacopyz_iod_t iod, char *buf, size_t size);
103 int _gacopyz_write(gacopyz_iod_t iod, const char *buf, size_t size);
104 int gacopyz_send_command(gacopyz_iod_t iod,
105 			 int cmd, const void *data, size_t size);
106 int gacopyz_read_command(gacopyz_iod_t iod, int tm, unsigned char *cmd,
107 			 size_t *pcount, char **pbuf, size_t *psize);
108 int gacopyz_register_child(gacopyz_conn_t conn, pid_t pid);
109 void gacopyz_unregister_child(gacopyz_conn_t conn, pid_t pid);
110 
111 
112 /* NLS */
113 #undef _
114 #undef N_
115 #undef gettext
116 #undef dgettext
117 #undef ngettext
118 #undef textdomain
119 #undef bindtextdomain
120 
121 #ifdef ENABLE_NLS
122 # include <gettext.h>
123 # define _(String) gettext(String)
124 # define N_(String) String
125 #else
126 # define _(String) (String)
127 # define N_(String) String
128 # define gettext(msgid) (msgid)
129 # define dgettext(domain, msgid) (msgid)
130 # define ngettext(sg,pl,cnt) (cnt == 1) ? (sg) : (pl)
131 # define textdomain(Domain)
132 # define bindtextdomain(Package, Directory)
133 #endif /* ENABLE_NLS */
134