1 /*- 2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 31 #include <sys/socket.h> 32 #include <netinet/in.h> 33 #include <arpa/inet.h> 34 #include <sys/un.h> 35 36 #include <errno.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <sys/stat.h> 40 #include <termios.h> 41 #include <unistd.h> 42 43 #include "log.h" 44 #include "descriptor.h" 45 #include "server.h" 46 #include "id.h" 47 #include "prompt.h" 48 49 static int 50 server_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n) 51 { 52 struct server *s = descriptor2server(d); 53 struct prompt *p; 54 int sets; 55 56 sets = 0; 57 if (r && s->fd >= 0) { 58 if (*n < s->fd + 1) 59 *n = s->fd + 1; 60 FD_SET(s->fd, r); 61 log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd); 62 sets++; 63 } 64 65 for (p = log_PromptList(); p; p = p->next) 66 sets += descriptor_UpdateSet(&p->desc, r, w, e, n); 67 68 return sets; 69 } 70 71 static int 72 server_IsSet(struct fdescriptor *d, const fd_set *fdset) 73 { 74 struct server *s = descriptor2server(d); 75 struct prompt *p; 76 77 if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) 78 return 1; 79 80 for (p = log_PromptList(); p; p = p->next) 81 if (descriptor_IsSet(&p->desc, fdset)) 82 return 1; 83 84 return 0; 85 } 86 87 #define IN_SIZE sizeof(struct sockaddr_in) 88 #define UN_SIZE sizeof(struct sockaddr_un) 89 #define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE) 90 91 static void 92 server_Read(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 93 { 94 struct server *s = descriptor2server(d); 95 char hisaddr[ADDRSZ]; 96 struct sockaddr *sa = (struct sockaddr *)hisaddr; 97 struct sockaddr_in *in = (struct sockaddr_in *)hisaddr; 98 int ssize = ADDRSZ, wfd; 99 struct prompt *p; 100 101 if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) { 102 wfd = accept(s->fd, sa, &ssize); 103 if (wfd < 0) 104 log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno)); 105 else if (sa->sa_len == 0) { 106 close(wfd); 107 wfd = -1; 108 } 109 } else 110 wfd = -1; 111 112 if (wfd >= 0) 113 switch (sa->sa_family) { 114 case AF_LOCAL: 115 log_Printf(LogPHASE, "Connected to local client.\n"); 116 break; 117 118 case AF_INET: 119 if (ntohs(in->sin_port) < 1024) { 120 log_Printf(LogALERT, "Rejected client connection from %s:%u" 121 "(invalid port number) !\n", 122 inet_ntoa(in->sin_addr), ntohs(in->sin_port)); 123 close(wfd); 124 wfd = -1; 125 break; 126 } 127 log_Printf(LogPHASE, "Connected to client from %s:%u\n", 128 inet_ntoa(in->sin_addr), in->sin_port); 129 break; 130 131 default: 132 write(wfd, "Unrecognised access !\n", 22); 133 close(wfd); 134 wfd = -1; 135 break; 136 } 137 138 if (wfd >= 0) { 139 if ((p = prompt_Create(s, bundle, wfd)) == NULL) { 140 write(wfd, "Connection refused.\n", 20); 141 close(wfd); 142 } else { 143 switch (sa->sa_family) { 144 case AF_LOCAL: 145 p->src.type = "local"; 146 strncpy(p->src.from, s->cfg.sockname, sizeof p->src.from - 1); 147 p->src.from[sizeof p->src.from - 1] = '\0'; 148 break; 149 case AF_INET: 150 p->src.type = "tcp"; 151 snprintf(p->src.from, sizeof p->src.from, "%s:%u", 152 inet_ntoa(in->sin_addr), in->sin_port); 153 break; 154 } 155 prompt_TtyCommandMode(p); 156 prompt_Required(p); 157 } 158 } 159 160 log_PromptListChanged = 0; 161 for (p = log_PromptList(); p; p = p->next) 162 if (descriptor_IsSet(&p->desc, fdset)) { 163 descriptor_Read(&p->desc, bundle, fdset); 164 if (log_PromptListChanged) 165 break; 166 } 167 } 168 169 static int 170 server_Write(struct fdescriptor *d, struct bundle *bundle, const fd_set *fdset) 171 { 172 /* We never want to write here ! */ 173 log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n"); 174 return 0; 175 } 176 177 struct server server = { 178 { 179 SERVER_DESCRIPTOR, 180 server_UpdateSet, 181 server_IsSet, 182 server_Read, 183 server_Write 184 }, 185 -1 186 }; 187 188 enum server_stat 189 server_Reopen(struct bundle *bundle) 190 { 191 char name[sizeof server.cfg.sockname]; 192 struct stat st; 193 u_short port; 194 mode_t mask; 195 enum server_stat ret; 196 197 if (server.cfg.sockname[0] != '\0') { 198 strcpy(name, server.cfg.sockname); 199 mask = server.cfg.mask; 200 server_Close(bundle); 201 if (server.cfg.sockname[0] != '\0' && stat(server.cfg.sockname, &st) == 0) 202 if (!(st.st_mode & S_IFSOCK) || unlink(server.cfg.sockname) != 0) 203 return SERVER_FAILED; 204 ret = server_LocalOpen(bundle, name, mask); 205 } else if (server.cfg.port != 0) { 206 port = server.cfg.port; 207 server_Close(bundle); 208 ret = server_TcpOpen(bundle, port); 209 } else 210 ret = SERVER_UNSET; 211 212 return ret; 213 } 214 215 enum server_stat 216 server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask) 217 { 218 struct sockaddr_un ifsun; 219 mode_t oldmask; 220 int s; 221 222 oldmask = (mode_t)-1; /* Silence compiler */ 223 224 if (server.cfg.sockname && !strcmp(server.cfg.sockname, name)) 225 server_Close(bundle); 226 227 memset(&ifsun, '\0', sizeof ifsun); 228 ifsun.sun_len = strlen(name); 229 if (ifsun.sun_len > sizeof ifsun.sun_path - 1) { 230 log_Printf(LogERROR, "Local: %s: Path too long\n", name); 231 return SERVER_INVALID; 232 } 233 ifsun.sun_family = AF_LOCAL; 234 strcpy(ifsun.sun_path, name); 235 236 s = socket(PF_LOCAL, SOCK_STREAM, 0); 237 if (s < 0) { 238 log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno)); 239 goto failed; 240 } 241 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s); 242 if (mask != (mode_t)-1) 243 oldmask = umask(mask); 244 if (bind(s, (struct sockaddr *)&ifsun, sizeof ifsun) < 0) { 245 if (mask != (mode_t)-1) 246 umask(oldmask); 247 log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno)); 248 close(s); 249 goto failed; 250 } 251 if (mask != (mode_t)-1) 252 umask(oldmask); 253 if (listen(s, 5) != 0) { 254 log_Printf(LogERROR, "Local: Unable to listen to socket -" 255 " BUNDLE overload?\n"); 256 close(s); 257 unlink(name); 258 goto failed; 259 } 260 server_Close(bundle); 261 server.fd = s; 262 server.cfg.port = 0; 263 strncpy(server.cfg.sockname, ifsun.sun_path, sizeof server.cfg.sockname - 1); 264 server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0'; 265 server.cfg.mask = mask; 266 log_Printf(LogPHASE, "Listening at local socket %s.\n", name); 267 268 return SERVER_OK; 269 270 failed: 271 if (server.fd == -1) { 272 server.fd = -1; 273 server.cfg.port = 0; 274 strncpy(server.cfg.sockname, ifsun.sun_path, 275 sizeof server.cfg.sockname - 1); 276 server.cfg.sockname[sizeof server.cfg.sockname - 1] = '\0'; 277 server.cfg.mask = mask; 278 } 279 return SERVER_FAILED; 280 } 281 282 enum server_stat 283 server_TcpOpen(struct bundle *bundle, u_short port) 284 { 285 struct sockaddr_in ifsin; 286 int s; 287 288 if (server.cfg.port == port) 289 server_Close(bundle); 290 291 if (port == 0) 292 return SERVER_INVALID; 293 294 s = socket(PF_INET, SOCK_STREAM, 0); 295 if (s < 0) { 296 log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno)); 297 goto failed; 298 } 299 memset(&ifsin, '\0', sizeof ifsin); 300 ifsin.sin_family = AF_INET; 301 ifsin.sin_addr.s_addr = INADDR_ANY; 302 ifsin.sin_port = htons(port); 303 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s); 304 if (bind(s, (struct sockaddr *)&ifsin, sizeof ifsin) < 0) { 305 log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno)); 306 close(s); 307 goto failed; 308 } 309 if (listen(s, 5) != 0) { 310 log_Printf(LogERROR, "Tcp: Unable to listen to socket: %s\n", 311 strerror(errno)); 312 close(s); 313 goto failed; 314 } 315 server_Close(bundle); 316 server.fd = s; 317 server.cfg.port = port; 318 *server.cfg.sockname = '\0'; 319 server.cfg.mask = 0; 320 log_Printf(LogPHASE, "Listening at port %d.\n", port); 321 return SERVER_OK; 322 323 failed: 324 if (server.fd == -1) { 325 server.fd = -1; 326 server.cfg.port = port; 327 *server.cfg.sockname = '\0'; 328 server.cfg.mask = 0; 329 } 330 return SERVER_FAILED; 331 } 332 333 int 334 server_Close(struct bundle *bundle) 335 { 336 if (server.fd >= 0) { 337 if (*server.cfg.sockname != '\0') { 338 struct sockaddr_un un; 339 int sz = sizeof un; 340 341 if (getsockname(server.fd, (struct sockaddr *)&un, &sz) == 0 && 342 un.sun_family == AF_LOCAL && sz == sizeof un) 343 unlink(un.sun_path); 344 } 345 close(server.fd); 346 server.fd = -1; 347 /* Drop associated prompts */ 348 log_DestroyPrompts(&server); 349 350 return 1; 351 } 352 353 return 0; 354 } 355 356 int 357 server_Clear(struct bundle *bundle) 358 { 359 int ret; 360 361 ret = server_Close(bundle); 362 363 server.fd = -1; 364 server.cfg.port = 0; 365 *server.cfg.sockname = '\0'; 366 server.cfg.mask = 0; 367 368 return ret; 369 } 370