summaryrefslogtreecommitdiffabout
path: root/src/net.c
blob: 25ef71d5a06ededb7b73ee6b0e404f14706a6cab (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009-2013, 2017, 2019-2020 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

struct grecs_sockaddr listen_sockaddr;
size_t max_connections = 16;
time_t idle_timeout = 5;

static int
open_listener()
{
    int fd;

    if (listen_sockaddr.sa == NULL)
	return -1;

    fd = socket(listen_sockaddr.sa->sa_family, SOCK_STREAM, 0);
    if (fd == -1) {
	wy_log(LOG_CRIT, _("cannot create socket: %s"), strerror(errno));
	exit(EX_OSERR);
    }
    if (listen_sockaddr.sa->sa_family == AF_UNIX) {
	struct stat st;
	struct sockaddr_un *s_un =
	    (struct sockaddr_un *) listen_sockaddr.sa;
	if (stat(s_un->sun_path, &st)) {
	    if (errno != ENOENT) {
		wy_log(LOG_CRIT,
		       _("%s: cannot stat socket: %s"),
		       s_un->sun_path, strerror(errno));
		exit(errno == EACCES ? EX_NOPERM : EX_OSERR);
	    }
	} else {
	    /* FIXME: Check permissions? */
	    if (!S_ISSOCK(st.st_mode)) {
		wy_log(LOG_CRIT, _("%s: not a socket"), s_un->sun_path);
		exit(EX_OSFILE);
	    }
	    unlink(s_un->sun_path);
	}
	/* FIXME: Setup umask */
    } else {
	int yes = 1;
	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *) &yes,
		   sizeof(yes));
    }

    if (bind(fd, listen_sockaddr.sa, listen_sockaddr.len) < 0) {
	wy_log(LOG_CRIT, _("cannot bind to local address: %s"),
	       strerror(errno));
	close(fd);
	exit(EX_OSERR);
    }
    if (listen(fd, 8) == -1) {
	wy_log(LOG_CRIT, "listen: %s", strerror(errno));
	close(fd);
	exit(EX_OSERR);
    }

    return fd;
}

void
trim_crlf(char *s)
{
    size_t len = strlen(s);
    if (len > 0 && s[len - 1] == '\n') {
	s[--len] = 0;
	if (len > 0 && s[len - 1] == '\r')
	    s[--len] = 0;
    }
}

struct wydawca_connection {
    struct timespec ts;
    pthread_t tid;
    FILE *fp;
    TAILQ_ENTRY(wydawca_connection) link;
};

typedef TAILQ_HEAD(,wydawca_connection) WYDAWCA_CONNECTION_QUEUE;

static inline void
wydawca_connection_enqueue(WYDAWCA_CONNECTION_QUEUE *q,
			   struct wydawca_connection *conn)
{
    TAILQ_INSERT_TAIL(q, conn, link);
}

static inline void
wydawca_connection_dequeue(WYDAWCA_CONNECTION_QUEUE *q,
			   struct wydawca_connection *conn)
{
    TAILQ_REMOVE(q, conn, link);
}

static struct wydawca_connection *conn_table;
static WYDAWCA_CONNECTION_QUEUE conn_avail = TAILQ_HEAD_INITIALIZER(conn_avail),
                                conn_idle = TAILQ_HEAD_INITIALIZER(conn_idle);
static pthread_mutex_t conn_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t conn_cond = PTHREAD_COND_INITIALIZER;

static void
conn_table_init(void)
{
    size_t i;

    conn_table = grecs_calloc(max_connections, sizeof(conn_table[0]));
    for (i = 1; i < max_connections; i++) {
	wydawca_connection_enqueue(&conn_avail, &conn_table[i]);
    }
}

static void
connection_start(int fd)
{
    struct wydawca_connection *conn;

    pthread_mutex_lock(&conn_mutex);
    if ((conn = TAILQ_FIRST(&conn_avail)) != NULL) {
	wydawca_connection_dequeue(&conn_avail, conn);
	clock_gettime(CLOCK_REALTIME, &conn->ts);
	conn->ts.tv_sec += idle_timeout;
	conn->fp = fdopen(fd, "w+");
	pthread_create(&conn->tid, NULL, wy_thr_tcpmux, conn);
	wydawca_connection_enqueue(&conn_idle, conn);
	pthread_cond_broadcast(&conn_cond);	
    } else {	
	wy_log(LOG_ERR, "connection table is full");
	close(fd);
    }
    pthread_mutex_unlock(&conn_mutex);	
}

static void
connection_unidle(struct wydawca_connection *conn)
{
    pthread_mutex_lock(&conn_mutex);
    fclose(conn->fp);
    conn->fp = NULL;
    wydawca_connection_dequeue(&conn_idle, conn);
    pthread_cond_broadcast(&conn_cond);	
    pthread_mutex_unlock(&conn_mutex);	
}

static void
connection_stop(struct wydawca_connection *conn)
{
    pthread_mutex_lock(&conn_mutex);
    if (conn->fp) {
	fclose(conn->fp);
	conn->fp = NULL;
	wydawca_connection_dequeue(&conn_idle, conn);
	pthread_cond_broadcast(&conn_cond);	
    }
    wydawca_connection_enqueue(&conn_avail, conn);
    pthread_mutex_unlock(&conn_mutex);	
}

static inline int
timespec_cmp(struct timespec const *a, struct timespec const *b)
{
    if (a->tv_sec > b->tv_sec)
	return 1;
    if (a->tv_sec < b->tv_sec)
	return -1;
    if (a->tv_nsec > b->tv_nsec)
	return 1;
    if (a->tv_nsec < b->tv_nsec)
	return -1;
    return 0;
}

static void
handle_connection(struct wydawca_connection *conn)
{
    char *buf = NULL;
    size_t buflen = 0;
    struct spool *spool;

    if (grecs_getline(&buf, &buflen, conn->fp) <= 0)
	return;
    trim_crlf(buf);
    wy_debug(1, ("recv: %s", buf));
    spool = wydawca_find_spool(buf);
    if (!spool) {
	if (all_spool_aliases && grecs_list_locate(all_spool_aliases, buf))
	    fprintf(conn->fp, "+ OK, all spools\r\n");
	else {
	    fprintf(conn->fp, "- Unknown service name\r\n");
	    free(buf);
	    return;
	}
    } else if (spool->url)
	fprintf(conn->fp, "+ OK, URL %s\r\n", spool->url);
    else
	fprintf(conn->fp, "+ OK, spool %s\r\n", spool->tag);

    if (grecs_getline(&buf, &buflen, conn->fp) < 0) {
	wy_log(LOG_ERR, "protocol error");
	free(buf);
	return;
    }
    connection_unidle(conn);
    
    trim_crlf(buf);
    wy_debug(1, ("recv: %s", buf));

    /* FIXME: User name not used any more */

    if (spool)
	scan_spool(spool);
    else
	scan_all_spools();
    free(buf);
}

void *
wy_thr_tcpmux(void *ptr)
{
    struct wydawca_connection *conn = ptr;
    wy_set_cur_thread_name("WY_tcpmux");
    pthread_cleanup_push((void (*)(void*))connection_stop, conn);
    setlinebuf(conn->fp);
    handle_connection(conn);
    pthread_cleanup_pop(1);
    return NULL;
}

void *
wy_thr_connection_watcher(void *ptr)
{
    wy_set_cur_thread_name("WY_connwatch");
    pthread_mutex_lock(&conn_mutex);
    while (1) {
	struct wydawca_connection *conn;
	
	if ((conn = TAILQ_FIRST(&conn_idle)) != NULL) {
	    struct timespec ts;
	    
	    pthread_cond_timedwait(&conn_cond, &conn_mutex, &conn->ts);
	    if ((conn = TAILQ_FIRST(&conn_idle)) != NULL) {
		clock_gettime(CLOCK_REALTIME, &ts);
		if (timespec_cmp(&ts, &conn->ts) >= 0) {
		    void *ret;
		    pthread_t tid = conn->tid;
		    pthread_cancel(tid);
		    pthread_mutex_unlock(&conn_mutex);
		    pthread_join(tid, &ret);
		    pthread_mutex_lock(&conn_mutex);
		}
	    }
	} else
	    pthread_cond_wait(&conn_cond, &conn_mutex);
    }
}
    
static inline int
notify_parent(void)
{
    char *p = getenv("WYDAWCA_NOTIFY_PARENT");
    return (p && strcmp(p, "1") == 0);
}

void *
wy_thr_listen(void *ptr)
{
    int ctlfd = open_listener();
    int wfd = watcher_init();
    int maxfd = 0;

    wy_set_cur_thread_name("WY_listener");
    
    if (ctlfd != -1) {
	pthread_t tid;
	pthread_create(&tid, NULL, wy_thr_connection_watcher, NULL);
	maxfd = ctlfd;
    }
    
    if (wfd != -1 && wfd > maxfd)
	maxfd = wfd;

    if (maxfd == 0) {
	wy_log(LOG_CRIT,
	       _("listener address is not configured and inotify "
		 "is not available"));
	exit(EX_CONFIG);
    }

    conn_table_init();
    if (notify_parent())
	kill(getppid(), SIGUSR1);
    
    while (1) {
	int rc;
	fd_set rset;

	FD_ZERO(&rset);
	if (ctlfd != -1)
	    FD_SET(ctlfd, &rset);
	if (wfd != -1)
	    FD_SET(wfd, &rset);

	rc = select(maxfd + 1, &rset, NULL, NULL, NULL);
	if (rc == 0)
	    continue;
	else if (rc < 0) {
	    if (errno == EINTR)
		continue;
	    wy_log(LOG_ERR, "select: %s", strerror(errno));
	    break;
	}

	if (wfd != -1 && FD_ISSET(wfd, &rset)) {
	    watcher_run(wfd);
	}

	if (ctlfd != -1 && FD_ISSET(ctlfd, &rset)) {
	    int fd;
	    union {
		struct sockaddr sa;
		struct sockaddr_in s_in;
		struct sockaddr_un s_un;
	    } addr;
	    socklen_t len;
		
	    len = sizeof(addr);
	    fd = accept(ctlfd, (struct sockaddr *) &addr, &len);
	    if (fd == -1)
		continue;
	    /* FIXME: Use Mailutils ACLs? */
#ifdef WITH_LIBWRAP
	    if (!tcpwrap_access(fd)) {
		close(fd);
		continue;
	    }
#endif
	    connection_start(fd);
	}
    }
    return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.