2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
46 #include "descriptor.h"
53 server_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
55 struct server *s = descriptor2server(d);
60 if (r && s->fd >= 0) {
64 log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd);
68 for (p = log_PromptList(); p; p = p->next)
69 sets += descriptor_UpdateSet(&p->desc, r, w, e, n);
75 server_IsSet(struct fdescriptor *d, const fd_set *fdset)
77 struct server *s = descriptor2server(d);
80 if (s->fd >= 0 && FD_ISSET(s->fd, fdset))
83 for (p = log_PromptList(); p; p = p->next)
84 if (descriptor_IsSet(&p->desc, fdset))
91 server_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset)
93 struct server *s = descriptor2server(d);
94 struct sockaddr_storage ss;
95 struct sockaddr *sa = (struct sockaddr *)&ss;
96 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
98 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
100 int ssize = sizeof ss, wfd;
104 if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) {
105 wfd = accept(s->fd, sa, &ssize);
107 log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno));
108 else if (sa->sa_len == 0) {
116 switch (sa->sa_family) {
118 log_Printf(LogPHASE, "Connected to local client.\n");
122 ncpaddr_setsa(&addr, sa);
123 if (ntohs(sin->sin_port) < 1024) {
124 log_Printf(LogALERT, "Rejected client connection from %s:%u"
125 "(invalid port number) !\n",
126 ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
131 log_Printf(LogPHASE, "Connected to client from %s:%u\n",
132 ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
137 ncpaddr_setsa(&addr, sa);
138 if (ntohs(sin6->sin6_port) < 1024) {
139 log_Printf(LogALERT, "Rejected client connection from %s:%u"
140 "(invalid port number) !\n",
141 ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
146 log_Printf(LogPHASE, "Connected to client from %s:%u\n",
147 ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
152 write(wfd, "Unrecognised access !\n", 22);
159 if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
160 write(wfd, "Connection refused.\n", 20);
163 switch (sa->sa_family) {
165 p->src.type = "local";
166 strncpy(p->src.from, s->cfg.sockname, sizeof p->src.from - 1);
167 p->src.from[sizeof p->src.from - 1] = '\0';
171 snprintf(p->src.from, sizeof p->src.from, "%s:%u",
172 ncpaddr_ntoa(&addr), ntohs(sin->sin_port));
177 snprintf(p->src.from, sizeof p->src.from, "%s:%u",
178 ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port));
182 prompt_TtyCommandMode(p);
187 log_PromptListChanged = 0;
188 for (p = log_PromptList(); p; p = p->next)
189 if (descriptor_IsSet(&p->desc, fdset)) {
190 descriptor_Read(&p->desc, bundle, fdset);
191 if (log_PromptListChanged)
197 server_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused,
198 const fd_set *fdset __unused)
200 /* We never want to write here ! */
201 log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n");
205 struct server server = {
218 server_Reopen(struct bundle *bundle)
220 char name[sizeof server.cfg.sockname];
224 enum server_stat ret;
226 if (server.cfg.sockname[0] != '\0') {
227 strcpy(name, server.cfg.sockname);
228 mask = server.cfg.mask;
229 server_Close(bundle);
230 if (server.cfg.sockname[0] != '\0' && stat(server.cfg.sockname, &st) == 0)
231 if (!(st.st_mode & S_IFSOCK) || unlink(server.cfg.sockname) != 0)
232 return SERVER_FAILED;
233 ret = server_LocalOpen(bundle, name, mask);
234 } else if (server.cfg.port != 0) {
235 port = server.cfg.port;
236 server_Close(bundle);
237 ret = server_TcpOpen(bundle, port);
245 server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask)
247 struct sockaddr_un ifsun;
251 oldmask = (mode_t)-1; /* Silence compiler */
253 if (server.cfg.sockname[0] != '\0' && !strcmp(server.cfg.sockname, name))
254 server_Close(bundle);
256 memset(&ifsun, '\0', sizeof ifsun);
257 ifsun.sun_len = strlen(name);
258 if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
259 log_Printf(LogERROR, "Local: %s: Path too long\n", name);
260 return SERVER_INVALID;
262 ifsun.sun_family = AF_LOCAL;
263 strcpy(ifsun.sun_path, name);
265 s = socket(PF_LOCAL, SOCK_STREAM, 0);
267 log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno));
270 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
271 if (mask != (mode_t)-1)
272 oldmask = umask(mask);
273 if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) {
274 if (mask != (mode_t)-1)
276 log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno));
280 if (mask != (mode_t)-1)
282 if (listen(s, 5) != 0) {
283 log_Printf(LogERROR, "Local: Unable to listen to socket -"
284 " BUNDLE overload?\n");
289 server_Close(bundle);
292 strncpy(server.cfg.sockname, ifsun.sun_path, sizeof server.cfg.sockname - 1);
293 server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0';
294 server.cfg.mask = mask;
295 log_Printf(LogPHASE, "Listening at local socket %s.\n", name);
300 if (server.fd == -1) {
303 strncpy(server.cfg.sockname, ifsun.sun_path,
304 sizeof server.cfg.sockname - 1);
305 server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0';
306 server.cfg.mask = mask;
308 return SERVER_FAILED;
312 server_TcpOpen(struct bundle *bundle, u_short port)
314 struct sockaddr_storage ss;
315 struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
317 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
321 if (server.cfg.port == port)
322 server_Close(bundle);
325 return SERVER_INVALID;
327 memset(&ss, '\0', sizeof ss);
329 if (probe.ipv6_available) {
330 sin6->sin6_family = AF_INET6;
331 sin6->sin6_port = htons(port);
332 sin6->sin6_len = (u_int8_t)sizeof ss;
334 s = socket(PF_INET6, SOCK_STREAM, 0);
338 sin->sin_family = AF_INET;
339 sin->sin_port = htons(port);
340 sin->sin_len = (u_int8_t)sizeof ss;
341 sin->sin_addr.s_addr = INADDR_ANY;
343 s = socket(PF_INET, SOCK_STREAM, 0);
347 log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
352 if (probe.ipv6_available) {
354 setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off));
358 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
359 if (bind(s, (struct sockaddr *)&ss, sz) < 0) {
360 log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno));
364 if (listen(s, 5) != 0) {
365 log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n",
370 server_Close(bundle);
372 server.cfg.port = port;
373 *server.cfg.sockname = '\0';
375 log_Printf(LogPHASE, "Listening at port %d.\n", port);
379 if (server.fd == -1) {
381 server.cfg.port = port;
382 *server.cfg.sockname = '\0';
385 return SERVER_FAILED;
389 server_Close(struct bundle *bundle __unused)
391 if (server.fd >= 0) {
392 if (*server.cfg.sockname != '\0') {
393 struct sockaddr_un un;
396 if (getsockname(server.fd, (struct sockaddr *)&un, &sz) == 0 &&
397 un.sun_family == AF_LOCAL && sz == sizeof un)
402 /* Drop associated prompts */
403 log_DestroyPrompts(&server);
412 server_Clear(struct bundle *bundle)
416 ret = server_Close(bundle);
420 *server.cfg.sockname = '\0';