| 1 |
sysadm |
1.1 |
#include "common/setup_before.h"
|
| 2 |
|
|
#include "setup.h"
|
| 3 |
|
|
|
| 4 |
|
|
#ifdef HAVE_STDDEF_H
|
| 5 |
|
|
# include <stddef.h>
|
| 6 |
|
|
#else
|
| 7 |
|
|
# ifndef NULL
|
| 8 |
|
|
# define NULL ((void *)0)
|
| 9 |
|
|
# endif
|
| 10 |
|
|
#endif
|
| 11 |
|
|
#ifdef STDC_HEADERS
|
| 12 |
|
|
# include <stdlib.h>
|
| 13 |
|
|
#else
|
| 14 |
|
|
# ifdef HAVE_MALLOC_H
|
| 15 |
|
|
# include <malloc.h>
|
| 16 |
|
|
# endif
|
| 17 |
|
|
#endif
|
| 18 |
|
|
#ifdef HAVE_STRING_H
|
| 19 |
|
|
# include <string.h>
|
| 20 |
|
|
#else
|
| 21 |
|
|
# ifdef HAVE_STRINGS_H
|
| 22 |
|
|
# include <strings.h>
|
| 23 |
|
|
# endif
|
| 24 |
|
|
#endif
|
| 25 |
|
|
#include "compat/strsep.h"
|
| 26 |
|
|
#include "compat/char_bit.h"
|
| 27 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 28 |
|
|
# include <sys/types.h> /* needed to include netinet/in.h */
|
| 29 |
|
|
#endif
|
| 30 |
|
|
#ifdef HAVE_SYS_SOCKET_H
|
| 31 |
|
|
# include <sys/socket.h>
|
| 32 |
|
|
#endif
|
| 33 |
|
|
#include "compat/socket.h"
|
| 34 |
|
|
#ifdef HAVE_SYS_PARAM_H
|
| 35 |
|
|
# include <sys/param.h>
|
| 36 |
|
|
#endif
|
| 37 |
|
|
#ifdef HAVE_NETINET_IN_H
|
| 38 |
|
|
# include <netinet/in.h>
|
| 39 |
|
|
#endif
|
| 40 |
|
|
#include "compat/netinet_in.h"
|
| 41 |
|
|
#ifdef HAVE_ARPA_INET_H
|
| 42 |
|
|
# include <arpa/inet.h> /* FIXME: probably not needed... do some systems put types in here or something? */
|
| 43 |
|
|
#endif
|
| 44 |
|
|
#ifdef TIME_WITH_SYS_TIME
|
| 45 |
|
|
# include <sys/time.h>
|
| 46 |
|
|
# include <time.h>
|
| 47 |
|
|
#else
|
| 48 |
|
|
# ifdef HAVE_SYS_TIME_H
|
| 49 |
|
|
# include <sys/time.h>
|
| 50 |
|
|
# else
|
| 51 |
|
|
# include <time.h>
|
| 52 |
|
|
# endif
|
| 53 |
|
|
#endif
|
| 54 |
|
|
#include "compat/psock.h"
|
| 55 |
|
|
#include <regex.h>
|
| 56 |
|
|
|
| 57 |
|
|
#include "d2gs.h"
|
| 58 |
|
|
#include "game.h"
|
| 59 |
|
|
#include "net.h"
|
| 60 |
|
|
#include "bit.h"
|
| 61 |
|
|
#include "prefs.h"
|
| 62 |
|
|
#include "gs_scope.h"
|
| 63 |
|
|
#include "connection.h"
|
| 64 |
|
|
#include "common/introtate.h"
|
| 65 |
|
|
#include "common/addr.h"
|
| 66 |
|
|
#include "common/list.h"
|
| 67 |
|
|
#include "common/eventlog.h"
|
| 68 |
|
|
#include "common/xalloc.h"
|
| 69 |
|
|
#include "common/setup_after.h"
|
| 70 |
|
|
|
| 71 |
|
|
/* Added by Leaflet at 2006-06-05 Begin */
|
| 72 |
|
|
// TODO: Rewriting these code is requied
|
| 73 |
|
|
// All these configuration should be put into a seperate configuration file
|
| 74 |
|
|
|
| 75 |
|
|
struct serve_scope_addr {
|
| 76 |
|
|
regex_t *preg;
|
| 77 |
|
|
unsigned short deny;
|
| 78 |
|
|
struct serve_scope_addr *next;
|
| 79 |
|
|
};
|
| 80 |
|
|
|
| 81 |
|
|
struct serve_scope_addr_list {
|
| 82 |
|
|
unsigned int ip;
|
| 83 |
|
|
struct serve_scope_addr *head;
|
| 84 |
|
|
struct serve_scope_addr_list *next;
|
| 85 |
|
|
};
|
| 86 |
|
|
|
| 87 |
|
|
static struct serve_scope_addr_list *serve_scope_list = NULL;
|
| 88 |
|
|
|
| 89 |
|
|
extern int serve_scope_list_load(const char *filename)
|
| 90 |
|
|
{
|
| 91 |
|
|
FILE *fp;
|
| 92 |
|
|
char temp[256], buffer[256];
|
| 93 |
|
|
int section_begin = 0;
|
| 94 |
|
|
struct serve_scope_addr_list *current_list, *new_list;
|
| 95 |
|
|
struct serve_scope_addr *current_addr, *new_addr;
|
| 96 |
|
|
|
| 97 |
|
|
unsigned int ip;
|
| 98 |
|
|
|
| 99 |
|
|
if ((fp=fopen(filename, "r"))== NULL)
|
| 100 |
|
|
{
|
| 101 |
|
|
eventlog(eventlog_level_error, __FUNCTION__, "unable to read file %s", filename);
|
| 102 |
|
|
return -1;
|
| 103 |
|
|
}
|
| 104 |
|
|
|
| 105 |
|
|
while(fgets(buffer, 255, fp))
|
| 106 |
|
|
{
|
| 107 |
|
|
switch(buffer[0])
|
| 108 |
|
|
{
|
| 109 |
|
|
case '!': // Begin of new server section
|
| 110 |
|
|
new_list = (struct serve_scope_addr_list *)malloc(sizeof(struct serve_scope_addr_list));
|
| 111 |
|
|
if (sscanf(buffer, "%*c %s", temp) == EOF)
|
| 112 |
|
|
continue;
|
| 113 |
|
|
if ((ip=net_inet_addr(temp))==~0U) {
|
| 114 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"got bad ip address %s",temp);
|
| 115 |
|
|
serve_scope_list_unload();
|
| 116 |
|
|
return -2;
|
| 117 |
|
|
}
|
| 118 |
|
|
|
| 119 |
|
|
if (!serve_scope_list)
|
| 120 |
|
|
serve_scope_list = new_list;
|
| 121 |
|
|
else
|
| 122 |
|
|
current_list->next = new_list;
|
| 123 |
|
|
current_list = new_list;
|
| 124 |
|
|
current_list->head = NULL;
|
| 125 |
|
|
current_list->next = NULL;
|
| 126 |
|
|
|
| 127 |
|
|
current_list->ip = ntohl(ip);
|
| 128 |
|
|
break;
|
| 129 |
|
|
case '@':
|
| 130 |
|
|
case '~':
|
| 131 |
|
|
new_addr = (struct serve_scope_addr *)malloc(sizeof(struct serve_scope_addr));
|
| 132 |
|
|
if (sscanf(buffer, "%*c %s", temp) == EOF)
|
| 133 |
|
|
continue;
|
| 134 |
|
|
|
| 135 |
|
|
if (!current_list->head)
|
| 136 |
|
|
current_list->head = new_addr;
|
| 137 |
|
|
else
|
| 138 |
|
|
current_addr->next = new_addr;
|
| 139 |
|
|
current_addr = new_addr;
|
| 140 |
|
|
current_addr->next = NULL;
|
| 141 |
|
|
|
| 142 |
|
|
current_addr->preg = (regex_t *) malloc (sizeof (regex_t));
|
| 143 |
|
|
if (regcomp (current_addr->preg, temp, REG_EXTENDED|REG_NOSUB) != 0)
|
| 144 |
|
|
{
|
| 145 |
|
|
eventlog(eventlog_level_error,__FUNCTION__,"Compile regular expression pattern failed %s",temp);
|
| 146 |
|
|
free (current_addr->preg);
|
| 147 |
|
|
serve_scope_list_unload();
|
| 148 |
|
|
return -3;
|
| 149 |
|
|
}
|
| 150 |
|
|
current_addr->deny = (buffer[0]=='~');
|
| 151 |
|
|
|
| 152 |
|
|
break;
|
| 153 |
|
|
}
|
| 154 |
|
|
}
|
| 155 |
|
|
|
| 156 |
|
|
return 0;
|
| 157 |
|
|
}
|
| 158 |
|
|
|
| 159 |
|
|
extern void serve_scope_list_unload(void)
|
| 160 |
|
|
{
|
| 161 |
|
|
struct serve_scope_addr_list *current_list;
|
| 162 |
|
|
struct serve_scope_addr *current_addr;
|
| 163 |
|
|
|
| 164 |
|
|
while(serve_scope_list)
|
| 165 |
|
|
{
|
| 166 |
|
|
current_list = serve_scope_list;
|
| 167 |
|
|
serve_scope_list = serve_scope_list->next;
|
| 168 |
|
|
while(current_list->head)
|
| 169 |
|
|
{
|
| 170 |
|
|
current_addr = current_list->head;
|
| 171 |
|
|
current_list->head = current_list->head->next;
|
| 172 |
|
|
regfree(current_addr->preg);
|
| 173 |
|
|
free(current_addr->preg);
|
| 174 |
|
|
free(current_addr);
|
| 175 |
|
|
}
|
| 176 |
|
|
free(current_list);
|
| 177 |
|
|
}
|
| 178 |
|
|
}
|
| 179 |
|
|
|
| 180 |
|
|
extern int serve_scope_list_reload(const char *filename)
|
| 181 |
|
|
{
|
| 182 |
|
|
serve_scope_list_unload();
|
| 183 |
|
|
return serve_scope_list_load(filename);
|
| 184 |
|
|
}
|
| 185 |
|
|
|
| 186 |
|
|
extern int serve_scope_list_validate(unsigned int ip, t_connection *c)
|
| 187 |
|
|
{
|
| 188 |
|
|
struct serve_scope_addr_list *current_list;
|
| 189 |
|
|
struct serve_scope_addr *current_addr;
|
| 190 |
|
|
regmatch_t pmatch[10];
|
| 191 |
|
|
|
| 192 |
|
|
struct sockaddr_in tsa;
|
| 193 |
|
|
|
| 194 |
|
|
memset(&tsa,0,sizeof(tsa));
|
| 195 |
|
|
tsa.sin_family = PSOCK_AF_INET;
|
| 196 |
|
|
tsa.sin_port = htons((unsigned short)0);
|
| 197 |
|
|
tsa.sin_addr.s_addr = htonl(c->addr);
|
| 198 |
|
|
|
| 199 |
|
|
current_list = serve_scope_list;
|
| 200 |
|
|
while(current_list)
|
| 201 |
|
|
{
|
| 202 |
|
|
if (current_list->ip == ip)
|
| 203 |
|
|
{
|
| 204 |
|
|
current_addr = current_list->head;
|
| 205 |
|
|
while(current_addr)
|
| 206 |
|
|
{
|
| 207 |
|
|
if (!regexec (current_addr->preg, inet_ntoa(tsa.sin_addr), 0, pmatch, 0))
|
| 208 |
|
|
return current_addr->deny; // Match
|
| 209 |
|
|
current_addr = current_addr->next;
|
| 210 |
|
|
}
|
| 211 |
|
|
return 1; // No match serve scope
|
| 212 |
|
|
}
|
| 213 |
|
|
current_list = current_list->next;
|
| 214 |
|
|
}
|
| 215 |
|
|
|
| 216 |
|
|
return 0; // No match game server
|
| 217 |
|
|
}
|
| 218 |
|
|
|
| 219 |
|
|
/* End */
|