1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 2003-2015 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file userhost.c |
23 |
* \brief Global user limits. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "list.h" |
28 |
#include "hash.h" |
29 |
#include "client.h" |
30 |
#include "userhost.h" |
31 |
#include "mempool.h" |
32 |
#include "irc_string.h" |
33 |
|
34 |
|
35 |
static mp_pool_t *userhost_pool; |
36 |
static mp_pool_t *namehost_pool; |
37 |
|
38 |
|
39 |
void |
40 |
userhost_init(void) |
41 |
{ |
42 |
userhost_pool = mp_pool_new(sizeof(struct UserHost), MP_CHUNK_SIZE_USERHOST); |
43 |
namehost_pool = mp_pool_new(sizeof(struct NameHost), MP_CHUNK_SIZE_NAMEHOST); |
44 |
} |
45 |
|
46 |
/* userhost_count() |
47 |
* |
48 |
* inputs - user name |
49 |
* - hostname |
50 |
* - int flag 1 if global, 0 if local |
51 |
* - pointer to where global count should go |
52 |
* - pointer to where local count should go |
53 |
* - pointer to where identd count should go (local clients only) |
54 |
* output - none |
55 |
* side effects - |
56 |
*/ |
57 |
void |
58 |
userhost_count(const char *user, const char *host, unsigned int *global_p, |
59 |
unsigned int *local_p, unsigned int *icount_p) |
60 |
{ |
61 |
dlink_node *node = NULL; |
62 |
struct UserHost *found_userhost; |
63 |
|
64 |
if ((found_userhost = hash_find_userhost(host)) == NULL) |
65 |
return; |
66 |
|
67 |
DLINK_FOREACH(node, found_userhost->list.head) |
68 |
{ |
69 |
struct NameHost *nameh = node->data; |
70 |
|
71 |
if (!irccmp(user, nameh->name)) |
72 |
{ |
73 |
if (global_p) |
74 |
*global_p = nameh->gcount; |
75 |
if (local_p) |
76 |
*local_p = nameh->lcount; |
77 |
if (icount_p) |
78 |
*icount_p = nameh->icount; |
79 |
|
80 |
return; |
81 |
} |
82 |
} |
83 |
} |
84 |
|
85 |
/* userhost_find_or_add() |
86 |
* |
87 |
* inputs - host name |
88 |
* output - none |
89 |
* side effects - find UserHost * for given host name |
90 |
*/ |
91 |
static struct UserHost * |
92 |
userhost_find_or_add(const char *host) |
93 |
{ |
94 |
struct UserHost *userhost = NULL; |
95 |
|
96 |
if ((userhost = hash_find_userhost(host))) |
97 |
return userhost; |
98 |
|
99 |
userhost = mp_pool_get(userhost_pool); |
100 |
|
101 |
strlcpy(userhost->host, host, sizeof(userhost->host)); |
102 |
hash_add_userhost(userhost); |
103 |
|
104 |
return userhost; |
105 |
} |
106 |
|
107 |
/* userhost_add() |
108 |
* |
109 |
* inputs - user name |
110 |
* - hostname |
111 |
* - int flag 1 if global, 0 if local |
112 |
* output - none |
113 |
* side effects - add given user@host to hash tables |
114 |
*/ |
115 |
void |
116 |
userhost_add(const char *user, const char *host, int global) |
117 |
{ |
118 |
dlink_node *node = NULL; |
119 |
struct UserHost *found_userhost; |
120 |
struct NameHost *nameh; |
121 |
unsigned int hasident = 1; |
122 |
|
123 |
if (*user == '~') |
124 |
{ |
125 |
hasident = 0; |
126 |
++user; |
127 |
} |
128 |
|
129 |
if ((found_userhost = userhost_find_or_add(host)) == NULL) |
130 |
return; |
131 |
|
132 |
DLINK_FOREACH(node, found_userhost->list.head) |
133 |
{ |
134 |
nameh = node->data; |
135 |
|
136 |
if (!irccmp(user, nameh->name)) |
137 |
{ |
138 |
nameh->gcount++; |
139 |
|
140 |
if (!global) |
141 |
{ |
142 |
if (hasident) |
143 |
nameh->icount++; |
144 |
nameh->lcount++; |
145 |
} |
146 |
|
147 |
return; |
148 |
} |
149 |
} |
150 |
|
151 |
nameh = mp_pool_get(namehost_pool); |
152 |
nameh->gcount = 1; |
153 |
strlcpy(nameh->name, user, sizeof(nameh->name)); |
154 |
|
155 |
if (!global) |
156 |
{ |
157 |
if (hasident) |
158 |
nameh->icount = 1; |
159 |
|
160 |
nameh->lcount = 1; |
161 |
} |
162 |
|
163 |
dlinkAdd(nameh, &nameh->node, &found_userhost->list); |
164 |
} |
165 |
|
166 |
/* userhost_del() |
167 |
* |
168 |
* inputs - user name |
169 |
* - hostname |
170 |
* - int flag 1 if global, 0 if local |
171 |
* output - none |
172 |
* side effects - delete given user@host to hash tables |
173 |
*/ |
174 |
void |
175 |
userhost_del(const char *user, const char *host, int global) |
176 |
{ |
177 |
dlink_node *node = NULL; |
178 |
struct UserHost *found_userhost = NULL; |
179 |
unsigned int hasident = 1; |
180 |
|
181 |
if (*user == '~') |
182 |
{ |
183 |
hasident = 0; |
184 |
++user; |
185 |
} |
186 |
|
187 |
if ((found_userhost = hash_find_userhost(host)) == NULL) |
188 |
return; |
189 |
|
190 |
DLINK_FOREACH(node, found_userhost->list.head) |
191 |
{ |
192 |
struct NameHost *nameh = node->data; |
193 |
|
194 |
if (!irccmp(user, nameh->name)) |
195 |
{ |
196 |
if (nameh->gcount > 0) |
197 |
nameh->gcount--; |
198 |
|
199 |
if (!global) |
200 |
{ |
201 |
if (nameh->lcount > 0) |
202 |
nameh->lcount--; |
203 |
|
204 |
if (hasident && nameh->icount > 0) |
205 |
nameh->icount--; |
206 |
} |
207 |
|
208 |
if (nameh->gcount == 0 && nameh->lcount == 0) |
209 |
{ |
210 |
dlinkDelete(&nameh->node, &found_userhost->list); |
211 |
mp_pool_release(nameh); |
212 |
} |
213 |
|
214 |
if (dlink_list_length(&found_userhost->list) == 0) |
215 |
{ |
216 |
hash_del_userhost(found_userhost); |
217 |
mp_pool_release(found_userhost); |
218 |
} |
219 |
|
220 |
return; |
221 |
} |
222 |
} |
223 |
} |