1 |
/* |
2 |
* Ban.c |
3 |
* Code was grabbed from the netrek source and modified to work with |
4 |
* crossfire. This function checks a file in the lib directory for any |
5 |
* banned players. If it finds one it returns a 1. Wildcards can be used. |
6 |
*/ |
7 |
|
8 |
#include <global.h> |
9 |
#include <sproto.h> |
10 |
#include <sys/ioctl.h> |
11 |
|
12 |
#include <errno.h> |
13 |
#include <stdio.h> |
14 |
#include <sys/file.h> |
15 |
|
16 |
/** |
17 |
* Check if a player and/or host is banned. |
18 |
* |
19 |
* @param login the player name to check; NULL to check only the host name |
20 |
* |
21 |
* @param host the host name to check |
22 |
* |
23 |
* @return 1=player/host is banned; 0=player/host is not banned |
24 |
*/ |
25 |
int |
26 |
checkbanned (const char *login, const char *host) |
27 |
{ |
28 |
FILE *bannedfile; |
29 |
char buf[MAX_BUF]; |
30 |
char log_buf0[160], host_buf[64], line_buf[160]; |
31 |
char *indexpos; |
32 |
int num1; |
33 |
int hits = 0; /* Hits==2 means we're banned */ |
34 |
int loop; |
35 |
|
36 |
/* Inverse ban feature: if a line is prefixed by a ~, then we will |
37 |
* immediately return "check passed" if it matches. This allow to ban a |
38 |
* network, but let a part of it still connect. |
39 |
*/ |
40 |
int inverse_ban = 0; |
41 |
|
42 |
for (loop = 0; loop < 2; loop++) |
43 |
{ /* have to check both ban files now */ |
44 |
|
45 |
/* First time through look for BANFILE */ |
46 |
|
47 |
if (loop == 0) |
48 |
{ |
49 |
sprintf (buf, "%s/%s", settings.confdir, BANFILE); |
50 |
bannedfile = fopen (buf, "r"); |
51 |
if (bannedfile == NULL) |
52 |
{ |
53 |
LOG (llevDebug, "Could not find file Banned file\n"); |
54 |
loop++; |
55 |
} |
56 |
} |
57 |
|
58 |
/* Second time through look for BANISHFILE */ |
59 |
|
60 |
if (loop == 1) |
61 |
{ |
62 |
sprintf (buf, "%s/%s", settings.localdir, BANISHFILE); |
63 |
bannedfile = fopen (buf, "r"); |
64 |
if (bannedfile == NULL) |
65 |
{ |
66 |
LOG (llevDebug, "Could not find file Banish file\n"); |
67 |
return (0); |
68 |
} |
69 |
} |
70 |
|
71 |
/* Do the actual work here checking for banned IPs */ |
72 |
|
73 |
while (fgets (line_buf, 160, bannedfile) != NULL) |
74 |
{ |
75 |
char *log_buf = log_buf0; |
76 |
|
77 |
inverse_ban = 0; |
78 |
hits = 0; |
79 |
|
80 |
/* Split line up */ |
81 |
if (*line_buf == '#' || *line_buf == '\n') |
82 |
continue; |
83 |
|
84 |
indexpos = strrchr (line_buf, '@'); |
85 |
if (indexpos == NULL) |
86 |
{ |
87 |
LOG (llevDebug, "Bad line in banned file\n"); |
88 |
continue; |
89 |
} |
90 |
|
91 |
/* copy login name into log_buf */ |
92 |
num1 = indexpos - line_buf; |
93 |
assign (log_buf, line_buf, num1); |
94 |
|
95 |
/* copy host name into host_buf */ |
96 |
assign (host_buf, indexpos + 1); |
97 |
|
98 |
/* Cut off any extra spaces on the host buffer */ |
99 |
indexpos = host_buf; |
100 |
while (!isspace (*indexpos)) |
101 |
indexpos++; |
102 |
|
103 |
*indexpos = '\0'; |
104 |
|
105 |
if (*log_buf == '~') |
106 |
{ |
107 |
log_buf++; |
108 |
inverse_ban = 1; |
109 |
} |
110 |
|
111 |
/* |
112 |
LOG(llevDebug, "Login: <%s>; host: <%s>\n", login, host); |
113 |
LOG(llevDebug, " Checking Banned <%s> and <%s>.\n", log_buf, host_buf); |
114 |
*/ |
115 |
|
116 |
if (*log_buf == '*') |
117 |
hits = 1; |
118 |
else if (login != NULL && strcmp (login, log_buf) == 0) |
119 |
hits = 1; |
120 |
|
121 |
if (hits == 1) |
122 |
{ |
123 |
if (*host_buf == '*') |
124 |
{ /* Lock out any host */ |
125 |
hits++; |
126 |
|
127 |
/* break out now. otherwise hits will get reset to one */ |
128 |
break; |
129 |
} |
130 |
else if (strstr (host, host_buf) != NULL) |
131 |
{ /* Lock out subdomains (eg, "*@usc.edu" */ |
132 |
hits++; |
133 |
|
134 |
/* break out now. otherwise hits will get reset to one */ |
135 |
break; |
136 |
} |
137 |
else if (strcmp (host, host_buf) == 0) |
138 |
{ /* Lock out specific host */ |
139 |
hits++; |
140 |
|
141 |
/* break out now. otherwise hits will get reset to one */ |
142 |
break; |
143 |
} |
144 |
} |
145 |
} |
146 |
fclose (bannedfile); |
147 |
|
148 |
if (hits >= 2) |
149 |
{ |
150 |
return (!inverse_ban); |
151 |
} |
152 |
|
153 |
loop++; |
154 |
} |
155 |
|
156 |
return (0); |
157 |
} |