1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 33 #include <sys/socket.h> 34 #include <netinet/in.h> 35 #include <sys/un.h> 36 37 #include <errno.h> 38 #include <stdarg.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <sys/stat.h> 42 #include <termios.h> 43 #include <unistd.h> 44 45 #include "log.h" 46 #include "descriptor.h" 47 #include "server.h" 48 #include "prompt.h" 49 #include "ncpaddr.h" 50 #include "probe.h" 51 52 static int 53 server_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 54 { 55 struct server *s = descriptor2server(d); 56 struct prompt *p; 57 int sets; 58 59 sets = 0; 60 if (r && s->fd >= 0) { 61 if (*n < s->fd + 1) 62 *n = s->fd + 1; 63 FD_SET(s->fd, r); 64 log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd); 65 sets++; 66 } 67 68 for (p = log_PromptList(); p; p = p->next) 69 sets += descriptor_UpdateSet(&p->desc, r, w, e, n); 70 71 return sets; 72 } 73 74 static int 75 server_IsSet(struct fdescriptor *d, const fd_set *fdset) 76 { 77 struct server *s = descriptor2server(d); 78 struct prompt *p; 79 80 if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) 81 return 1; 82 83 for (p = log_PromptList(); p; p = p->next) 84 if (descriptor_IsSet(&p->desc, fdset)) 85 return 1; 86 87 return 0; 88 } 89 90 static void 91 server_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 92 { 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; 97 #ifndef NOINET6 98 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss; 99 #endif 100 int ssize = sizeof ss, wfd; 101 struct prompt *p; 102 struct ncpaddr addr; 103 104 if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) { 105 wfd = accept(s->fd, sa, &ssize); 106 if (wfd < 0) 107 log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno)); 108 else if (sa->sa_len == 0) { 109 close(wfd); 110 wfd = -1; 111 } 112 } else 113 wfd = -1; 114 115 if (wfd >= 0) 116 switch (sa->sa_family) { 117 case AF_LOCAL: 118 log_Printf(LogPHASE, "Connected to local client.\n"); 119 break; 120 121 case AF_INET: 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)); 127 close(wfd); 128 wfd = -1; 129 break; 130 } 131 log_Printf(LogPHASE, "Connected to client from %s:%u\n", 132 ncpaddr_ntoa(&addr), ntohs(sin->sin_port)); 133 break; 134 135 #ifndef NOINET6 136 case AF_INET6: 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)); 142 close(wfd); 143 wfd = -1; 144 break; 145 } 146 log_Printf(LogPHASE, "Connected to client from %s:%u\n", 147 ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port)); 148 break; 149 #endif 150 151 default: 152 write(wfd, "Unrecognised access !\n", 22); 153 close(wfd); 154 wfd = -1; 155 break; 156 } 157 158 if (wfd >= 0) { 159 if ((p = prompt_Create(s, bundle, wfd)) == NULL) { 160 write(wfd, "Connection refused.\n", 20); 161 close(wfd); 162 } else { 163 switch (sa->sa_family) { 164 case AF_LOCAL: 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'; 168 break; 169 case AF_INET: 170 p->src.type = "ip"; 171 snprintf(p->src.from, sizeof p->src.from, "%s:%u", 172 ncpaddr_ntoa(&addr), ntohs(sin->sin_port)); 173 break; 174 #ifndef NOINET6 175 case AF_INET6: 176 p->src.type = "ip6"; 177 snprintf(p->src.from, sizeof p->src.from, "%s:%u", 178 ncpaddr_ntoa(&addr), ntohs(sin6->sin6_port)); 179 break; 180 #endif 181 } 182 prompt_TtyCommandMode(p); 183 prompt_Required(p); 184 } 185 } 186 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) 192 break; 193 } 194 } 195 196 static int 197 server_Write(struct fdescriptor *d __unused, struct bundle *bundle __unused, 198 const fd_set *fdset __unused) 199 { 200 /* We never want to write here ! */ 201 log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n"); 202 return 0; 203 } 204 205 struct server server = { 206 { 207 SERVER_DESCRIPTOR, 208 server_UpdateSet, 209 server_IsSet, 210 server_Read, 211 server_Write 212 }, 213 -1, 214 { "", "", 0, 0 } 215 }; 216 217 enum server_stat 218 server_Reopen(struct bundle *bundle) 219 { 220 char name[sizeof server.cfg.sockname]; 221 struct stat st; 222 u_short port; 223 mode_t mask; 224 enum server_stat ret; 225 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); 238 } else 239 ret = SERVER_UNSET; 240 241 return ret; 242 } 243 244 enum server_stat 245 server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask) 246 { 247 struct sockaddr_un ifsun; 248 mode_t oldmask; 249 int s; 250 251 oldmask = (mode_t)-1; /* Silence compiler */ 252 253 if (server.cfg.sockname[0] != '\0' && !strcmp(server.cfg.sockname, name)) 254 server_Close(bundle); 255 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; 261 } 262 ifsun.sun_family = AF_LOCAL; 263 strcpy(ifsun.sun_path, name); 264 265 s = socket(PF_LOCAL, SOCK_STREAM, 0); 266 if (s < 0) { 267 log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno)); 268 goto failed; 269 } 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) 275 umask(oldmask); 276 log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno)); 277 close(s); 278 goto failed; 279 } 280 if (mask != (mode_t)-1) 281 umask(oldmask); 282 if (listen(s, 5) != 0) { 283 log_Printf(LogERROR, "Local: Unable to listen to socket -" 284 " BUNDLE overload?\n"); 285 close(s); 286 unlink(name); 287 goto failed; 288 } 289 server_Close(bundle); 290 server.fd = s; 291 server.cfg.port = 0; 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); 296 297 return SERVER_OK; 298 299 failed: 300 if (server.fd == -1) { 301 server.fd = -1; 302 server.cfg.port = 0; 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; 307 } 308 return SERVER_FAILED; 309 } 310 311 enum server_stat 312 server_TcpOpen(struct bundle *bundle, u_short port) 313 { 314 struct sockaddr_storage ss; 315 struct sockaddr_in *sin = (struct sockaddr_in *)&ss; 316 #ifndef NOINET6 317 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss; 318 #endif 319 int s, sz; 320 321 if (server.cfg.port == port) 322 server_Close(bundle); 323 324 if (port == 0) 325 return SERVER_INVALID; 326 327 memset(&ss, '\0', sizeof ss); 328 #ifndef NOINET6 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; 333 sz = sizeof *sin6; 334 s = socket(PF_INET6, SOCK_STREAM, 0); 335 } else 336 #endif 337 { 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; 342 sz = sizeof *sin; 343 s = socket(PF_INET, SOCK_STREAM, 0); 344 } 345 346 if (s < 0) { 347 log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno)); 348 goto failed; 349 } 350 351 #ifndef NOINET6 352 if (probe.ipv6_available) { 353 int off = 0; 354 setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&off, sizeof(off)); 355 } 356 #endif 357 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)); 361 close(s); 362 goto failed; 363 } 364 if (listen(s, 5) != 0) { 365 log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n", 366 strerror(errno)); 367 close(s); 368 goto failed; 369 } 370 server_Close(bundle); 371 server.fd = s; 372 server.cfg.port = port; 373 *server.cfg.sockname = '\0'; 374 server.cfg.mask = 0; 375 log_Printf(LogPHASE, "Listening at port %d.\n", port); 376 return SERVER_OK; 377 378 failed: 379 if (server.fd == -1) { 380 server.fd = -1; 381 server.cfg.port = port; 382 *server.cfg.sockname = '\0'; 383 server.cfg.mask = 0; 384 } 385 return SERVER_FAILED; 386 } 387 388 int 389 server_Close(struct bundle *bundle __unused) 390 { 391 if (server.fd >= 0) { 392 if (*server.cfg.sockname != '\0') { 393 struct sockaddr_un un; 394 int sz = sizeof un; 395 396 if (getsockname(server.fd, (struct sockaddr *)&un, &sz) == 0 && 397 un.sun_family == AF_LOCAL && sz == sizeof un) 398 unlink(un.sun_path); 399 } 400 close(server.fd); 401 server.fd = -1; 402 /* Drop associated prompts */ 403 log_DestroyPrompts(&server); 404 405 return 1; 406 } 407 408 return 0; 409 } 410 411 int 412 server_Clear(struct bundle *bundle) 413 { 414 int ret; 415 416 ret = server_Close(bundle); 417 418 server.fd = -1; 419 server.cfg.port = 0; 420 *server.cfg.sockname = '\0'; 421 server.cfg.mask = 0; 422 423 return ret; 424 } 425