2016-03-22 01:48:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2016-03-27 11:53:46 +00:00
|
|
|
#include <sys/wait.h>
|
2016-08-04 09:41:43 +00:00
|
|
|
#include <sys/stat.h>
|
2018-02-18 09:52:55 +00:00
|
|
|
#include <pwd.h>
|
2016-03-27 11:53:46 +00:00
|
|
|
#include <signal.h>
|
2016-03-22 01:48:59 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2016-03-27 11:53:46 +00:00
|
|
|
#include <errno.h>
|
2016-04-10 04:10:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2018-10-18 01:36:02 +00:00
|
|
|
#ifndef DISABLE_SSH
|
2016-08-06 11:57:12 +00:00
|
|
|
#include <libssh/libssh.h>
|
2016-08-07 00:06:21 +00:00
|
|
|
#include <libssh/server.h>
|
|
|
|
#include <libssh/callbacks.h>
|
2018-10-18 01:36:02 +00:00
|
|
|
#endif
|
2016-04-13 07:42:22 +00:00
|
|
|
#include <string.h>
|
2016-08-07 00:06:21 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#if defined(linux)
|
2018-10-08 14:13:11 +00:00
|
|
|
#include <pty.h>
|
|
|
|
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
|
|
|
|
#include <util.h>
|
2017-10-19 21:27:58 +00:00
|
|
|
#elif defined(__FreeBSD__)
|
2018-10-08 14:13:11 +00:00
|
|
|
#include <libutil.h>
|
2017-10-19 21:27:58 +00:00
|
|
|
#elif defined(__sun)
|
2018-10-08 14:13:11 +00:00
|
|
|
#include "os/sunos.h"
|
2016-08-07 00:06:21 +00:00
|
|
|
#endif
|
2016-08-16 10:28:48 +00:00
|
|
|
#if defined(ENABLE_WWW)
|
2018-10-08 14:13:11 +00:00
|
|
|
#include <microhttpd.h>
|
2016-08-16 10:28:48 +00:00
|
|
|
#endif
|
2016-08-07 03:12:54 +00:00
|
|
|
#include <termios.h>
|
2016-03-22 01:48:59 +00:00
|
|
|
#include "bbs.h"
|
2016-08-02 01:58:10 +00:00
|
|
|
#include "inih/ini.h"
|
2016-12-11 04:42:28 +00:00
|
|
|
#include "hashmap/hashmap.h"
|
|
|
|
|
|
|
|
map_t ip_guard_map;
|
2016-08-02 01:58:10 +00:00
|
|
|
|
|
|
|
extern struct bbs_config conf;
|
2016-08-06 11:57:12 +00:00
|
|
|
extern struct user_record *gUser;
|
2016-08-06 23:54:37 +00:00
|
|
|
|
|
|
|
int ssh_pid = -1;
|
2016-08-07 06:15:41 +00:00
|
|
|
int bbs_pid = 0;
|
2016-08-12 06:15:10 +00:00
|
|
|
int server_socket = -1;
|
2018-02-18 09:52:55 +00:00
|
|
|
int ipv6_pid = -1;
|
2016-08-06 23:54:37 +00:00
|
|
|
|
2016-12-11 12:14:36 +00:00
|
|
|
int bbs_stdin;
|
|
|
|
int bbs_stdout;
|
|
|
|
int bbs_stderr;
|
|
|
|
|
2016-08-16 10:28:48 +00:00
|
|
|
#if defined(ENABLE_WWW)
|
|
|
|
struct MHD_Daemon *www_daemon;
|
|
|
|
#endif
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
void sigterm_handler(int s) {
|
2016-08-06 23:54:37 +00:00
|
|
|
if (ssh_pid != -1) {
|
|
|
|
kill(ssh_pid, SIGTERM);
|
|
|
|
}
|
2016-08-12 06:15:10 +00:00
|
|
|
if (server_socket != -1) {
|
|
|
|
close(server_socket);
|
|
|
|
}
|
2016-08-16 10:28:48 +00:00
|
|
|
#if defined(ENABLE_WWW)
|
|
|
|
if (www_daemon != NULL) {
|
|
|
|
MHD_stop_daemon(www_daemon);
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-18 09:52:55 +00:00
|
|
|
if (ipv6_pid != -1) {
|
|
|
|
kill(ipv6_pid, SIGTERM);
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-03-22 01:48:59 +00:00
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
void sigchld_handler(int s) {
|
|
|
|
// waitpid() might overwrite errno, so we save and restore it:
|
|
|
|
int saved_errno = errno;
|
2016-03-27 11:53:46 +00:00
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
while (waitpid(-1, NULL, WNOHANG) > 0)
|
|
|
|
;
|
2016-03-27 11:53:46 +00:00
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
errno = saved_errno;
|
2016-03-27 11:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int protocol_config_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-12-10 07:55:47 +00:00
|
|
|
struct bbs_config *conf = (struct bbs_config *)user;
|
|
|
|
|
2018-10-16 15:05:15 +00:00
|
|
|
struct protocol *proto = NULL;
|
|
|
|
for (size_t i = 0; i < ptr_vector_len(&conf->protocols); ++i) {
|
|
|
|
struct protocol *aproto = ptr_vector_get(&conf->protocols, i);
|
|
|
|
assert(aproto != NULL);
|
|
|
|
if (strcasecmp(aproto->name, section) == 0) {
|
|
|
|
proto = aproto;
|
|
|
|
break;
|
2016-12-10 07:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
if (proto == NULL) {
|
|
|
|
proto = (struct protocol *)malloz(sizeof(struct protocol));
|
|
|
|
ptr_vector_append(&conf->protocols, proto);
|
|
|
|
proto->name = strdup(section);
|
|
|
|
proto->internal_zmodem = 0;
|
|
|
|
proto->upload_prompt = 0;
|
|
|
|
proto->stdio = 0;
|
2016-12-10 07:55:47 +00:00
|
|
|
}
|
|
|
|
if (strcasecmp(name, "upload command") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(proto->upload);
|
|
|
|
proto->upload = strdup(value);
|
2016-12-10 07:55:47 +00:00
|
|
|
} else if (strcasecmp(name, "download command") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(proto->download);
|
|
|
|
proto->download = strdup(value);
|
2016-12-10 07:55:47 +00:00
|
|
|
} else if (strcasecmp(name, "internal zmodem") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
proto->internal_zmodem = (strcasecmp(value, "true") == 0);
|
2016-12-10 07:55:47 +00:00
|
|
|
} else if (strcasecmp(name, "stdio") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
proto->stdio = (strcasecmp(value, "true") == 0);
|
2016-12-10 07:55:47 +00:00
|
|
|
} else if (strcasecmp(name, "upload prompt") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
proto->upload_prompt = (strcasecmp(value, "true") == 0);
|
2016-12-10 07:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int archiver_config_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-12-08 04:08:04 +00:00
|
|
|
struct bbs_config *conf = (struct bbs_config *)user;
|
|
|
|
|
2018-10-16 15:05:15 +00:00
|
|
|
struct archiver *arc = NULL;
|
|
|
|
for (size_t i = 0; i < ptr_vector_len(&conf->archivers); ++i) {
|
|
|
|
struct archiver *anarc = ptr_vector_get(&conf->archivers, i);
|
|
|
|
if (strcasecmp(anarc->name, section) == 0) {
|
|
|
|
arc = anarc; // hy in the UK.
|
|
|
|
break;
|
2016-12-08 04:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
if (arc == NULL) {
|
|
|
|
arc = malloz(sizeof(struct archiver));
|
|
|
|
ptr_vector_append(&conf->archivers, arc);
|
2016-12-08 04:08:04 +00:00
|
|
|
|
2018-10-16 15:05:15 +00:00
|
|
|
arc->name = strdup(section);
|
2016-12-08 04:08:04 +00:00
|
|
|
}
|
|
|
|
if (strcasecmp(name, "extension") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(arc->extension);
|
|
|
|
arc->extension = strdup(value);
|
2016-12-08 04:08:04 +00:00
|
|
|
} else if (strcasecmp(name, "unpack") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(arc->unpack);
|
|
|
|
arc->unpack = strdup(value);
|
2016-12-08 04:08:04 +00:00
|
|
|
} else if (strcasecmp(name, "pack") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(arc->pack);
|
|
|
|
arc->pack = strdup(value);
|
2016-12-08 04:08:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2016-03-27 11:53:46 +00:00
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int door_config_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-08-02 01:58:10 +00:00
|
|
|
struct bbs_config *conf = (struct bbs_config *)user;
|
|
|
|
|
2018-10-16 15:05:15 +00:00
|
|
|
struct door_config *door = NULL;
|
|
|
|
for (size_t i = 0; i < ptr_vector_len(&conf->doors); ++i) {
|
|
|
|
struct door_config *adoor = ptr_vector_get(&conf->doors, i);
|
|
|
|
assert(adoor != NULL);
|
|
|
|
if (strcasecmp(adoor->name, section) == 0) {
|
|
|
|
door = adoor;
|
|
|
|
break;
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
if (door == NULL) {
|
|
|
|
door = malloz(sizeof(struct door_config));
|
|
|
|
ptr_vector_append(&conf->doors, door);
|
|
|
|
door->name = strdup(section);
|
|
|
|
door->codepage = NULL;
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
2017-04-12 12:37:21 +00:00
|
|
|
if (strcasecmp(name, "command") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(door->command);
|
|
|
|
door->command = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "stdio") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
door->stdio = (strcasecmp(value, "true") == 0);
|
2017-04-23 02:14:14 +00:00
|
|
|
} else if (strcasecmp(name, "codepage") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(door->codepage);
|
|
|
|
door->codepage = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int file_sub_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-08-02 01:58:10 +00:00
|
|
|
struct file_directory *fd = (struct file_directory *)user;
|
|
|
|
|
|
|
|
if (strcasecmp(section, "main") == 0) {
|
2016-08-08 07:21:04 +00:00
|
|
|
if (strcasecmp(name, "visible sec level") == 0) {
|
2016-08-02 01:58:10 +00:00
|
|
|
fd->sec_level = atoi(value);
|
2018-02-15 04:43:37 +00:00
|
|
|
} else if (strcasecmp(name, "visible on web") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
fd->display_on_web = (strcasecmp(value, "true") == 0);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// check if it's partially filled in
|
|
|
|
struct file_sub *sub = NULL;
|
|
|
|
for (size_t i = 0; i < ptr_vector_len(&fd->file_subs); ++i) {
|
|
|
|
struct file_sub *asub = ptr_vector_get(&fd->file_subs, i);
|
|
|
|
assert(asub != NULL);
|
|
|
|
if (strcasecmp(asub->name, section) == 0) {
|
|
|
|
sub = asub;
|
|
|
|
break;
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
}
|
|
|
|
if (sub == NULL) {
|
|
|
|
sub = (struct file_sub *)malloz(sizeof(struct file_sub));
|
|
|
|
ptr_vector_append(&fd->file_subs, sub);
|
|
|
|
sub->name = strdup(section);
|
|
|
|
}
|
|
|
|
if (strcasecmp(name, "upload sec level") == 0) {
|
|
|
|
sub->upload_sec_level = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "download sec level") == 0) {
|
|
|
|
sub->download_sec_level = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "database") == 0) {
|
|
|
|
free(sub->database);
|
|
|
|
sub->database = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "upload path") == 0) {
|
|
|
|
free(sub->upload_path);
|
|
|
|
sub->upload_path = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int mail_area_handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-08-02 01:58:10 +00:00
|
|
|
struct mail_conference *mc = (struct mail_conference *)user;
|
|
|
|
|
|
|
|
if (strcasecmp(section, "main") == 0) {
|
|
|
|
if (strcasecmp(name, "visible sec level") == 0) {
|
|
|
|
mc->sec_level = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "networked") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
mc->networked = (strcasecmp(value, "true") == 0);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "real names") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
mc->realnames = (strcasecmp(value, "true") == 0);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "tagline") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(mc->tagline);
|
2016-08-02 01:58:10 +00:00
|
|
|
mc->tagline = strdup(value);
|
2018-10-04 00:05:04 +00:00
|
|
|
} else if (strcasecmp(name, "header") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(mc->header);
|
2018-10-04 00:05:04 +00:00
|
|
|
mc->header = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(section, "network") == 0) {
|
|
|
|
if (strcasecmp(name, "type") == 0) {
|
|
|
|
if (strcasecmp(value, "fido") == 0) {
|
|
|
|
mc->nettype = NETWORK_FIDO;
|
2018-05-22 11:02:22 +00:00
|
|
|
} else if (strcasecmp(value, "magi") == 0) {
|
|
|
|
mc->nettype = NETWORK_MAGI;
|
2018-06-24 00:28:18 +00:00
|
|
|
} else if (strcasecmp(value, "qwk") == 0) {
|
|
|
|
mc->nettype = NETWORK_QWK;
|
2018-05-22 11:02:22 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "fido node") == 0) {
|
|
|
|
mc->fidoaddr = parse_fido_addr(value);
|
2018-02-05 22:05:02 +00:00
|
|
|
} else if (strcasecmp(name, "domain") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
free(mc->domain);
|
2018-02-05 22:05:02 +00:00
|
|
|
mc->domain = strdup(value);
|
2018-05-22 11:02:22 +00:00
|
|
|
} else if (strcasecmp(name, "magi node") == 0) {
|
|
|
|
mc->maginode = atoi(value);
|
2018-02-05 22:05:02 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
} else {
|
|
|
|
// check if it's partially filled in
|
2018-10-16 15:05:15 +00:00
|
|
|
struct mail_area *area = NULL;
|
|
|
|
for (size_t i = 0; i < ptr_vector_len(&mc->mail_areas); ++i) {
|
|
|
|
struct mail_area *anarea = ptr_vector_get(&mc->mail_areas, i);
|
|
|
|
if (strcasecmp(anarea->name, section) == 0) {
|
|
|
|
area = anarea;
|
|
|
|
break;
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
if (area == NULL) {
|
|
|
|
area = (struct mail_area *)malloz(sizeof(struct mail_area));
|
|
|
|
ptr_vector_append(&mc->mail_areas, area);
|
|
|
|
area->qwkname = NULL;
|
|
|
|
area->name = strdup(section);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
if (strcasecmp(name, "read sec level") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->read_sec_level = atoi(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "write sec level") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->write_sec_level = atoi(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "path") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->path = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "type") == 0) {
|
|
|
|
if (strcasecmp(value, "local") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->type = TYPE_LOCAL_AREA;
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(value, "echo") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->type = TYPE_ECHOMAIL_AREA;
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(value, "netmail") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->type = TYPE_NETMAIL_AREA;
|
2016-12-09 09:47:33 +00:00
|
|
|
} else if (strcasecmp(value, "newsgroup") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->type = TYPE_NEWSGROUP_AREA;
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
2016-12-05 03:38:03 +00:00
|
|
|
} else if (strcasecmp(name, "qwk name") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
area->qwkname = strndup(value, 8);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
static int handler(void *user, const char *section, const char *name,
|
|
|
|
const char *value) {
|
2016-08-02 01:58:10 +00:00
|
|
|
struct bbs_config *conf = (struct bbs_config *)user;
|
2018-02-18 09:52:55 +00:00
|
|
|
struct passwd *pwd;
|
2016-08-02 01:58:10 +00:00
|
|
|
|
|
|
|
if (strcasecmp(section, "main") == 0) {
|
|
|
|
if (strcasecmp(name, "bbs name") == 0) {
|
|
|
|
conf->bbs_name = strdup(value);
|
2018-10-30 22:42:35 +00:00
|
|
|
} else if (strcasecmp(name, "external address") == 0) {
|
|
|
|
conf->external_address = strdup(value);
|
2016-08-06 11:57:12 +00:00
|
|
|
} else if (strcasecmp(name, "telnet port") == 0) {
|
|
|
|
conf->telnet_port = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "enable ssh") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->ssh_server = 1;
|
|
|
|
} else {
|
|
|
|
conf->ssh_server = 0;
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2018-01-18 11:27:10 +00:00
|
|
|
} else if (strcasecmp(name, "enable ipv6") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->ipv6 = 1;
|
|
|
|
} else {
|
|
|
|
conf->ipv6 = 0;
|
2016-08-06 11:57:12 +00:00
|
|
|
}
|
2016-08-16 10:28:48 +00:00
|
|
|
} else if (strcasecmp(name, "enable www") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->www_server = 1;
|
|
|
|
} else {
|
|
|
|
conf->www_server = 0;
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "www port") == 0) {
|
|
|
|
conf->www_port = atoi(value);
|
2018-01-21 05:02:21 +00:00
|
|
|
} else if (strcasecmp(name, "www url") == 0) {
|
|
|
|
if (value[strlen(value) - 1] == '/') {
|
|
|
|
conf->www_url = strdup(value);
|
|
|
|
} else {
|
2018-10-09 15:55:13 +00:00
|
|
|
conf->www_url = str2dup(value, "/");
|
2018-01-21 05:02:21 +00:00
|
|
|
}
|
2016-08-06 11:57:12 +00:00
|
|
|
} else if (strcasecmp(name, "ssh port") == 0) {
|
|
|
|
conf->ssh_port = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "ssh dsa key") == 0) {
|
|
|
|
conf->ssh_dsa_key = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "ssh rsa key") == 0) {
|
|
|
|
conf->ssh_rsa_key = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "sysop name") == 0) {
|
|
|
|
conf->sysop_name = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "nodes") == 0) {
|
|
|
|
conf->nodes = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "new user level") == 0) {
|
|
|
|
conf->newuserlvl = atoi(value);
|
2017-03-20 01:37:56 +00:00
|
|
|
} else if (strcasecmp(name, "magichat server") == 0) {
|
|
|
|
conf->mgchat_server = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "magichat port") == 0) {
|
|
|
|
conf->mgchat_port = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "magichat bbstag") == 0) {
|
|
|
|
conf->mgchat_bbstag = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "default tagline") == 0) {
|
|
|
|
conf->default_tagline = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "external editor cmd") == 0) {
|
|
|
|
conf->external_editor_cmd = strdup(value);
|
2017-04-23 02:14:14 +00:00
|
|
|
} else if (strcasecmp(name, "external editor codepage") == 0) {
|
|
|
|
conf->external_editor_codepage = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(name, "external editor stdio") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->external_editor_stdio = 1;
|
|
|
|
} else {
|
|
|
|
conf->external_editor_stdio = 0;
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "automessage write level") == 0) {
|
|
|
|
conf->automsgwritelvl = atoi(value);
|
2018-10-08 14:13:11 +00:00
|
|
|
} else if (strcasecmp(name, "fork") == 0) {
|
2016-08-02 01:58:10 +00:00
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->fork = 1;
|
|
|
|
} else {
|
|
|
|
conf->fork = 0;
|
|
|
|
}
|
2016-12-06 07:06:28 +00:00
|
|
|
} else if (strcasecmp(name, "qwk name") == 0) {
|
|
|
|
conf->bwave_name = strdup(value);
|
|
|
|
if (strlen(conf->bwave_name) > 8) {
|
|
|
|
conf->bwave_name[8] = '\0';
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "main aka") == 0) {
|
|
|
|
conf->main_aka = parse_fido_addr(value);
|
|
|
|
} else if (strcasecmp(name, "qwk max messages") == 0) {
|
|
|
|
conf->bwave_max_msgs = atoi(value);
|
2018-06-18 06:52:27 +00:00
|
|
|
} else if (strcasecmp(name, "mqtt enable") == 0) {
|
2016-12-08 12:17:19 +00:00
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->broadcast_enable = 1;
|
|
|
|
} else {
|
|
|
|
conf->broadcast_enable = 0;
|
|
|
|
}
|
2018-06-18 06:52:27 +00:00
|
|
|
} else if (strcasecmp(name, "mqtt port") == 0) {
|
2016-12-08 12:17:19 +00:00
|
|
|
conf->broadcast_port = atoi(value);
|
2018-06-18 06:52:27 +00:00
|
|
|
} else if (strcasecmp(name, "mqtt address") == 0) {
|
2016-12-08 12:27:06 +00:00
|
|
|
conf->broadcast_address = strdup(value);
|
2018-06-18 06:52:27 +00:00
|
|
|
} else if (strcasecmp(name, "mqtt topic") == 0) {
|
|
|
|
conf->broadcast_topic = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "mqtt user") == 0) {
|
|
|
|
conf->broadcast_user = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "mqtt pass") == 0) {
|
|
|
|
conf->broadcast_pass = strdup(value);
|
2016-12-11 04:42:28 +00:00
|
|
|
} else if (strcasecmp(name, "ip guard enable") == 0) {
|
|
|
|
if (strcasecmp(value, "true") == 0) {
|
|
|
|
conf->ipguard_enable = 1;
|
|
|
|
} else {
|
|
|
|
conf->ipguard_enable = 0;
|
|
|
|
}
|
|
|
|
} else if (strcasecmp(name, "ip guard timeout") == 0) {
|
|
|
|
conf->ipguard_timeout = atoi(value);
|
|
|
|
} else if (strcasecmp(name, "ip guard tries") == 0) {
|
|
|
|
conf->ipguard_tries = atoi(value);
|
2017-04-12 06:38:33 +00:00
|
|
|
} else if (strcasecmp(name, "root menu") == 0) {
|
|
|
|
conf->root_menu = strdup(value);
|
2017-04-23 00:51:40 +00:00
|
|
|
} else if (strcasecmp(name, "codepage") == 0) {
|
|
|
|
if (strcasecmp(value, "cp437") == 0) {
|
|
|
|
conf->codepage = 0;
|
2017-04-23 02:14:14 +00:00
|
|
|
} else if (strcasecmp(value, "utf-8") == 0) {
|
2017-04-23 00:51:40 +00:00
|
|
|
conf->codepage = 1;
|
|
|
|
}
|
2017-10-03 02:09:50 +00:00
|
|
|
} else if (strcasecmp(name, "date style") == 0) {
|
|
|
|
if (strcasecmp(value, "us") == 0) {
|
|
|
|
conf->date_style = 1;
|
|
|
|
} else {
|
|
|
|
conf->date_style = 0;
|
|
|
|
}
|
2018-02-18 09:52:55 +00:00
|
|
|
} else if (strcasecmp(name, "run as user") == 0) {
|
|
|
|
pwd = getpwnam(value);
|
|
|
|
if (pwd != NULL) {
|
|
|
|
conf->uid = pwd->pw_uid;
|
|
|
|
conf->gid = pwd->pw_gid;
|
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
} else if (strcasecmp(section, "paths") == 0) {
|
2016-08-02 01:58:10 +00:00
|
|
|
if (strcasecmp(name, "ansi path") == 0) {
|
|
|
|
conf->ansi_path = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "bbs path") == 0) {
|
|
|
|
conf->bbs_path = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "log path") == 0) {
|
|
|
|
conf->log_path = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "script path") == 0) {
|
|
|
|
conf->script_path = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "echomail semaphore") == 0) {
|
|
|
|
conf->echomail_sem = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "netmail semaphore") == 0) {
|
|
|
|
conf->netmail_sem = strdup(value);
|
|
|
|
} else if (strcasecmp(name, "pid file") == 0) {
|
|
|
|
conf->pid_file = strdup(value);
|
2016-08-14 09:56:15 +00:00
|
|
|
} else if (strcasecmp(name, "string file") == 0) {
|
|
|
|
conf->string_file = strdup(value);
|
2016-08-16 10:28:48 +00:00
|
|
|
} else if (strcasecmp(name, "www path") == 0) {
|
|
|
|
conf->www_path = strdup(value);
|
2016-12-10 07:55:47 +00:00
|
|
|
} else if (strcasecmp(name, "config path") == 0) {
|
|
|
|
conf->config_path = strdup(value);
|
2017-04-12 12:34:08 +00:00
|
|
|
} else if (strcasecmp(name, "menu path") == 0) {
|
|
|
|
conf->menu_path = strdup(value);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
} else if (strcasecmp(section, "mail conferences") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
struct mail_conference *conference = malloz(sizeof(struct mail_conference));
|
|
|
|
conference->name = strdup(name);
|
|
|
|
conference->path = strdup(value);
|
|
|
|
conference->tagline = NULL;
|
|
|
|
init_ptr_vector(&conference->mail_areas);
|
|
|
|
conference->nettype = 0;
|
|
|
|
conference->domain = NULL;
|
|
|
|
conference->header = NULL;
|
|
|
|
ptr_vector_append(&conf->mail_conferences, conference);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(section, "file directories") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
struct file_directory *dir = malloz(sizeof(struct file_directory));
|
|
|
|
dir->name = strdup(name);
|
|
|
|
dir->path = strdup(value);
|
|
|
|
init_ptr_vector(&dir->file_subs);
|
|
|
|
dir->display_on_web = 0;
|
|
|
|
ptr_vector_append(&conf->file_directories, dir);
|
2016-08-02 01:58:10 +00:00
|
|
|
} else if (strcasecmp(section, "text files") == 0) {
|
2018-10-16 15:05:15 +00:00
|
|
|
struct text_file *file = malloz(sizeof(struct text_file));
|
|
|
|
file->name = strdup(name);
|
|
|
|
file->path = strdup(value);
|
|
|
|
ptr_vector_append(&conf->text_files, file);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:36:02 +00:00
|
|
|
#ifndef DISABLE_SSH
|
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
int ssh_authenticate(ssh_session p_ssh_session) {
|
|
|
|
ssh_message message;
|
|
|
|
char *username;
|
|
|
|
char *password;
|
|
|
|
|
|
|
|
do {
|
2016-08-07 00:06:21 +00:00
|
|
|
message = ssh_message_get(p_ssh_session);
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-05-30 23:56:01 +00:00
|
|
|
if (message == NULL) {
|
|
|
|
gUser = NULL;
|
2018-10-08 14:13:11 +00:00
|
|
|
return 0;
|
2018-05-30 23:56:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
switch (ssh_message_type(message)) {
|
2016-08-06 11:57:12 +00:00
|
|
|
case SSH_REQUEST_AUTH:
|
2018-10-08 14:13:11 +00:00
|
|
|
switch (ssh_message_subtype(message)) {
|
2016-08-06 11:57:12 +00:00
|
|
|
case SSH_AUTH_METHOD_PASSWORD:
|
|
|
|
username = ssh_message_auth_user(message);
|
|
|
|
password = ssh_message_auth_password(message);
|
|
|
|
|
|
|
|
if (strcasecmp(username, "new") == 0 && strcasecmp(password, "new") == 0) {
|
2016-08-07 02:29:21 +00:00
|
|
|
ssh_message_auth_reply_success(message, 0);
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_message_free(message);
|
|
|
|
gUser = NULL;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
gUser = check_user_pass(username, password);
|
|
|
|
if (gUser != NULL) {
|
2016-08-07 02:29:21 +00:00
|
|
|
ssh_message_auth_reply_success(message, 0);
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_message_free(message);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-07 04:18:57 +00:00
|
|
|
ssh_message_free(message);
|
|
|
|
return 0;
|
2016-08-06 11:57:12 +00:00
|
|
|
case SSH_AUTH_METHOD_NONE:
|
|
|
|
default:
|
|
|
|
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_INTERACTIVE);
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_PASSWORD | SSH_AUTH_METHOD_INTERACTIVE);
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
break;
|
|
|
|
}
|
2018-05-30 23:56:01 +00:00
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_message_free(message);
|
2018-10-08 14:13:11 +00:00
|
|
|
} while (1);
|
2016-08-06 11:57:12 +00:00
|
|
|
}
|
2017-04-04 05:07:25 +00:00
|
|
|
/*
|
2016-08-06 23:54:37 +00:00
|
|
|
char *ssh_getip(ssh_session session) {
|
|
|
|
struct sockaddr_storage tmp;
|
|
|
|
struct sockaddr_in *sock;
|
|
|
|
unsigned int len = 100;
|
|
|
|
char ip[100] = "\0";
|
|
|
|
|
|
|
|
getpeername(ssh_get_fd(session), (struct sockaddr*)&tmp, &len);
|
|
|
|
sock = (struct sockaddr_in *)&tmp;
|
|
|
|
inet_ntop(AF_INET, &sock->sin_addr, ip, len);
|
|
|
|
|
|
|
|
return strdup(ip);
|
|
|
|
}
|
2017-04-04 05:07:25 +00:00
|
|
|
*/
|
2016-08-06 23:54:37 +00:00
|
|
|
static int ssh_copy_fd_to_chan(socket_t fd, int revents, void *userdata) {
|
2018-10-08 14:13:11 +00:00
|
|
|
ssh_channel chan = (ssh_channel)userdata;
|
|
|
|
char buf[2048];
|
|
|
|
int sz = 0;
|
|
|
|
|
|
|
|
if (!chan) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (revents & POLLIN) {
|
|
|
|
sz = read(fd, buf, 2048);
|
|
|
|
if (sz > 0) {
|
|
|
|
ssh_channel_write(chan, buf, sz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (revents & POLLHUP) {
|
|
|
|
ssh_channel_close(chan);
|
|
|
|
sz = -1;
|
|
|
|
}
|
|
|
|
return sz;
|
2016-08-06 23:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ssh_copy_chan_to_fd(ssh_session session,
|
2018-10-08 14:13:11 +00:00
|
|
|
ssh_channel channel,
|
|
|
|
void *data,
|
|
|
|
uint32_t len,
|
|
|
|
int is_stderr,
|
|
|
|
void *userdata) {
|
|
|
|
int fd = *(int *)userdata;
|
|
|
|
int sz;
|
|
|
|
(void)session;
|
|
|
|
(void)channel;
|
|
|
|
(void)is_stderr;
|
|
|
|
|
|
|
|
sz = write(fd, data, len);
|
|
|
|
return sz;
|
2016-08-06 23:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ssh_chan_close(ssh_session session, ssh_channel channel, void *userdata) {
|
2018-10-08 14:13:11 +00:00
|
|
|
int fd = *(int *)userdata;
|
2016-08-10 01:22:55 +00:00
|
|
|
int status;
|
2018-10-08 14:13:11 +00:00
|
|
|
(void)session;
|
|
|
|
(void)channel;
|
|
|
|
close(fd);
|
2016-08-06 23:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ssh_channel_callbacks_struct ssh_cb = {
|
2018-10-08 14:13:11 +00:00
|
|
|
.channel_data_function = ssh_copy_chan_to_fd,
|
|
|
|
.channel_eof_function = ssh_chan_close,
|
|
|
|
.channel_close_function = ssh_chan_close,
|
|
|
|
.userdata = NULL};
|
2016-08-06 23:54:37 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
void serverssh(int port, int ipv6) {
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_session p_ssh_session;
|
|
|
|
ssh_bind p_ssh_bind;
|
|
|
|
int err;
|
|
|
|
int pid;
|
2016-08-07 03:56:33 +00:00
|
|
|
int shell = 0;
|
2016-08-06 23:54:37 +00:00
|
|
|
int fd;
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_channel chan = 0;
|
2016-08-06 23:54:37 +00:00
|
|
|
char *ip;
|
|
|
|
ssh_event event;
|
|
|
|
short events;
|
2016-08-07 00:06:21 +00:00
|
|
|
ssh_message message;
|
2016-08-07 03:12:54 +00:00
|
|
|
struct termios tios;
|
2016-12-11 12:14:36 +00:00
|
|
|
struct ip_address_guard *ip_guard;
|
|
|
|
int i;
|
|
|
|
char buffer[1024];
|
|
|
|
FILE *fptr;
|
2017-04-04 05:07:25 +00:00
|
|
|
struct sockaddr_in6 server, client;
|
2018-01-18 11:27:10 +00:00
|
|
|
struct sockaddr_in server4, client4;
|
|
|
|
void *server_p, *client_p;
|
2017-04-04 05:07:25 +00:00
|
|
|
int ssh_sock, csock, c;
|
|
|
|
int on = 1;
|
|
|
|
char str[INET6_ADDRSTRLEN];
|
|
|
|
|
2016-12-11 12:14:36 +00:00
|
|
|
bbs_stdin = dup(STDIN_FILENO);
|
|
|
|
bbs_stdout = dup(STDOUT_FILENO);
|
|
|
|
bbs_stderr = dup(STDERR_FILENO);
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
err = ssh_init();
|
|
|
|
if (err == -1) {
|
|
|
|
fprintf(stderr, "Error starting SSH server.\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
p_ssh_bind = ssh_bind_new();
|
|
|
|
if (p_ssh_bind == NULL) {
|
|
|
|
fprintf(stderr, "Error starting SSH server.\n");
|
2016-08-07 00:06:21 +00:00
|
|
|
exit(-1);
|
2016-08-06 11:57:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 00:06:21 +00:00
|
|
|
ssh_bind_options_set(p_ssh_bind, SSH_BIND_OPTIONS_DSAKEY, conf.ssh_dsa_key);
|
|
|
|
ssh_bind_options_set(p_ssh_bind, SSH_BIND_OPTIONS_RSAKEY, conf.ssh_rsa_key);
|
2016-08-06 11:57:12 +00:00
|
|
|
|
2017-04-04 05:07:25 +00:00
|
|
|
//ssh_bind_listen(p_ssh_bind);
|
2018-01-18 11:27:10 +00:00
|
|
|
if (ipv6) {
|
|
|
|
ssh_sock = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
} else {
|
|
|
|
ssh_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
}
|
2017-04-04 05:10:35 +00:00
|
|
|
if (ssh_sock == -1) {
|
2017-04-04 05:07:25 +00:00
|
|
|
fprintf(stderr, "Error starting SSH server.\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setsockopt(ssh_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
|
|
|
|
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
if (ipv6) {
|
|
|
|
if (setsockopt(ssh_sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on)) < 0) {
|
|
|
|
fprintf(stderr, "setsockopt(IPV6_V6ONLY) failed");
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
memset(&server, 0, sizeof(server));
|
|
|
|
server.sin6_family = AF_INET6;
|
|
|
|
server.sin6_addr = in6addr_any;
|
|
|
|
server.sin6_port = htons(port);
|
2017-04-04 05:07:25 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
server_p = &server;
|
|
|
|
client_p = &client;
|
2017-04-04 05:07:25 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
if (bind(ssh_sock, (struct sockaddr *)server_p, sizeof(struct sockaddr_in6)) < 0) {
|
|
|
|
perror("Bind Failed, Error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
c = sizeof(struct sockaddr_in6);
|
|
|
|
} else {
|
|
|
|
memset(&server4, 0, sizeof(server4));
|
|
|
|
server4.sin_family = AF_INET;
|
|
|
|
server4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
server4.sin_port = htons(port);
|
|
|
|
|
|
|
|
server_p = &server4;
|
|
|
|
client_p = &client4;
|
|
|
|
|
|
|
|
if (bind(ssh_sock, (struct sockaddr *)server_p, sizeof(struct sockaddr_in)) < 0) {
|
|
|
|
perror("Bind Failed, Error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
c = sizeof(struct sockaddr_in);
|
2017-04-04 05:07:25 +00:00
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-02-18 09:52:55 +00:00
|
|
|
if (conf.uid != getuid()) {
|
|
|
|
if (setgid(conf.gid) != 0 || setuid(conf.uid) != 0) {
|
|
|
|
perror("SetUID Failed: ");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 11:57:12 +00:00
|
|
|
|
2017-04-04 05:07:25 +00:00
|
|
|
listen(ssh_sock, 3);
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
while ((csock = accept(ssh_sock, (struct sockaddr *)client_p, (socklen_t *)&c))) {
|
2017-09-15 10:56:43 +00:00
|
|
|
p_ssh_session = ssh_new();
|
|
|
|
if (p_ssh_session == NULL) {
|
|
|
|
fprintf(stderr, "Error starting SSH session.\n");
|
|
|
|
close(csock);
|
|
|
|
continue;
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2017-04-04 05:07:25 +00:00
|
|
|
if (ssh_bind_accept_fd(p_ssh_bind, p_ssh_session, csock) == SSH_OK) {
|
2018-01-18 11:27:10 +00:00
|
|
|
if (ipv6) {
|
|
|
|
ip = strdup(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)client_p)->sin6_addr, str, sizeof(str)));
|
|
|
|
} else {
|
|
|
|
ip = strdup(inet_ntop(AF_INET, &((struct sockaddr_in *)client_p)->sin_addr, str, sizeof(str)));
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2016-12-11 12:14:36 +00:00
|
|
|
if (conf.ipguard_enable) {
|
|
|
|
i = hashmap_get(ip_guard_map, ip, (void **)(&ip_guard));
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 12:14:36 +00:00
|
|
|
if (i == MAP_MISSING) {
|
2018-10-09 15:55:13 +00:00
|
|
|
ip_guard = (struct ip_address_guard *)malloz(sizeof(struct ip_address_guard));
|
2016-12-11 12:14:36 +00:00
|
|
|
ip_guard->status = IP_STATUS_UNKNOWN;
|
|
|
|
ip_guard->last_connection = time(NULL);
|
|
|
|
ip_guard->connection_count = 1;
|
|
|
|
hashmap_put(ip_guard_map, strdup(ip), ip_guard);
|
|
|
|
} else if (i == MAP_OK) {
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 12:14:36 +00:00
|
|
|
if (ip_guard->status == IP_STATUS_BLACKLISTED) {
|
|
|
|
free(ip);
|
|
|
|
ssh_disconnect(p_ssh_session);
|
|
|
|
continue;
|
|
|
|
} else if (ip_guard->status == IP_STATUS_UNKNOWN) {
|
|
|
|
if (ip_guard->last_connection + conf.ipguard_timeout > time(NULL)) {
|
|
|
|
ip_guard->connection_count++;
|
|
|
|
if (ip_guard->connection_count == conf.ipguard_tries) {
|
|
|
|
ip_guard->status = IP_STATUS_BLACKLISTED;
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/blacklist.ip%d", conf.bbs_path, (ipv6 ? 6 : 4));
|
2016-12-11 12:14:36 +00:00
|
|
|
fptr = fopen(buffer, "a");
|
|
|
|
fprintf(fptr, "%s\n", ip);
|
|
|
|
fclose(fptr);
|
|
|
|
free(ip);
|
|
|
|
ssh_disconnect(p_ssh_session);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ip_guard->connection_count = 0;
|
|
|
|
ip_guard->last_connection = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
2017-04-04 05:19:16 +00:00
|
|
|
close(ssh_sock);
|
2016-08-06 11:57:12 +00:00
|
|
|
if (ssh_handle_key_exchange(p_ssh_session)) {
|
2017-04-04 05:19:16 +00:00
|
|
|
fprintf(stderr, "Key exchange failed.\n");
|
2016-08-06 11:57:12 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (ssh_authenticate(p_ssh_session) == 1) {
|
|
|
|
do {
|
|
|
|
message = ssh_message_get(p_ssh_session);
|
|
|
|
if (message) {
|
2016-08-07 02:29:21 +00:00
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
if (ssh_message_type(message) == SSH_REQUEST_CHANNEL_OPEN && ssh_message_subtype(message) == SSH_CHANNEL_SESSION) {
|
|
|
|
chan = ssh_message_channel_request_open_reply_accept(message);
|
|
|
|
ssh_message_free(message);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ssh_message_reply_default(message);
|
|
|
|
ssh_message_free(message);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
} while (!chan);
|
2016-08-06 11:57:12 +00:00
|
|
|
if (!chan) {
|
2016-08-07 02:20:31 +00:00
|
|
|
fprintf(stderr, "Failed to get channel\n");
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_finalize();
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
message = ssh_message_get(p_ssh_session);
|
|
|
|
if (message) {
|
|
|
|
if (ssh_message_type(message) == SSH_REQUEST_CHANNEL) {
|
|
|
|
if (ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_SHELL) {
|
2018-10-08 14:13:11 +00:00
|
|
|
shell = 1;
|
2016-08-07 02:01:50 +00:00
|
|
|
ssh_message_channel_request_reply_success(message);
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_message_free(message);
|
|
|
|
break;
|
|
|
|
} else if (ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_PTY) {
|
2016-08-07 02:01:50 +00:00
|
|
|
ssh_message_channel_request_reply_success(message);
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_message_free(message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!shell);
|
|
|
|
|
|
|
|
if (!shell) {
|
2016-08-07 02:20:31 +00:00
|
|
|
fprintf(stderr, "Failed to get shell\n");
|
2016-08-06 11:57:12 +00:00
|
|
|
ssh_finalize();
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-08-06 23:54:37 +00:00
|
|
|
bbs_pid = forkpty(&fd, NULL, NULL, NULL);
|
|
|
|
if (bbs_pid == 0) {
|
2016-08-07 03:14:26 +00:00
|
|
|
tcgetattr(STDIN_FILENO, &tios);
|
2016-08-07 03:12:54 +00:00
|
|
|
tios.c_lflag &= ~(ICANON | ECHO | ECHONL);
|
2016-08-07 03:45:52 +00:00
|
|
|
tios.c_iflag &= INLCR;
|
2016-08-07 03:14:26 +00:00
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
|
2016-08-06 23:54:37 +00:00
|
|
|
runbbs_ssh(ip);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
free(ip);
|
|
|
|
ssh_cb.userdata = &fd;
|
|
|
|
ssh_callbacks_init(&ssh_cb);
|
2016-08-07 00:07:32 +00:00
|
|
|
ssh_set_channel_callbacks(chan, &ssh_cb);
|
2016-08-06 23:54:37 +00:00
|
|
|
|
|
|
|
events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL;
|
|
|
|
|
|
|
|
event = ssh_event_new();
|
2018-10-08 14:13:11 +00:00
|
|
|
if (event == NULL) {
|
2016-08-06 23:54:37 +00:00
|
|
|
ssh_finalize();
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ssh_event_add_fd(event, fd, events, ssh_copy_fd_to_chan, chan) != SSH_OK) {
|
2016-08-06 23:54:37 +00:00
|
|
|
ssh_finalize();
|
|
|
|
exit(0);
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ssh_event_add_session(event, p_ssh_session) != SSH_OK) {
|
2016-08-06 23:54:37 +00:00
|
|
|
ssh_finalize();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ssh_event_dopoll(event, 1000);
|
2018-10-08 14:13:11 +00:00
|
|
|
} while (!ssh_channel_is_closed(chan));
|
2016-08-06 23:54:37 +00:00
|
|
|
|
|
|
|
ssh_event_remove_fd(event, fd);
|
|
|
|
|
|
|
|
ssh_event_remove_session(event, p_ssh_session);
|
|
|
|
|
|
|
|
ssh_event_free(event);
|
2016-08-06 11:57:12 +00:00
|
|
|
}
|
2016-08-07 04:28:27 +00:00
|
|
|
ssh_disconnect(p_ssh_session);
|
2016-08-06 23:54:37 +00:00
|
|
|
ssh_finalize();
|
2017-04-04 05:14:06 +00:00
|
|
|
close(csock);
|
2016-08-06 11:57:12 +00:00
|
|
|
exit(0);
|
|
|
|
} else if (pid > 0) {
|
2017-09-15 10:56:43 +00:00
|
|
|
ssh_free(p_ssh_session);
|
2017-04-04 05:14:06 +00:00
|
|
|
close(csock);
|
2017-04-04 05:19:16 +00:00
|
|
|
free(ip);
|
2016-08-06 11:57:12 +00:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 01:36:02 +00:00
|
|
|
#endif
|
2018-01-18 11:27:10 +00:00
|
|
|
void server(int port, int ipv6) {
|
2016-08-02 01:58:10 +00:00
|
|
|
struct sigaction sa;
|
|
|
|
struct sigaction st;
|
2016-08-12 06:15:10 +00:00
|
|
|
struct sigaction sq;
|
|
|
|
int client_sock, c;
|
2016-03-22 01:48:59 +00:00
|
|
|
int pid;
|
2016-12-11 04:42:28 +00:00
|
|
|
char *ip;
|
2017-04-04 04:19:05 +00:00
|
|
|
struct sockaddr_in6 server, client;
|
2018-01-18 11:27:10 +00:00
|
|
|
struct sockaddr_in server4, client4;
|
|
|
|
void *client_p, *server_p;
|
2016-12-11 04:42:28 +00:00
|
|
|
FILE *fptr;
|
|
|
|
char buffer[1024];
|
|
|
|
struct ip_address_guard *ip_guard;
|
|
|
|
int i;
|
2017-04-04 04:19:05 +00:00
|
|
|
int on = 1;
|
|
|
|
char str[INET6_ADDRSTRLEN];
|
2017-09-06 07:33:27 +00:00
|
|
|
struct stat s;
|
2016-08-16 10:28:48 +00:00
|
|
|
#if defined(ENABLE_WWW)
|
|
|
|
www_daemon = NULL;
|
|
|
|
#endif
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
if (conf.ipguard_enable) {
|
|
|
|
|
|
|
|
ip_guard_map = hashmap_new();
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/whitelist.ip%d", conf.bbs_path, (ipv6 ? 6 : 4));
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
fptr = fopen(buffer, "r");
|
|
|
|
if (fptr) {
|
|
|
|
fgets(buffer, 1024, fptr);
|
|
|
|
while (!feof(fptr)) {
|
2018-10-08 14:13:11 +00:00
|
|
|
for (i = strlen(buffer) - 1; i > 0; i--) {
|
2016-12-11 04:42:28 +00:00
|
|
|
if (buffer[i] == '\r' || buffer[i] == '\n') {
|
|
|
|
buffer[i] = '\0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-10-09 15:55:13 +00:00
|
|
|
ip_guard = (struct ip_address_guard *)malloz(sizeof(struct ip_address_guard));
|
2016-12-11 04:42:28 +00:00
|
|
|
ip_guard->status = IP_STATUS_WHITELISTED;
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
hashmap_put(ip_guard_map, strdup(buffer), ip_guard);
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
fgets(buffer, 1024, fptr);
|
|
|
|
}
|
|
|
|
fclose(fptr);
|
|
|
|
}
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/blacklist.ip%d", conf.bbs_path, (ipv6 ? 6 : 4));
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
fptr = fopen(buffer, "r");
|
|
|
|
if (fptr) {
|
|
|
|
fgets(buffer, 1024, fptr);
|
|
|
|
while (!feof(fptr)) {
|
2018-10-08 14:13:11 +00:00
|
|
|
for (i = strlen(buffer) - 1; i > 0; i--) {
|
2016-12-11 04:42:28 +00:00
|
|
|
if (buffer[i] == '\r' || buffer[i] == '\n') {
|
|
|
|
buffer[i] = '\0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2018-10-09 15:55:13 +00:00
|
|
|
ip_guard = (struct ip_address_guard *)malloz(sizeof(struct ip_address_guard));
|
2016-12-11 04:42:28 +00:00
|
|
|
ip_guard->status = IP_STATUS_BLACKLISTED;
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
hashmap_put(ip_guard_map, strdup(buffer), ip_guard);
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
fgets(buffer, 1024, fptr);
|
|
|
|
}
|
|
|
|
fclose(fptr);
|
|
|
|
}
|
|
|
|
}
|
2016-03-27 11:53:46 +00:00
|
|
|
sa.sa_handler = sigchld_handler; // reap all dead processes
|
2016-08-02 01:58:10 +00:00
|
|
|
sigemptyset(&sa.sa_mask);
|
2016-08-16 10:28:48 +00:00
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
2016-08-02 01:58:10 +00:00
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
|
2018-10-08 14:13:11 +00:00
|
|
|
perror("sigaction - sigchld");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st.sa_handler = sigterm_handler;
|
|
|
|
sigemptyset(&st.sa_mask);
|
2016-08-16 10:28:48 +00:00
|
|
|
st.sa_flags = SA_SIGINFO;
|
2016-08-02 01:58:10 +00:00
|
|
|
if (sigaction(SIGTERM, &st, NULL) == -1) {
|
2018-10-08 14:13:11 +00:00
|
|
|
perror("sigaction - sigterm");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
|
2016-08-12 06:15:10 +00:00
|
|
|
sq.sa_handler = sigterm_handler;
|
|
|
|
sigemptyset(&sq.sa_mask);
|
2016-08-16 10:28:48 +00:00
|
|
|
sq.sa_flags = SA_SIGINFO;
|
2016-08-12 06:15:10 +00:00
|
|
|
if (sigaction(SIGQUIT, &sq, NULL) == -1) {
|
2018-10-08 14:13:11 +00:00
|
|
|
perror("sigaction - sigquit");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
2016-08-12 06:15:10 +00:00
|
|
|
}
|
2018-10-18 01:36:02 +00:00
|
|
|
#ifndef DISABLE_SSH
|
2016-08-06 23:54:37 +00:00
|
|
|
if (conf.ssh_server) {
|
2017-04-09 03:17:22 +00:00
|
|
|
if (!conf.fork) {
|
2018-01-18 11:27:10 +00:00
|
|
|
printf(" - SSH Starting on Port %d (IPv%d)\n", conf.ssh_port, (ipv6 ? 6 : 4));
|
2017-04-09 03:17:22 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 23:54:37 +00:00
|
|
|
// fork ssh server
|
|
|
|
ssh_pid = fork();
|
|
|
|
|
2016-08-13 01:07:02 +00:00
|
|
|
if (ssh_pid == 0) {
|
2018-02-18 09:52:55 +00:00
|
|
|
ipv6_pid = -1;
|
2016-08-13 04:17:28 +00:00
|
|
|
ssh_pid = -1;
|
2018-01-18 11:27:10 +00:00
|
|
|
serverssh(conf.ssh_port, ipv6);
|
2016-08-06 23:54:37 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2016-08-13 01:07:02 +00:00
|
|
|
if (ssh_pid < 0) {
|
2016-08-06 23:54:37 +00:00
|
|
|
fprintf(stderr, "Error forking ssh server.");
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 01:36:02 +00:00
|
|
|
#endif
|
2018-01-18 11:27:10 +00:00
|
|
|
if (ipv6) {
|
|
|
|
server_socket = socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
} else {
|
|
|
|
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
}
|
|
|
|
|
2016-08-12 06:15:10 +00:00
|
|
|
if (server_socket == -1) {
|
2016-08-05 03:49:24 +00:00
|
|
|
remove(conf.pid_file);
|
2016-08-04 09:25:49 +00:00
|
|
|
fprintf(stderr, "Couldn't create socket..\n");
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(1);
|
2016-03-22 01:48:59 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2017-04-04 04:19:05 +00:00
|
|
|
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
|
|
|
|
remove(conf.pid_file);
|
|
|
|
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-04-09 03:17:22 +00:00
|
|
|
if (!conf.fork) {
|
2018-01-18 11:27:10 +00:00
|
|
|
printf(" - Telnet Starting on Port %d (IPv%d)\n", port, (ipv6 ? 6 : 4));
|
2017-04-09 03:17:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
if (ipv6) {
|
|
|
|
if (setsockopt(server_socket, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on)) < 0) {
|
|
|
|
fprintf(stderr, "setsockopt(IPV6_V6ONLY) failed");
|
|
|
|
}
|
|
|
|
memset(&server, 0, sizeof(server));
|
|
|
|
|
|
|
|
server.sin6_family = AF_INET6;
|
|
|
|
server.sin6_addr = in6addr_any;
|
|
|
|
server.sin6_port = htons(port);
|
|
|
|
|
|
|
|
if (bind(server_socket, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
|
|
|
perror("Bind Failed, Error\n");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
c = sizeof(struct sockaddr_in6);
|
|
|
|
server_p = &server;
|
|
|
|
client_p = &client;
|
|
|
|
} else {
|
|
|
|
memset(&server4, 0, sizeof(server4));
|
|
|
|
|
|
|
|
server4.sin_family = AF_INET;
|
|
|
|
server4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
server4.sin_port = htons(port);
|
|
|
|
|
|
|
|
if (bind(server_socket, (struct sockaddr *)&server4, sizeof(server4)) < 0) {
|
|
|
|
perror("Bind Failed, Error\n");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
c = sizeof(struct sockaddr_in);
|
|
|
|
server_p = &server4;
|
|
|
|
client_p = &client4;
|
2016-03-22 01:48:59 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2018-02-18 09:52:55 +00:00
|
|
|
if (conf.uid != getuid()) {
|
|
|
|
if (setgid(conf.gid) != 0 || setuid(conf.uid) != 0) {
|
|
|
|
perror("SetUID Failed: ");
|
|
|
|
remove(conf.pid_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
#if defined(ENABLE_WWW)
|
2018-02-18 09:52:55 +00:00
|
|
|
if (conf.www_server && conf.www_path != NULL && conf.www_url != NULL) {
|
|
|
|
if (!conf.fork) {
|
|
|
|
printf(" - HTTP Starting on Port %d (IPv%d)\n", conf.www_port, (ipv6 ? 6 : 4));
|
|
|
|
}
|
|
|
|
www_init();
|
|
|
|
if (ipv6) {
|
2018-10-08 14:13:11 +00:00
|
|
|
www_daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_IPv6, conf.www_port, NULL, NULL, &www_handler, NULL, MHD_OPTION_NOTIFY_COMPLETED, &www_request_completed, NULL, MHD_OPTION_URI_LOG_CALLBACK, &www_logger, NULL, MHD_OPTION_END);
|
2018-02-18 09:52:55 +00:00
|
|
|
} else {
|
|
|
|
www_daemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, conf.www_port, NULL, NULL, &www_handler, NULL, MHD_OPTION_NOTIFY_COMPLETED, &www_request_completed, NULL, MHD_OPTION_URI_LOG_CALLBACK, &www_logger, NULL, MHD_OPTION_END);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-12 06:15:10 +00:00
|
|
|
listen(server_socket, 3);
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2018-01-18 11:27:10 +00:00
|
|
|
while ((client_sock = accept(server_socket, (struct sockaddr *)client_p, (socklen_t *)&c))) {
|
|
|
|
if (ipv6) {
|
|
|
|
ip = strdup(inet_ntop(AF_INET6, &((struct sockaddr_in6 *)client_p)->sin6_addr, str, sizeof(str)));
|
|
|
|
} else {
|
|
|
|
ip = strdup(inet_ntop(AF_INET, &((struct sockaddr_in *)client_p)->sin_addr, str, sizeof(str)));
|
|
|
|
}
|
2016-08-01 12:03:46 +00:00
|
|
|
if (client_sock == -1) {
|
2016-08-02 01:58:10 +00:00
|
|
|
if (errno == EINTR) {
|
2016-08-01 12:03:46 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
if (conf.ipguard_enable) {
|
|
|
|
i = hashmap_get(ip_guard_map, ip, (void **)(&ip_guard));
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
if (i == MAP_MISSING) {
|
2018-10-09 15:55:13 +00:00
|
|
|
ip_guard = (struct ip_address_guard *)malloz(sizeof(struct ip_address_guard));
|
2016-12-11 04:42:28 +00:00
|
|
|
ip_guard->status = IP_STATUS_UNKNOWN;
|
|
|
|
ip_guard->last_connection = time(NULL);
|
|
|
|
ip_guard->connection_count = 1;
|
2016-12-11 05:12:18 +00:00
|
|
|
hashmap_put(ip_guard_map, strdup(ip), ip_guard);
|
2016-12-11 04:42:28 +00:00
|
|
|
} else if (i == MAP_OK) {
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-11 04:42:28 +00:00
|
|
|
if (ip_guard->status == IP_STATUS_BLACKLISTED) {
|
|
|
|
write(client_sock, "BLOCKED\r\n", 9);
|
|
|
|
free(ip);
|
|
|
|
close(client_sock);
|
|
|
|
continue;
|
|
|
|
} else if (ip_guard->status == IP_STATUS_UNKNOWN) {
|
2016-12-11 05:17:09 +00:00
|
|
|
if (ip_guard->last_connection + conf.ipguard_timeout > time(NULL)) {
|
2016-12-11 04:42:28 +00:00
|
|
|
ip_guard->connection_count++;
|
|
|
|
if (ip_guard->connection_count == conf.ipguard_tries) {
|
|
|
|
ip_guard->status = IP_STATUS_BLACKLISTED;
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/blacklist.ip%d", conf.bbs_path, (ipv6 ? 6 : 4));
|
2016-12-11 04:42:28 +00:00
|
|
|
fptr = fopen(buffer, "a");
|
|
|
|
fprintf(fptr, "%s\n", ip);
|
|
|
|
fclose(fptr);
|
|
|
|
write(client_sock, "BLOCKED\r\n", 9);
|
|
|
|
free(ip);
|
|
|
|
close(client_sock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ip_guard->connection_count = 0;
|
|
|
|
ip_guard->last_connection = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2016-12-11 04:42:28 +00:00
|
|
|
}
|
2016-03-22 01:48:59 +00:00
|
|
|
pid = fork();
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2016-03-22 01:48:59 +00:00
|
|
|
if (pid < 0) {
|
|
|
|
perror("Error on fork\n");
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(1);
|
2016-03-22 01:48:59 +00:00
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2016-03-22 01:48:59 +00:00
|
|
|
if (pid == 0) {
|
2016-08-12 06:15:10 +00:00
|
|
|
close(server_socket);
|
|
|
|
server_socket = -1;
|
2016-12-11 04:42:28 +00:00
|
|
|
runbbs(client_sock, ip);
|
2016-08-02 01:58:10 +00:00
|
|
|
|
2016-03-22 01:48:59 +00:00
|
|
|
exit(0);
|
|
|
|
} else {
|
2016-12-11 04:42:28 +00:00
|
|
|
free(ip);
|
2016-03-22 01:48:59 +00:00
|
|
|
close(client_sock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i;
|
2018-02-18 09:52:55 +00:00
|
|
|
int main_pid;
|
2016-08-02 01:58:10 +00:00
|
|
|
FILE *fptr;
|
2016-08-04 09:42:50 +00:00
|
|
|
struct stat s;
|
2016-12-10 07:55:47 +00:00
|
|
|
char buffer[1024];
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-08-06 11:57:12 +00:00
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "Usage ./magicka config/bbs.ini\n");
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:05:15 +00:00
|
|
|
init_ptr_vector(&conf.mail_conferences);
|
|
|
|
init_ptr_vector(&conf.doors);
|
|
|
|
init_ptr_vector(&conf.file_directories);
|
2017-03-20 01:37:56 +00:00
|
|
|
conf.mgchat_server = NULL;
|
|
|
|
conf.mgchat_port = 2025;
|
2017-03-20 01:47:36 +00:00
|
|
|
conf.mgchat_bbstag = NULL;
|
2018-10-16 15:05:15 +00:00
|
|
|
init_ptr_vector(&conf.text_files);
|
2016-08-02 01:58:10 +00:00
|
|
|
conf.external_editor_cmd = NULL;
|
2017-04-23 02:14:14 +00:00
|
|
|
conf.external_editor_codepage = NULL;
|
2016-08-02 01:58:10 +00:00
|
|
|
conf.log_path = NULL;
|
|
|
|
conf.script_path = NULL;
|
|
|
|
conf.automsgwritelvl = 10;
|
|
|
|
conf.echomail_sem = NULL;
|
|
|
|
conf.netmail_sem = NULL;
|
2016-08-06 11:57:12 +00:00
|
|
|
conf.telnet_port = 0;
|
2016-08-14 09:56:15 +00:00
|
|
|
conf.string_file = NULL;
|
2016-08-16 10:28:48 +00:00
|
|
|
conf.www_path = NULL;
|
2018-01-21 05:02:21 +00:00
|
|
|
conf.www_url = NULL;
|
2018-10-16 15:05:15 +00:00
|
|
|
init_ptr_vector(&conf.archivers);
|
2016-12-08 12:17:19 +00:00
|
|
|
conf.broadcast_enable = 0;
|
2016-12-09 03:57:17 +00:00
|
|
|
conf.broadcast_port = 0;
|
|
|
|
conf.broadcast_address = NULL;
|
2018-06-18 06:52:27 +00:00
|
|
|
conf.broadcast_topic = NULL;
|
|
|
|
conf.broadcast_user = NULL;
|
|
|
|
conf.broadcast_pass = NULL;
|
2016-12-10 07:55:47 +00:00
|
|
|
conf.config_path = NULL;
|
2016-12-11 04:42:28 +00:00
|
|
|
conf.ipguard_enable = 0;
|
|
|
|
conf.ipguard_tries = 4;
|
|
|
|
conf.ipguard_timeout = 120;
|
2018-10-16 15:05:15 +00:00
|
|
|
init_ptr_vector(&conf.protocols);
|
2017-04-23 00:51:40 +00:00
|
|
|
conf.codepage = 0;
|
2017-10-03 02:09:50 +00:00
|
|
|
conf.date_style = 0;
|
2018-01-18 11:27:10 +00:00
|
|
|
conf.ipv6 = 0;
|
2018-02-18 09:52:55 +00:00
|
|
|
conf.uid = getuid();
|
|
|
|
conf.gid = getgid();
|
2018-10-30 22:42:35 +00:00
|
|
|
conf.external_address = NULL;
|
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
// Load BBS data
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ini_parse(argv[1], handler, &conf) < 0) {
|
2016-08-04 09:25:49 +00:00
|
|
|
fprintf(stderr, "Unable to load configuration ini (%s)!\n", argv[1]);
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2016-12-10 07:55:47 +00:00
|
|
|
if (conf.config_path == NULL) {
|
|
|
|
fprintf(stderr, "Config Path must be set in your bbs ini!\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
|
2017-04-12 12:34:08 +00:00
|
|
|
if (conf.root_menu == NULL) {
|
|
|
|
fprintf(stderr, "Root Menu must be set in your bbs ini!\n");
|
2018-10-08 14:13:11 +00:00
|
|
|
exit(-1);
|
2017-04-12 12:34:08 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
// Load mail Areas
|
2018-10-16 15:05:15 +00:00
|
|
|
for (i = 0; i < ptr_vector_len(&conf.mail_conferences); i++) {
|
|
|
|
struct mail_conference *conference = ptr_vector_get(&conf.mail_conferences, i);
|
|
|
|
if (ini_parse(conference->path, mail_area_handler, conference) < 0) {
|
|
|
|
fprintf(stderr, "Unable to load configuration ini (%s)!\n", conference->path);
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 15:05:15 +00:00
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
// Load file Subs
|
2018-10-16 15:05:15 +00:00
|
|
|
for (i = 0; i < ptr_vector_len(&conf.file_directories); i++) {
|
|
|
|
struct file_directory *dir = ptr_vector_get(&conf.file_directories, i);
|
|
|
|
if (ini_parse(dir->path, file_sub_handler, dir) < 0) {
|
|
|
|
fprintf(stderr, "Unable to load configuration ini (%s)!\n", dir->path);
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 07:55:47 +00:00
|
|
|
snprintf(buffer, 1024, "%s/doors.ini", conf.config_path);
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ini_parse(buffer, door_config_handler, &conf) < 0) {
|
2016-08-04 09:25:49 +00:00
|
|
|
fprintf(stderr, "Unable to load configuration ini (doors.ini)!\n");
|
2016-08-02 01:58:10 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-12-10 07:55:47 +00:00
|
|
|
snprintf(buffer, 1024, "%s/archivers.ini", conf.config_path);
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ini_parse(buffer, archiver_config_handler, &conf) < 0) {
|
2016-12-10 07:55:47 +00:00
|
|
|
fprintf(stderr, "Unable to load configuration ini %s\n", buffer);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(buffer, 1024, "%s/protocols.ini", conf.config_path);
|
2018-10-08 14:13:11 +00:00
|
|
|
if (ini_parse(buffer, protocol_config_handler, &conf) < 0) {
|
2016-12-10 07:55:47 +00:00
|
|
|
fprintf(stderr, "Unable to load configuration ini %s\n", buffer);
|
|
|
|
exit(-1);
|
2016-12-08 04:08:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-14 09:56:15 +00:00
|
|
|
load_strings();
|
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
if (conf.fork) {
|
2016-08-04 09:41:43 +00:00
|
|
|
if (stat(conf.pid_file, &s) == 0) {
|
|
|
|
fprintf(stderr, "Magicka already running or stale pid file at: %s\n", conf.pid_file);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2016-08-02 01:58:10 +00:00
|
|
|
main_pid = fork();
|
|
|
|
|
|
|
|
if (main_pid < 0) {
|
|
|
|
fprintf(stderr, "Error forking.\n");
|
|
|
|
exit(-1);
|
2018-10-08 14:13:11 +00:00
|
|
|
} else if (main_pid > 0) {
|
2018-02-18 09:52:55 +00:00
|
|
|
if (conf.uid != getuid()) {
|
|
|
|
if (setgid(conf.gid) != 0 || setuid(conf.uid) != 0) {
|
|
|
|
perror("Setuid Error: ");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
fptr = fopen(conf.pid_file, "w");
|
|
|
|
if (!fptr) {
|
|
|
|
fprintf(stderr, "Unable to open pid file for writing.\n");
|
|
|
|
} else {
|
|
|
|
fprintf(fptr, "%d", main_pid);
|
|
|
|
fclose(fptr);
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-08 14:13:11 +00:00
|
|
|
for (i = 1; i <= conf.nodes; i++) {
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/nodeinuse.%d", conf.bbs_path, i);
|
|
|
|
if (stat(buffer, &s) == 0) {
|
|
|
|
unlink(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (conf.ipv6) {
|
|
|
|
ipv6_pid = fork();
|
|
|
|
if (ipv6_pid < 0) {
|
|
|
|
fprintf(stderr, "Error forking.\n");
|
2018-10-08 14:13:11 +00:00
|
|
|
exit(-1);
|
2018-01-18 11:27:10 +00:00
|
|
|
} else if (ipv6_pid > 0) {
|
|
|
|
server(conf.telnet_port, 0);
|
|
|
|
} else {
|
2018-02-18 09:52:55 +00:00
|
|
|
ipv6_pid = -1;
|
2018-01-18 11:27:10 +00:00
|
|
|
server(conf.telnet_port, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server(conf.telnet_port, 0);
|
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-18 11:27:10 +00:00
|
|
|
printf("Magicka BBS Server Starting....\n");
|
|
|
|
|
2018-10-08 14:13:11 +00:00
|
|
|
for (i = 1; i <= conf.nodes; i++) {
|
2018-01-18 11:27:10 +00:00
|
|
|
snprintf(buffer, 1024, "%s/nodeinuse.%d", conf.bbs_path, i);
|
|
|
|
if (stat(buffer, &s) == 0) {
|
|
|
|
printf(" - Removing stale file: nodeinuse.%d\n", i);
|
|
|
|
unlink(buffer);
|
|
|
|
}
|
2018-10-08 14:13:11 +00:00
|
|
|
}
|
2018-01-18 11:27:10 +00:00
|
|
|
if (conf.ipv6) {
|
|
|
|
ipv6_pid = fork();
|
|
|
|
if (ipv6_pid < 0) {
|
|
|
|
fprintf(stderr, "Error forking.\n");
|
2018-10-08 14:13:11 +00:00
|
|
|
exit(-1);
|
2018-01-18 11:27:10 +00:00
|
|
|
} else if (ipv6_pid > 0) {
|
|
|
|
server(conf.telnet_port, 0);
|
|
|
|
} else {
|
2018-02-18 09:52:55 +00:00
|
|
|
ipv6_pid = -1;
|
2018-01-18 11:27:10 +00:00
|
|
|
server(conf.telnet_port, 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server(conf.telnet_port, 0);
|
|
|
|
}
|
2016-08-02 01:58:10 +00:00
|
|
|
}
|
|
|
|
}
|