1997-12-21 12:11:13 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1998-08-02 13:01:16 +00:00
|
|
|
* $Id: server.c,v 1.22 1998/06/27 14:18:10 brian Exp $
|
1997-10-26 01:04:02 +00:00
|
|
|
*/
|
|
|
|
|
1998-04-07 00:54:26 +00:00
|
|
|
#include <sys/types.h>
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
1998-04-28 01:25:46 +00:00
|
|
|
#include <sys/un.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <errno.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
1998-02-10 03:23:50 +00:00
|
|
|
#include <termios.h>
|
1997-06-25 19:30:05 +00:00
|
|
|
#include <unistd.h>
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "log.h"
|
1998-02-09 19:24:03 +00:00
|
|
|
#include "descriptor.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
#include "server.h"
|
1997-11-09 06:22:49 +00:00
|
|
|
#include "id.h"
|
1998-02-10 03:23:50 +00:00
|
|
|
#include "prompt.h"
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
static int
|
|
|
|
server_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
|
|
|
|
{
|
|
|
|
struct server *s = descriptor2server(d);
|
1998-05-23 22:24:50 +00:00
|
|
|
struct prompt *p;
|
|
|
|
int sets;
|
1997-10-26 01:04:02 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
sets = 0;
|
1998-02-13 05:10:26 +00:00
|
|
|
if (r && s->fd >= 0) {
|
1998-02-09 19:24:03 +00:00
|
|
|
if (*n < s->fd + 1)
|
|
|
|
*n = s->fd + 1;
|
1998-02-10 03:23:50 +00:00
|
|
|
FD_SET(s->fd, r);
|
1998-05-10 22:20:20 +00:00
|
|
|
log_Printf(LogTIMER, "server: fdset(r) %d\n", s->fd);
|
1998-05-23 22:24:50 +00:00
|
|
|
sets++;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
|
|
|
sets += descriptor_UpdateSet(&p->desc, r, w, e, n);
|
|
|
|
|
|
|
|
return sets;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
static int
|
1998-03-20 19:48:28 +00:00
|
|
|
server_IsSet(struct descriptor *d, const fd_set *fdset)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-02-09 19:24:03 +00:00
|
|
|
struct server *s = descriptor2server(d);
|
1998-05-23 22:24:50 +00:00
|
|
|
struct prompt *p;
|
|
|
|
|
|
|
|
if (s->fd >= 0 && FD_ISSET(s->fd, fdset))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
|
|
|
if (descriptor_IsSet(&p->desc, fdset))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
#define IN_SIZE sizeof(struct sockaddr_in)
|
|
|
|
#define UN_SIZE sizeof(struct sockaddr_in)
|
|
|
|
#define ADDRSZ (IN_SIZE > UN_SIZE ? IN_SIZE : UN_SIZE)
|
|
|
|
|
|
|
|
static void
|
1998-02-10 03:22:05 +00:00
|
|
|
server_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-09 19:24:03 +00:00
|
|
|
{
|
|
|
|
struct server *s = descriptor2server(d);
|
|
|
|
char hisaddr[ADDRSZ];
|
|
|
|
struct sockaddr *sa = (struct sockaddr *)hisaddr;
|
1998-04-18 01:01:28 +00:00
|
|
|
struct sockaddr_in *in = (struct sockaddr_in *)hisaddr;
|
1998-02-09 19:24:03 +00:00
|
|
|
int ssize = ADDRSZ, wfd;
|
1998-04-03 19:26:02 +00:00
|
|
|
struct prompt *p;
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if (s->fd >= 0 && FD_ISSET(s->fd, fdset)) {
|
|
|
|
wfd = accept(s->fd, sa, &ssize);
|
|
|
|
if (wfd < 0)
|
|
|
|
log_Printf(LogERROR, "server_Read: accept(): %s\n", strerror(errno));
|
|
|
|
} else
|
|
|
|
wfd = -1;
|
1998-02-09 19:24:03 +00:00
|
|
|
|
1998-05-23 22:24:50 +00:00
|
|
|
if (wfd >= 0)
|
1998-04-03 19:26:02 +00:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_LOCAL:
|
1998-05-23 22:24:50 +00:00
|
|
|
log_Printf(LogPHASE, "Connected to local client.\n");
|
1998-04-03 19:26:02 +00:00
|
|
|
break;
|
1998-05-23 22:24:50 +00:00
|
|
|
|
1998-04-03 19:26:02 +00:00
|
|
|
case AF_INET:
|
1998-05-23 22:24:50 +00:00
|
|
|
if (ntohs(in->sin_port) < 1024) {
|
|
|
|
log_Printf(LogALERT, "Rejected client connection from %s:%u"
|
|
|
|
"(invalid port number) !\n",
|
|
|
|
inet_ntoa(in->sin_addr), ntohs(in->sin_port));
|
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
log_Printf(LogPHASE, "Connected to client from %s:%u\n",
|
|
|
|
inet_ntoa(in->sin_addr), in->sin_port);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
write(wfd, "Unrecognised access !\n", 22);
|
|
|
|
close(wfd);
|
|
|
|
wfd = -1;
|
1998-04-03 19:26:02 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
|
|
|
if (wfd >= 0) {
|
|
|
|
if ((p = prompt_Create(s, bundle, wfd)) == NULL) {
|
|
|
|
write(wfd, "Connection refused.\n", 20);
|
|
|
|
close(wfd);
|
|
|
|
} else {
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_LOCAL:
|
|
|
|
p->src.type = "local";
|
|
|
|
strncpy(p->src.from, s->rm, sizeof p->src.from - 1);
|
|
|
|
p->src.from[sizeof p->src.from - 1] = '\0';
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
p->src.type = "tcp";
|
|
|
|
snprintf(p->src.from, sizeof p->src.from, "%s:%u",
|
|
|
|
inet_ntoa(in->sin_addr), in->sin_port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prompt_TtyCommandMode(p);
|
|
|
|
prompt_Required(p);
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
1998-05-23 22:24:50 +00:00
|
|
|
|
1998-08-02 13:01:16 +00:00
|
|
|
log_PromptListChanged = 0;
|
1998-05-23 22:24:50 +00:00
|
|
|
for (p = log_PromptList(); p; p = p->next)
|
1998-08-02 13:01:16 +00:00
|
|
|
if (descriptor_IsSet(&p->desc, fdset)) {
|
1998-05-23 22:24:50 +00:00
|
|
|
descriptor_Read(&p->desc, bundle, fdset);
|
1998-08-02 13:01:16 +00:00
|
|
|
if (log_PromptListChanged)
|
|
|
|
break;
|
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
|
|
|
|
1998-06-24 19:33:36 +00:00
|
|
|
static int
|
1998-02-23 00:38:44 +00:00
|
|
|
server_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
|
1998-02-09 19:24:03 +00:00
|
|
|
{
|
|
|
|
/* We never want to write here ! */
|
1998-06-16 19:40:42 +00:00
|
|
|
log_Printf(LogALERT, "server_Write: Internal error: Bad call !\n");
|
1998-06-24 19:33:36 +00:00
|
|
|
return 0;
|
1998-02-09 19:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct server server = {
|
|
|
|
{
|
|
|
|
SERVER_DESCRIPTOR,
|
|
|
|
server_UpdateSet,
|
|
|
|
server_IsSet,
|
|
|
|
server_Read,
|
|
|
|
server_Write
|
|
|
|
},
|
|
|
|
-1
|
|
|
|
};
|
1998-01-29 00:44:16 +00:00
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
server_LocalOpen(struct bundle *bundle, const char *name, mode_t mask)
|
1998-01-29 00:44:16 +00:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (server.rm && !strcmp(server.rm, name)) {
|
|
|
|
if (chmod(server.rm, mask))
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: chmod: %s\n", strerror(errno));
|
1998-04-10 13:19:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&server.ifsun, '\0', sizeof server.ifsun);
|
|
|
|
server.ifsun.sun_len = strlen(name);
|
|
|
|
if (server.ifsun.sun_len > sizeof server.ifsun.sun_path - 1) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: %s: Path too long\n", name);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 2;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1998-04-10 13:19:23 +00:00
|
|
|
server.ifsun.sun_family = AF_LOCAL;
|
|
|
|
strcpy(server.ifsun.sun_path, name);
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
s = ID0socket(PF_LOCAL, SOCK_STREAM, 0);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: socket: %s\n", strerror(errno));
|
1997-09-04 00:38:22 +00:00
|
|
|
return 3;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
mask = umask(mask);
|
1998-04-10 13:19:23 +00:00
|
|
|
if (bind(s, (struct sockaddr *)&server.ifsun, sizeof server.ifsun) < 0) {
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
umask(mask);
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Local: bind: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 4;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-11-09 22:07:29 +00:00
|
|
|
if (mask != (mode_t)-1)
|
|
|
|
umask(mask);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (listen(s, 5) != 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Local: Unable to listen to socket - BUNDLE overload?\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
1997-11-09 06:22:49 +00:00
|
|
|
ID0unlink(name);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 5;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
server_Close(bundle);
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = s;
|
1998-04-10 13:19:23 +00:00
|
|
|
server.rm = server.ifsun.sun_path;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Listening at local socket %s.\n", name);
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
server_TcpOpen(struct bundle *bundle, int port)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1997-08-25 00:29:32 +00:00
|
|
|
struct sockaddr_in ifsin;
|
|
|
|
int s;
|
1997-06-25 19:30:05 +00:00
|
|
|
|
1998-04-10 13:19:23 +00:00
|
|
|
if (server.port == port)
|
|
|
|
return 0;
|
1997-09-09 21:51:39 +00:00
|
|
|
|
1997-11-09 06:22:49 +00:00
|
|
|
s = ID0socket(PF_INET, SOCK_STREAM, 0);
|
1997-08-25 00:29:32 +00:00
|
|
|
if (s < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Tcp: socket: %s\n", strerror(errno));
|
1997-09-04 00:38:22 +00:00
|
|
|
return 7;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1997-12-21 02:27:32 +00:00
|
|
|
memset(&ifsin, '\0', sizeof ifsin);
|
1997-08-25 00:29:32 +00:00
|
|
|
ifsin.sin_family = AF_INET;
|
|
|
|
ifsin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
ifsin.sin_port = htons(port);
|
|
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &s, sizeof s);
|
1997-12-24 09:29:17 +00:00
|
|
|
if (bind(s, (struct sockaddr *)&ifsin, sizeof ifsin) < 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogWARN, "Tcp: bind: %s\n", strerror(errno));
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 8;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
|
|
|
if (listen(s, 5) != 0) {
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogERROR, "Tcp: Unable to listen to socket - BUNDLE overload?\n");
|
1997-08-25 00:29:32 +00:00
|
|
|
close(s);
|
1997-09-04 00:38:22 +00:00
|
|
|
return 9;
|
1997-08-25 00:29:32 +00:00
|
|
|
}
|
1998-05-01 19:26:12 +00:00
|
|
|
server_Close(bundle);
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = s;
|
1998-04-10 13:19:23 +00:00
|
|
|
server.port = port;
|
1998-05-01 19:26:12 +00:00
|
|
|
log_Printf(LogPHASE, "Listening at port %d.\n", port);
|
1997-08-25 00:29:32 +00:00
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
|
|
|
|
1998-02-09 19:24:03 +00:00
|
|
|
int
|
1998-05-01 19:26:12 +00:00
|
|
|
server_Close(struct bundle *bundle)
|
1997-06-25 19:30:05 +00:00
|
|
|
{
|
1998-02-09 19:24:03 +00:00
|
|
|
if (server.fd >= 0) {
|
|
|
|
close(server.fd);
|
1998-04-10 13:19:23 +00:00
|
|
|
if (server.rm) {
|
|
|
|
ID0unlink(server.rm);
|
|
|
|
server.rm = NULL;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
server.fd = -1;
|
1998-04-10 13:19:23 +00:00
|
|
|
server.port = 0;
|
1998-04-03 19:26:02 +00:00
|
|
|
/* Drop associated prompts */
|
1998-05-23 22:24:50 +00:00
|
|
|
log_DestroyPrompts(&server);
|
1998-02-09 19:24:03 +00:00
|
|
|
return 1;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|
1998-02-09 19:24:03 +00:00
|
|
|
return 0;
|
1997-06-25 19:30:05 +00:00
|
|
|
}
|