summaryrefslogtreecommitdiffabout
path: root/src/net.c
blob: 5df000510b3a7f98d627fb4fd7150c729591f1f6 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009-2013, 2017 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;
	struct passwd *pw;

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

	p = strchr(buf, ' ');
	if (p) {
		*p++ = 0;
		while (*p && (*p == ' ' || *p == '\t'))
			p++;
	} else
		p = "";

	pw = getpwnam(buf);
	if (pw)
		schedule_job(spool, pw->pw_uid, NULL);
	else
		wy_log(LOG_ERR, "no such user: %s", buf);
	free(buf);
}

int reconfigure;
static int terminate;

RETSIGTYPE
sig_hup(int sig)
{
	reconfigure = 1;
	terminate = 1;
}

RETSIGTYPE
sig_term(int sig)
{
	terminate = 1;
}

void
wydawca_listener()
{
	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);
	}

	job_init();
	signal(SIGHUP, sig_hup);
	signal(SIGTERM, sig_term);
	signal(SIGQUIT, sig_term);
	signal(SIGINT, sig_term);
	while (!terminate) {
		int rc;
		fd_set rset;
		struct timeval to, *pto;

		job_queue_runner(triplet_sweep());

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

		if (wakeup_interval) {
			to.tv_sec = wakeup_interval;
			to.tv_usec = 0;
			*pto = to;
		} else
			pto = NULL;

		rc = select(maxfd + 1, &rset, NULL, NULL, pto);
		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 to:

Send suggestions and report system problems to the System administrator.