Debian Codegraph @

modules/protocol/charybdis.c

raw
1 /*
2 * Copyright (c) 2003-2004 E. Will et al.
3 * Copyright (c) 2005-2007 Atheme Development Group
4 * Rights to this code are documented in doc/LICENSE.
5 *
6 * This file contains protocol support for charybdis-based ircd.
7 *
8 */
9
10 #include "atheme.h"
11 #include "uplink.h"
12 #include "pmodule.h"
13 #include "protocol/charybdis.h"
14
15 DECLARE_MODULE_V1("protocol/charybdis", true, _modinit, NULL, PACKAGE_STRING, VENDOR_STRING);
16
17 /* *INDENT-OFF* */
18
19 ircd_t Charybdis = {
20 .ircdname = "Charybdis",
21 .tldprefix = "$$",
22 .uses_uid = true,
23 .uses_rcommand = false,
24 .uses_owner = false,
25 .uses_protect = false,
26 .uses_halfops = false,
27 .uses_p10 = false,
28 .uses_vhost = false,
29 .oper_only_modes = CMODE_EXLIMIT | CMODE_PERM | CMODE_IMMUNE,
30 .owner_mode = 0,
31 .protect_mode = 0,
32 .halfops_mode = 0,
33 .owner_mchar = "+",
34 .protect_mchar = "+",
35 .halfops_mchar = "+",
36 .type = PROTOCOL_CHARYBDIS,
37 .perm_mode = CMODE_PERM,
38 .oimmune_mode = CMODE_IMMUNE,
39 .ban_like_modes = "beIq",
40 .except_mchar = 'e',
41 .invex_mchar = 'I',
42 .flags = IRCD_CIDR_BANS | IRCD_HOLDNICK,
43 };
44
45 struct cmode_ charybdis_mode_list[] = {
46 { 'i', CMODE_INVITE },
47 { 'm', CMODE_MOD },
48 { 'n', CMODE_NOEXT },
49 { 'p', CMODE_PRIV },
50 { 's', CMODE_SEC },
51 { 't', CMODE_TOPIC },
52 { 'r', CMODE_REGONLY},
53 { 'z', CMODE_OPMOD },
54 { 'g', CMODE_FINVITE},
55 { 'L', CMODE_EXLIMIT},
56 { 'P', CMODE_PERM },
57 { 'F', CMODE_FTARGET},
58 { 'Q', CMODE_DISFWD },
59
60 /* following modes are added as extensions */
61 { 'N', CMODE_NPC },
62 { 'S', CMODE_SSLONLY },
63 { 'O', CMODE_OPERONLY },
64 { 'A', CMODE_ADMINONLY },
65 { 'c', CMODE_NOCOLOR },
66 { 'C', CMODE_NOCTCP },
67 { 'T', CMODE_NONOTICE },
68 { 'M', CMODE_IMMUNE },
69
70 { '\0', 0 }
71 };
72
73 static bool check_forward(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
74 static bool check_jointhrottle(const char *, channel_t *, mychan_t *, user_t *, myuser_t *);
75
76 struct extmode charybdis_ignore_mode_list[] = {
77 { 'f', check_forward },
78 { 'j', check_jointhrottle },
79 { '\0', 0 }
80 };
81
82 struct cmode_ charybdis_status_mode_list[] = {
83 { 'o', CSTATUS_OP },
84 { 'v', CSTATUS_VOICE },
85 { '\0', 0 }
86 };
87
88 struct cmode_ charybdis_prefix_mode_list[] = {
89 { '@', CSTATUS_OP },
90 { '+', CSTATUS_VOICE },
91 { '\0', 0 }
92 };
93
94 struct cmode_ charybdis_user_mode_list[] = {
95 { 'p', UF_IMMUNE },
96 { 'a', UF_ADMIN },
97 { 'i', UF_INVIS },
98 { 'o', UF_IRCOP },
99 { 'D', UF_DEAF },
100 { 'S', UF_SERVICE },
101 { '\0', 0 }
102 };
103
104 /* *INDENT-ON* */
105
106 /* ircd allows forwards to existing channels; the target channel must be
107 * +F or the setter must have ops in it */
108 static bool check_forward(const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
109 {
110 channel_t *target_c;
111 mychan_t *target_mc;
112 chanuser_t *target_cu;
113
114 if (!VALID_GLOBAL_CHANNEL_PFX(value) || strlen(value) > 50)
115 return false;
116 if (u == NULL && mu == NULL)
117 return true;
118 target_c = channel_find(value);
119 target_mc = mychan_from(target_c);
120 if (target_c == NULL && target_mc == NULL)
121 return false;
122 if (target_c != NULL && target_c->modes & CMODE_FTARGET)
123 return true;
124 if (target_mc != NULL && target_mc->mlock_on & CMODE_FTARGET)
125 return true;
126 if (u != NULL)
127 {
128 target_cu = chanuser_find(target_c, u);
129 if (target_cu != NULL && target_cu->modes & CSTATUS_OP)
130 return true;
131 if (chanacs_user_flags(target_mc, u) & CA_SET)
132 return true;
133 }
134 else if (mu != NULL)
135 if (chanacs_entity_has_flag(target_mc, entity(mu), CA_SET))
136 return true;
137 return false;
138 }
139
140 static bool check_jointhrottle(const char *value, channel_t *c, mychan_t *mc, user_t *u, myuser_t *mu)
141 {
142 const char *p, *arg2;
143
144 p = value, arg2 = NULL;
145 while (*p != '\0')
146 {
147 if (*p == ':')
148 {
149 if (arg2 != NULL)
150 return false;
151 arg2 = p + 1;
152 }
153 else if (!isdigit((unsigned char)*p))
154 return false;
155 p++;
156 }
157 if (arg2 == NULL)
158 return false;
159 if (p - arg2 > 10 || arg2 - value - 1 > 10 || !atoi(value) || !atoi(arg2))
160 return false;
161 return true;
162 }
163
164 /* this may be slow, but it is not used much */
165 /* returns true if it matches, false if not */
166 /* note that the host part matches differently from a regular ban */
167 static bool extgecos_match(const char *mask, user_t *u)
168 {
169 char hostgbuf[NICKLEN+USERLEN+HOSTLEN+GECOSLEN];
170 char realgbuf[NICKLEN+USERLEN+HOSTLEN+GECOSLEN];
171
172 snprintf(hostgbuf, sizeof hostgbuf, "%s!%s@%s#%s", u->nick, u->user, u->vhost, u->gecos);
173 snprintf(realgbuf, sizeof realgbuf, "%s!%s@%s#%s", u->nick, u->user, u->host, u->gecos);
174 return !match(mask, hostgbuf) || !match(mask, realgbuf);
175 }
176
177 static mowgli_node_t *charybdis_next_matching_ban(channel_t *c, user_t *u, int type, mowgli_node_t *first)
178 {
179 chanban_t *cb;
180 mowgli_node_t *n;
181 char hostbuf[NICKLEN+USERLEN+HOSTLEN];
182 char realbuf[NICKLEN+USERLEN+HOSTLEN];
183 char ipbuf[NICKLEN+USERLEN+HOSTLEN];
184 char strippedmask[NICKLEN+USERLEN+HOSTLEN+CHANNELLEN+2];
185 char *p;
186 bool negate, matched;
187 int exttype;
188 channel_t *target_c;
189
190 snprintf(hostbuf, sizeof hostbuf, "%s!%s@%s", u->nick, u->user, u->vhost);
191 snprintf(realbuf, sizeof realbuf, "%s!%s@%s", u->nick, u->user, u->host);
192 /* will be nick!user@ if ip unknown, doesn't matter */
193 snprintf(ipbuf, sizeof ipbuf, "%s!%s@%s", u->nick, u->user, u->ip);
194
195 MOWGLI_ITER_FOREACH(n, first)
196 {
197 cb = n->data;
198
199 if (cb->type != type)
200 continue;
201
202 /*
203 * strip any banforwards from the mask. (SRV-73)
204 * charybdis itself doesn't support banforward but i don't feel like copying
205 * this stuff into ircd-seven and it is possible that charybdis may support them
206 * one day.
207 * --nenolod
208 */
209 mowgli_strlcpy(strippedmask, cb->mask, sizeof strippedmask);
210 p = strrchr(strippedmask, '$');
211 if (p != NULL && p != strippedmask)
212 *p = 0;
213
214 if ((!match(strippedmask, hostbuf) || !match(strippedmask, realbuf) || !match(strippedmask, ipbuf) || !match_cidr(strippedmask, ipbuf)))
215 return n;
216 if (strippedmask[0] == '$')
217 {
218 p = strippedmask + 1;
219 negate = *p == '~';
220 if (negate)
221 p++;
222 exttype = *p++;
223 if (exttype == '\0')
224 continue;
225 /* check parameter */
226 if (*p++ != ':')
227 p = NULL;
228 switch (exttype)
229 {
230 case 'a':
231 matched = u->myuser != NULL && !(u->myuser->flags & MU_WAITAUTH) && (p == NULL || !match(p, entity(u->myuser)->name));
232 break;
233 case 'c':
234 if (p == NULL)
235 continue;
236 target_c = channel_find(p);
237 if (target_c == NULL || (target_c->modes & (CMODE_PRIV | CMODE_SEC)))
238 continue;
239 matched = chanuser_find(target_c, u) != NULL;
240 break;
241 case 'o':
242 matched = is_ircop(u);
243 break;
244 case 'r':
245 if (p == NULL)
246 continue;
247 matched = !match(p, u->gecos);
248 break;
249 case 's':
250 if (p == NULL)
251 continue;
252 matched = !match(p, u->server->name);
253 break;
254 case 'x':
255 if (p == NULL)
256 continue;
257 matched = extgecos_match(p, u);
258 break;
259 default:
260 continue;
261 }
262 if (negate ^ matched)
263 return n;
264 }
265 }
266 return NULL;
267 }
268
269 static bool charybdis_is_valid_host(const char *host)
270 {
271 const char *p;
272
273 for (p = host; *p != '\0'; p++)
274 if (!((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'Z') ||
275 (*p >= 'a' && *p <= 'z') || *p == '.'
276 || *p == ':' || *p == '-' || *p == '/'))
277 return false;
278 return true;
279 }
280
281 static void charybdis_notice_channel_sts(user_t *from, channel_t *target, const char *text)
282 {
283 sts(":%s NOTICE %s :%s", from ? CLIENT_NAME(from) : ME, target->name, text);
284 }
285
286 static bool charybdis_is_extban(const char *mask)
287 {
288 const char without_param[] = "oza";
289 const char with_param[] = "ajcxr";
290 const size_t mask_len = strlen(mask);
291 unsigned char offset = 0;
292
293 if ((mask_len < 2 || mask[0] != '$'))
294 return NULL;
295
296 if (strchr(mask, ' '))
297 return false;
298
299 if (mask_len > 2 && mask[1] == '~')
300 offset = 1;
301
302 /* e.g. $a and $~a */
303 if ((mask_len == 2 + offset) && strchr(without_param, mask[1 + offset]))
304 return true;
305 /* e.g. $~a:Shutter and $~a:Shutter */
306 else if ((mask_len >= 3 + offset) && mask[2 + offset] == ':' && strchr(with_param, mask[1 + offset]))
307 return true;
308 return false;
309 }
310
311 void _modinit(module_t * m)
312 {
313 MODULE_TRY_REQUEST_DEPENDENCY(m, "protocol/ts6-generic");
314
315 notice_channel_sts = &charybdis_notice_channel_sts;
316
317 next_matching_ban = &charybdis_next_matching_ban;
318 is_valid_host = &charybdis_is_valid_host;
319 is_extban = &charybdis_is_extban;
320
321 mode_list = charybdis_mode_list;
322 ignore_mode_list = charybdis_ignore_mode_list;
323 status_mode_list = charybdis_status_mode_list;
324 prefix_mode_list = charybdis_prefix_mode_list;
325 user_mode_list = charybdis_user_mode_list;
326 ignore_mode_list_size = ARRAY_SIZE(charybdis_ignore_mode_list);
327
328 ircd = &Charybdis;
329
330 m->mflags = MODTYPE_CORE;
331
332 pmodule_loaded = true;
333 }
334
335 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
336 * vim:ts=8
337 * vim:sw=8
338 * vim:noexpandtab
339 */