aboutsummaryrefslogtreecommitdiff
path: root/src/net.c
blob: 3f8d291f5222eec37e22d7b5f5c1280226106529 (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
/* 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"

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;
    }
}

void
handle_connection(FILE * in, FILE * out)
{
    char *buf = NULL;
    size_t buflen = 0;
    struct spool *spool;
    char *p;

    if (grecs_getline(&buf, &buflen, in) <= 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(out, "+ OK, all spools\r\n");
	else {
	    fprintf(out, "- Unknown service name\r\n");
	    free(buf);
	    return;
	}
    } else if (spool->url)
	fprintf(out, "+ OK, URL %s\r\n", spool->url);
    else
	fprintf(out, "+ OK, spool %s\r\n", spool->tag);

    if (grecs_getline(&buf, &buflen, in) < 0) {
	wy_log(LOG_ERR, "protocol error");
	free(buf);
	return;
    }

    trim_crlf(buf);
    wy_debug(1, ("recv: %s", buf));

    /* FIXME: User name not used any more */
    p = strchr(buf, ' ');
    if (p) {
	*p++ = 0;
	while (*p && (*p == ' ' || *p == '\t'))
	    p++;
    } else {
	wy_log(LOG_ERR, "protocol error");
	free(buf);
	return;
    }

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

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;

    if (ctlfd != -1)
	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);
    }

    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;
	    FILE *in, *out;
	    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

	    in = fdopen(fd, "r");
	    setlinebuf(in);
	    out = fdopen(fd, "w");
	    setlinebuf(out);
	    handle_connection(in, out);
	    fclose(in);
	    fclose(out);
	}
    }
    return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.