ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/ban.C
Revision: 1.5
Committed: Mon Dec 11 19:46:46 2006 UTC (17 years, 5 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +4 -8 lines
Log Message:
removed #ifn?def WIN32 from all files

File Contents

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