]>
Commit | Line | Data |
---|---|---|
543c86ce GB |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <stdarg.h> | |
4 | #include <unistd.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netinet/in.h> | |
8 | ||
9 | #ifndef __USE_MISC | |
10 | #define __USE_MISC /* inet_aton */ | |
11 | #endif | |
12 | ||
13 | #include <arpa/inet.h> | |
14 | #include <sys/un.h> | |
15 | #include <sys/poll.h> | |
16 | #include <errno.h> | |
17 | #include <fcntl.h> | |
18 | ||
c4610da5 | 19 | #include "../lib/version.h" |
543c86ce GB |
20 | #include "../lib/hmac.h" |
21 | #include "../core/events.h" | |
22 | #include "../core/schedule.h" | |
23 | #include "../core/nsmalloc.h" | |
cfe0e8e4 | 24 | #include "../core/hooks.h" |
1961b117 | 25 | #include "../core/config.h" |
cfe0e8e4 | 26 | #include "../control/control.h" |
148db7b8 | 27 | #include "../lib/irc_string.h" |
1961b117 | 28 | #include "../irc/irc.h" |
a473a1be | 29 | #include "../glines/glines.h" |
0444dc16 | 30 | #include "../patricianick/patricianick.h" |
cfe0e8e4 CP |
31 | #include "trusts.h" |
32 | ||
c4610da5 GB |
33 | MODULE_VERSION(""); |
34 | ||
b3a88c21 | 35 | static int countext, enforcepolicy_irc, enforcepolicy_auth; |
2129448c | 36 | |
543c86ce GB |
37 | #define TRUSTBUFSIZE 8192 |
38 | #define TRUSTPASSLEN 128 | |
39 | #define NONCELEN 16 | |
cfe0e8e4 | 40 | |
543c86ce GB |
41 | typedef struct trustsocket { |
42 | int fd; | |
6a7133c8 GB |
43 | int authed; |
44 | char authuser[SERVERLEN+1]; | |
543c86ce GB |
45 | char buf[TRUSTBUFSIZE]; |
46 | unsigned char nonce[NONCELEN]; | |
47 | int size; | |
6a7133c8 | 48 | time_t connected; |
543c86ce | 49 | time_t timeout; |
6a7133c8 GB |
50 | int accepted; |
51 | int rejected; | |
08619b6d | 52 | int unthrottled; |
543c86ce GB |
53 | |
54 | struct trustsocket *next; | |
55 | } trustsocket; | |
9097ab05 | 56 | |
543c86ce | 57 | static trustsocket *tslist; |
6a7133c8 | 58 | static int listenerfd = -1; |
543c86ce GB |
59 | static FILE *urandom; |
60 | ||
61 | typedef struct trustaccount { | |
62 | int used; | |
63 | char server[SERVERLEN+1]; | |
64 | char password[TRUSTPASSLEN+1]; | |
65 | } trustaccount; | |
66 | ||
67 | trustaccount trustaccounts[MAXSERVERS]; | |
68 | ||
0444dc16 GB |
69 | static int checkconnection(const char *username, struct irc_in_addr *ipaddress, int hooknum, int usercountadjustment, char *message, size_t messagelen, int *unthrottle) { |
70 | trusthost *th; | |
543c86ce | 71 | trustgroup *tg; |
0444dc16 GB |
72 | struct irc_in_addr ipaddress_canonical; |
73 | ||
74 | ip_canonicalize_tunnel(&ipaddress_canonical, ipaddress); | |
75 | ||
76 | th = th_getbyhost(&ipaddress_canonical); | |
543c86ce | 77 | |
4b40d278 GB |
78 | if (unthrottle) |
79 | *unthrottle = 0; | |
80 | ||
543c86ce GB |
81 | if(messagelen>0) |
82 | message[0] = '\0'; | |
83 | ||
0444dc16 | 84 | if(!th || !trustsdbloaded || irc_in_addr_is_loopback(ipaddress)) |
543c86ce | 85 | return POLICY_SUCCESS; |
cebc4cab | 86 | |
6e6e98da GB |
87 | tg = th->group; |
88 | ||
2129448c CP |
89 | /* |
90 | * the purpose of this logic is to avoid spam like this: | |
91 | * WARNING: tgX exceeded limit: 11 connected vs 10 max | |
92 | * (goes back down to 10) | |
93 | * WARNING: tgX exceeded limit: 11 connected vs 10 max | |
94 | */ | |
95 | ||
96 | if(hooknum == HOOK_TRUSTS_NEWNICK) { | |
0444dc16 GB |
97 | patricia_node_t *head, *node; |
98 | int i, nodecount = 0; | |
99 | patricianick_t *pnp; | |
100 | nick *npp; | |
101 | ||
102 | head = refnode(iptree, &ipaddress_canonical, th->nodebits); | |
103 | nodecount = head->usercount; | |
104 | ||
105 | /* Account for borrowed IP addresses. */ | |
106 | PATRICIA_WALK(head, node) { | |
107 | pnp = node->exts[pnode_ext]; | |
108 | ||
109 | if (pnp) | |
110 | for (i = 0; i < PATRICIANICK_HASHSIZE; i++) | |
111 | for (npp = pnp->identhash[i]; npp; npp=npp->exts[pnick_ext]) | |
112 | if (NickOnServiceServer(npp)) | |
113 | usercountadjustment--; | |
114 | } | |
115 | PATRICIA_WALK_END; | |
afe99911 | 116 | |
0444dc16 | 117 | derefnode(iptree, head); |
afe99911 | 118 | |
6ca057fd | 119 | if(th->maxpernode && nodecount + usercountadjustment > th->maxpernode) { |
0444dc16 GB |
120 | controlwall(NO_OPER, NL_CLONING, "Hard connection limit exceeded on subnet: %s (group: %s): %d connected, %d max.", CIDRtostr(*ipaddress, th->nodebits), tg->name->content, nodecount + usercountadjustment, th->maxpernode); |
121 | snprintf(message, messagelen, "Too many connections from your host (%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ipaddress)); | |
543c86ce | 122 | return POLICY_FAILURE_NODECOUNT; |
afe99911 GB |
123 | } |
124 | ||
6ca057fd | 125 | if(tg->trustedfor && tg->count + usercountadjustment > tg->trustedfor) { |
2129448c CP |
126 | if(tg->count > (long)tg->exts[countext]) { |
127 | tg->exts[countext] = (void *)(long)tg->count; | |
afe99911 | 128 | |
639d4165 | 129 | controlwall(NO_OPER, NL_CLONING, "Hard connection limit exceeded (group %s): %d connected, %d max.", tg->name->content, tg->count + usercountadjustment, tg->trustedfor); |
0444dc16 | 130 | snprintf(message, messagelen, "Too many connections from your trust (%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ipaddress)); |
148db7b8 | 131 | } |
61e5ea0a | 132 | |
0444dc16 | 133 | snprintf(message, messagelen, "Too many connections from your trust (%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ipaddress)); |
61e5ea0a | 134 | return POLICY_FAILURE_GROUPCOUNT; |
148db7b8 | 135 | } |
afe99911 | 136 | |
543c86ce | 137 | if((tg->flags & TRUST_ENFORCE_IDENT) && (username[0] == '~')) { |
0444dc16 GB |
138 | controlwall(NO_OPER, NL_CLONING, "Ident required: %s@%s (group: %s).", username, IPtostr(*ipaddress), tg->name->content); |
139 | snprintf(message, messagelen, "IDENTD required from your host (%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ipaddress)); | |
543c86ce | 140 | return POLICY_FAILURE_IDENTD; |
1961b117 GB |
141 | } |
142 | ||
148db7b8 CP |
143 | if(tg->maxperident > 0) { |
144 | int identcount = 0; | |
145 | trusthost *th2; | |
146 | nick *tnp; | |
147 | ||
148 | for(th2=tg->hosts;th2;th2=th2->next) { | |
149 | for(tnp=th2->users;tnp;tnp=nextbytrust(tnp)) { | |
543c86ce | 150 | if(!ircd_strcmp(tnp->ident, username)) |
148db7b8 CP |
151 | identcount++; |
152 | } | |
2129448c | 153 | } |
148db7b8 | 154 | |
6ca057fd | 155 | if(identcount + usercountadjustment > tg->maxperident) { |
0444dc16 GB |
156 | controlwall(NO_OPER, NL_CLONING, "Hard ident limit exceeded: %s@%s (group: %s): %d connected, %d max.", username, IPtostr(*ipaddress), tg->name->content, identcount + usercountadjustment, tg->maxperident); |
157 | snprintf(message, messagelen, "Too many connections from your username (%s@%s) - see https://www.quakenet.org/help/trusts/connection-limit for details.", username, IPtostr(*ipaddress)); | |
543c86ce | 158 | return POLICY_FAILURE_IDENTCOUNT; |
1961b117 | 159 | } |
2129448c CP |
160 | } |
161 | } else { | |
162 | if(tg->count < tg->maxusage) | |
163 | tg->exts[countext] = (void *)(long)tg->count; | |
164 | } | |
543c86ce | 165 | |
61e5ea0a GB |
166 | if(tg->trustedfor > 0) |
167 | snprintf(message, messagelen, "Trust has %d out of %d allowed connections.", tg->count + usercountadjustment, tg->trustedfor); | |
168 | ||
4b40d278 | 169 | if(unthrottle && (tg->flags & TRUST_UNTHROTTLE)) |
08619b6d | 170 | *unthrottle = 1; |
543c86ce | 171 | |
4b40d278 | 172 | return POLICY_SUCCESS; |
543c86ce GB |
173 | } |
174 | ||
175 | static int trustdowrite(trustsocket *sock, char *format, ...) { | |
176 | char buf[1024]; | |
177 | va_list va; | |
178 | int r; | |
179 | ||
180 | va_start(va, format); | |
181 | r = vsnprintf(buf, sizeof(buf), format, va); | |
182 | va_end(va); | |
183 | ||
184 | if(r >= sizeof(buf)) | |
185 | r = sizeof(buf) - 1; | |
186 | ||
187 | buf[r] = '\n'; | |
188 | ||
189 | if(write(sock->fd, buf, r + 1) != r + 1) | |
190 | return 0; | |
191 | return 1; | |
192 | } | |
193 | ||
194 | static int policycheck_auth(trustsocket *sock, const char *sequence_id, const char *username, const char *host) { | |
195 | char message[512]; | |
4b40d278 | 196 | int verdict, unthrottle; |
0444dc16 | 197 | struct irc_in_addr ipaddress; |
543c86ce | 198 | unsigned char bits; |
08619b6d | 199 | |
0444dc16 | 200 | if(!ipmask_parse(host, &ipaddress, &bits)) { |
6a7133c8 | 201 | sock->accepted++; |
543c86ce | 202 | return trustdowrite(sock, "PASS %s", sequence_id); |
6a7133c8 | 203 | } |
4b40d278 | 204 | |
0444dc16 | 205 | verdict = checkconnection(username, &ipaddress, HOOK_TRUSTS_NEWNICK, 1, message, sizeof(message), &unthrottle); |
543c86ce | 206 | |
b3a88c21 GB |
207 | if(!enforcepolicy_auth) |
208 | verdict = POLICY_SUCCESS; | |
209 | ||
543c86ce | 210 | if (verdict == POLICY_SUCCESS) { |
6a7133c8 GB |
211 | sock->accepted++; |
212 | ||
4b40d278 | 213 | if (unthrottle) { |
08619b6d TS |
214 | sock->unthrottled++; |
215 | trustdowrite(sock, "UNTHROTTLE %s", sequence_id); | |
4b40d278 GB |
216 | } |
217 | ||
543c86ce GB |
218 | if(message[0]) |
219 | return trustdowrite(sock, "PASS %s %s", sequence_id, message); | |
220 | else | |
221 | return trustdowrite(sock, "PASS %s", sequence_id); | |
222 | } else { | |
6a7133c8 GB |
223 | sock->rejected++; |
224 | ||
639d4165 | 225 | controlwall(NO_OPER, NL_CLONING, "Rejected connection from %s@%s using IAuth: %s", username, host, message); |
543c86ce GB |
226 | return trustdowrite(sock, "KILL %s %s", sequence_id, message); |
227 | } | |
228 | } | |
229 | ||
230 | static int trustkillconnection(trustsocket *sock, char *reason) { | |
231 | trustdowrite(sock, "QUIT %s", reason); | |
232 | return 0; | |
233 | } | |
234 | ||
0bd28986 | 235 | static void trustfreeconnection(trustsocket *sock, int unlink) { |
84978a27 | 236 | trustsocket **pnext, *ts; |
0bd28986 GB |
237 | |
238 | if(!unlink) { | |
39a628b3 GB |
239 | controlwall(NO_OPER, NL_TRUSTS, "Lost connection on policy socket for '%s'.", sock->authed?sock->authuser:"<unauthenticated connection>"); |
240 | ||
0bd28986 GB |
241 | deregisterhandler(sock->fd, 1); |
242 | nsfree(POOL_TRUSTS, sock); | |
243 | return; | |
244 | } | |
245 | ||
cb2e40cf GB |
246 | for(pnext=&tslist;*pnext;pnext=&((*pnext)->next)) { |
247 | ts=*pnext; | |
543c86ce | 248 | if(ts == sock) { |
543c86ce | 249 | *pnext = sock->next; |
0bd28986 | 250 | trustfreeconnection(sock, 0); |
543c86ce GB |
251 | break; |
252 | } | |
543c86ce GB |
253 | } |
254 | } | |
255 | ||
256 | static int handletrustauth(trustsocket *sock, char *server_name, char *mac) { | |
257 | int i; | |
258 | char *password = NULL; | |
259 | unsigned char digest[16]; | |
260 | char noncehexbuf[NONCELEN * 2 + 1]; | |
261 | char hexbuf[sizeof(digest) * 2 + 1]; | |
cb2e40cf | 262 | trustsocket *ts, **pnext; |
543c86ce GB |
263 | |
264 | for(i=0;i<MAXSERVERS;i++) { | |
265 | if(trustaccounts[i].used && strcmp(trustaccounts[i].server, server_name) == 0) { | |
266 | password = trustaccounts[i].password; | |
267 | break; | |
268 | } | |
269 | } | |
270 | ||
427f6077 GB |
271 | if (!password) { |
272 | controlwall(NO_OPER, NL_TRUSTS, "Invalid servername for policy socket: '%s'", server_name); | |
543c86ce | 273 | return trustkillconnection(sock, "Invalid servername."); |
427f6077 | 274 | } |
543c86ce GB |
275 | |
276 | hmacmd5 h; | |
277 | hmacmd5_init(&h, (unsigned char *)password, strlen(password)); | |
278 | hmacmd5_update(&h, (unsigned char *)hmac_printhex(sock->nonce, noncehexbuf, NONCELEN), NONCELEN * 2); | |
279 | hmacmd5_final(&h, digest); | |
427f6077 GB |
280 | if(hmac_strcmp(mac, hmac_printhex(digest, hexbuf, sizeof(digest)))) { |
281 | controlwall(NO_OPER, NL_TRUSTS, "Invalid password for policy socket with servername '%s'.", server_name); | |
543c86ce | 282 | return trustkillconnection(sock, "Bad MAC."); |
427f6077 | 283 | } |
543c86ce | 284 | |
cb2e40cf GB |
285 | for(pnext=&tslist;*pnext;pnext=&((*pnext)->next)) { |
286 | ts = *pnext; | |
287 | if(ts->authed && strcmp(ts->authuser, server_name) == 0) { | |
288 | trustkillconnection(ts, "New connection with same server name."); | |
289 | *pnext = ts->next; | |
290 | trustfreeconnection(ts, 0); | |
291 | break; | |
292 | } | |
293 | } | |
294 | ||
6a7133c8 GB |
295 | sock->authed = 1; |
296 | strncpy(sock->authuser, server_name, SERVERLEN); | |
427f6077 GB |
297 | |
298 | controlwall(NO_OPER, NL_TRUSTS, "Successful authentication for policy socket with servername '%s'.", server_name); | |
543c86ce GB |
299 | return trustdowrite(sock, "AUTHOK"); |
300 | } | |
301 | ||
302 | #define MAXTOKENS 10 | |
303 | static int handletrustline(trustsocket *sock, char *line) { | |
304 | char *command, *p, *lastpos; | |
305 | char *tokens[MAXTOKENS]; | |
306 | int tokensfound = -1; | |
307 | ||
308 | for(command=lastpos=p=line;*p;p++) { | |
309 | if(*p == ' ') { | |
310 | *p = '\0'; | |
311 | if(tokensfound == MAXTOKENS) | |
312 | return trustkillconnection(sock, "too many tokens"); | |
313 | ||
314 | if(tokensfound >= 0) { | |
315 | tokens[tokensfound++] = lastpos; | |
316 | } else { | |
317 | tokensfound++; | |
318 | } | |
319 | lastpos = p + 1; | |
320 | } | |
321 | } | |
322 | if(lastpos != p) { | |
323 | if(tokensfound == MAXTOKENS) | |
324 | return trustkillconnection(sock, "too many tokens"); | |
325 | tokens[tokensfound++] = lastpos; | |
326 | } | |
327 | ||
6a7133c8 | 328 | if(!sock->authed && !strcmp("AUTH", command)) { |
543c86ce GB |
329 | if(tokensfound != 2) |
330 | return trustkillconnection(sock, "incorrect arg count for command."); | |
331 | ||
332 | return handletrustauth(sock, tokens[0], tokens[1]); | |
6a7133c8 | 333 | } else if(sock->authed && !strcmp("CHECK", command)) { |
543c86ce GB |
334 | if(tokensfound != 3) |
335 | return trustkillconnection(sock, "incorrect arg count for command."); | |
336 | ||
337 | policycheck_auth(sock, tokens[0], tokens[1], tokens[2]); | |
338 | return 1; | |
61b0c595 GB |
339 | } else if(!strcmp("VERSION", command)) { |
340 | /* Ignore this command for now. */ | |
341 | return 1; | |
543c86ce GB |
342 | } else { |
343 | Error("trusts_policy", ERR_WARNING, "Bad command: %s", command); | |
344 | return 0; | |
345 | } | |
346 | } | |
347 | ||
348 | static trustsocket *findtrustsocketbyfd(int fd) { | |
349 | for(trustsocket *ts=tslist;ts;ts=ts->next) | |
350 | if(ts->fd==fd) | |
351 | return ts; | |
352 | ||
353 | return NULL; | |
354 | } | |
355 | ||
356 | static int handletrustclient(trustsocket *sock) { | |
357 | int r, remaining = TRUSTBUFSIZE - sock->size, i; | |
358 | char *lastpos, *c; | |
359 | ||
360 | if(!remaining) { | |
361 | trustkillconnection(sock, "Buffer overflow."); | |
362 | return 0; | |
363 | } | |
364 | ||
365 | r = read(sock->fd, sock->buf + sock->size, remaining); | |
366 | if(r <= 0) | |
367 | return 0; | |
368 | ||
369 | sock->size+=r; | |
370 | lastpos = sock->buf; | |
371 | ||
372 | for(c=sock->buf,i=0;i<sock->size;i++,c++) { | |
373 | if(*c != '\n') | |
374 | continue; | |
375 | *c = '\0'; | |
376 | if(!handletrustline(sock, lastpos)) | |
377 | return 0; | |
378 | ||
379 | lastpos = c + 1; /* is this ok? */ | |
380 | } | |
381 | sock->size-=lastpos - sock->buf; | |
382 | memmove(sock->buf, lastpos, sock->size); | |
383 | ||
384 | return 1; | |
385 | } | |
386 | ||
387 | static void processtrustclient(int fd, short events) { | |
388 | trustsocket *sock = findtrustsocketbyfd(fd); | |
389 | ||
390 | if(!sock) | |
391 | return; | |
8e8e611b GB |
392 | |
393 | if (events & (POLLPRI | POLLERR | POLLHUP | POLLNVAL)) { | |
394 | trustfreeconnection(sock, 1); | |
395 | return; | |
396 | } | |
397 | ||
543c86ce GB |
398 | if(events & POLLIN) |
399 | if(!handletrustclient(sock)) | |
0bd28986 | 400 | trustfreeconnection(sock, 1); |
543c86ce GB |
401 | } |
402 | ||
403 | static void trustdotimeout(void *arg) { | |
404 | time_t t = time(NULL); | |
56c707b6 | 405 | trustsocket **pnext, *sock; |
543c86ce | 406 | |
cb2e40cf GB |
407 | for(pnext=&tslist;*pnext;pnext=&((*pnext)->next)) { |
408 | sock = *pnext; | |
6a7133c8 | 409 | if(!sock->authed && t >= sock->timeout) { |
543c86ce | 410 | trustkillconnection(sock, "Auth timeout."); |
543c86ce | 411 | *pnext = sock->next; |
0bd28986 | 412 | trustfreeconnection(sock, 0); |
543c86ce | 413 | } |
543c86ce GB |
414 | } |
415 | } | |
416 | ||
417 | static void processtrustlistener(int fd, short events) { | |
418 | if(events & POLLIN) { | |
419 | trustsocket *sock; | |
420 | char buf[NONCELEN * 2 + 1]; | |
421 | ||
422 | int newfd = accept(fd, NULL, NULL), flags; | |
423 | if(newfd == -1) | |
424 | return; | |
425 | ||
426 | flags = fcntl(newfd, F_GETFL, 0); | |
427 | if(flags < 0) { | |
428 | Error("trusts_policy", ERR_WARNING, "Unable to set socket non-blocking."); | |
429 | close(newfd); | |
430 | return; | |
431 | } | |
432 | ||
433 | if(fcntl(fd, F_SETFL, flags|O_NONBLOCK) < 0) { | |
434 | Error("trusts_policy", ERR_WARNING, "Unable to set socket non-blocking."); | |
435 | close(newfd); | |
436 | return; | |
437 | } | |
438 | ||
439 | registerhandler(newfd, POLLIN|POLLERR|POLLHUP, processtrustclient); | |
440 | ||
441 | sock = nsmalloc(POOL_TRUSTS, sizeof(trustsocket)); | |
442 | if(!sock) { | |
443 | deregisterhandler(newfd, 1); | |
444 | return; | |
445 | } | |
446 | ||
447 | sock->fd = newfd; | |
448 | sock->next = tslist; | |
449 | tslist = sock; | |
450 | ||
451 | if(fread((char *)sock->nonce, 1, NONCELEN, urandom) != NONCELEN) { | |
452 | Error("trusts_policy", ERR_WARNING, "Error getting random bytes."); | |
453 | deregisterhandler(newfd, 1); | |
454 | tslist = sock->next; | |
455 | nsfree(POOL_TRUSTS, sock); | |
456 | } else { | |
6a7133c8 | 457 | sock->authed = 0; |
543c86ce | 458 | sock->size = 0; |
6a7133c8 | 459 | sock->connected = time(NULL); |
543c86ce | 460 | sock->timeout = time(NULL) + 30; |
6a7133c8 GB |
461 | sock->accepted = 0; |
462 | sock->rejected = 0; | |
08619b6d | 463 | sock->unthrottled = 0; |
543c86ce GB |
464 | if(!trustdowrite(sock, "AUTH %s", hmac_printhex(sock->nonce, buf, NONCELEN))) { |
465 | Error("trusts_policy", ERR_WARNING, "Error writing auth to fd %d.", newfd); | |
466 | deregisterhandler(newfd, 1); | |
467 | tslist = sock->next; | |
468 | nsfree(POOL_TRUSTS, sock); | |
469 | return; | |
470 | } | |
471 | } | |
472 | } | |
473 | } | |
474 | ||
475 | static int createlistenersock(int port) { | |
476 | struct sockaddr_in s; | |
477 | int fd; | |
478 | int optval; | |
479 | ||
480 | memset(&s, 0, sizeof(struct sockaddr_in)); | |
481 | s.sin_family = AF_INET; | |
482 | s.sin_addr.s_addr = INADDR_ANY; | |
483 | s.sin_port = htons(port); | |
484 | ||
485 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
486 | if(fd < 0) { | |
487 | Error("trusts_policy", ERR_WARNING, "Unable to get socket for trustfd."); | |
488 | return -1; | |
489 | } | |
490 | ||
491 | optval = 1; | |
492 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); | |
493 | ||
494 | if(bind(fd, (struct sockaddr *)&s, sizeof(struct sockaddr_in)) < 0) { | |
495 | Error("trusts_policy", ERR_WARNING, "Unable to bind trustfd."); | |
496 | close(fd); | |
497 | return -1; | |
498 | } | |
499 | ||
500 | if(listen(fd, 5) < 0) { | |
501 | Error("trusts_policy", ERR_WARNING, "Unable to listen on trustfd."); | |
502 | close(fd); | |
503 | return -1; | |
504 | } | |
505 | ||
506 | registerhandler(fd, POLLIN, processtrustlistener); | |
507 | ||
508 | return fd; | |
509 | } | |
510 | ||
511 | static void policycheck_irc(int hooknum, void *arg) { | |
512 | void **args = arg; | |
513 | nick *np = args[0]; | |
514 | long moving = (long)args[1]; | |
515 | char message[512]; | |
ceb2a8bc | 516 | int verdict, unthrottle; |
543c86ce GB |
517 | |
518 | if(moving) | |
519 | return; | |
520 | ||
0444dc16 | 521 | verdict = checkconnection(np->ident, &np->ipaddress, hooknum, 0, message, sizeof(message), &unthrottle); |
543c86ce | 522 | |
b3a88c21 GB |
523 | if(!enforcepolicy_irc) |
524 | verdict = POLICY_SUCCESS; | |
3c5c26a8 | 525 | |
543c86ce GB |
526 | switch (verdict) { |
527 | case POLICY_FAILURE_NODECOUNT: | |
3c5c26a8 | 528 | glinebynick(np, POLICY_GLINE_DURATION, message, GLINE_IGNORE_TRUST, "trusts_policy"); |
543c86ce GB |
529 | break; |
530 | case POLICY_FAILURE_IDENTD: | |
fdcb5d66 | 531 | glinebyip("~*", &np->ipaddress, 128, POLICY_GLINE_DURATION, message, GLINE_ALWAYS_USER|GLINE_IGNORE_TRUST, "trusts_policy"); |
543c86ce GB |
532 | break; |
533 | case POLICY_FAILURE_IDENTCOUNT: | |
3c5c26a8 | 534 | glinebynick(np, POLICY_GLINE_DURATION, message, GLINE_ALWAYS_USER|GLINE_IGNORE_TRUST, "trusts_policy"); |
543c86ce GB |
535 | break; |
536 | } | |
ceb2a8bc | 537 | |
cfe0e8e4 CP |
538 | } |
539 | ||
b3a88c21 | 540 | static int trusts_cmdtrustpolicyirc(void *source, int cargc, char **cargv) { |
24954f63 GB |
541 | nick *sender = source; |
542 | ||
543 | if(cargc < 1) { | |
b3a88c21 | 544 | controlreply(sender, "Use of glines for trust policy enforcement is currently %s.", enforcepolicy_irc?"enabled":"disabled"); |
24954f63 GB |
545 | return CMD_OK; |
546 | } | |
547 | ||
b3a88c21 GB |
548 | enforcepolicy_irc = atoi(cargv[0]); |
549 | controlwall(NO_OPER, NL_TRUSTS, "%s %s use of glines for trust policy enforcement.", controlid(sender), enforcepolicy_irc?"enabled":"disabled"); | |
550 | controlreply(sender, "Use of glines for trust policy enforcement is now %s.", enforcepolicy_irc?"enabled":"disabled"); | |
24954f63 GB |
551 | |
552 | return CMD_OK; | |
553 | } | |
554 | ||
b3a88c21 GB |
555 | static int trusts_cmdtrustpolicyauth(void *source, int cargc, char **cargv) { |
556 | nick *sender = source; | |
557 | ||
558 | if(cargc < 1) { | |
559 | controlreply(sender, "Trust policy enforcement with IAuth is currently %s.", enforcepolicy_auth?"enabled":"disabled"); | |
560 | return CMD_OK; | |
561 | } | |
562 | ||
563 | enforcepolicy_auth = atoi(cargv[0]); | |
564 | controlwall(NO_OPER, NL_TRUSTS, "%s %s trust policy enforcement with IAuth.", controlid(sender), enforcepolicy_auth?"enabled":"disabled"); | |
565 | controlreply(sender, "Trust policy enforcement with IAuth is now %s.", enforcepolicy_auth?"enabled":"disabled"); | |
566 | ||
567 | return CMD_OK; | |
568 | } | |
569 | ||
570 | ||
6a7133c8 GB |
571 | static int trusts_cmdtrustsockets(void *source, int cargc, char **cargv) { |
572 | nick *sender = source; | |
573 | time_t now; | |
574 | trustsocket *sock; | |
575 | ||
576 | time(&now); | |
577 | ||
08619b6d | 578 | controlreply(sender, "Server Connected for Accepted Rejected Unthrottled"); |
6a7133c8 GB |
579 | |
580 | for(sock=tslist;sock;sock=sock->next) | |
08619b6d | 581 | controlreply(sender, "%-35s %-20s %-15d %-15d %-15d", sock->authed?sock->authuser:"<unauthenticated connection>", longtoduration(now - sock->connected, 0), sock->accepted, sock->rejected, sock->unthrottled); |
6a7133c8 GB |
582 | |
583 | controlreply(sender, "-- End of list."); | |
584 | return CMD_OK; | |
585 | } | |
586 | ||
427f6077 | 587 | void loadtrustaccounts(void) { |
543c86ce | 588 | array *accts; |
3affe49a | 589 | |
427f6077 | 590 | memset(trustaccounts, 0, sizeof(trustaccounts)); |
543c86ce GB |
591 | |
592 | accts = getconfigitems("trusts_policy", "server"); | |
593 | if(!accts) { | |
594 | Error("trusts_policy", ERR_INFO, "No servers added."); | |
595 | } else { | |
596 | sstring **servers = (sstring **)(accts->content); | |
597 | int i; | |
598 | for(i=0;i<accts->cursi;i++) { | |
599 | char server[512]; | |
600 | char *pos; | |
601 | ||
602 | if(i>=MAXSERVERS) { | |
603 | Error("trusts_policy", ERR_INFO, "Too many servers specified."); | |
604 | break; | |
605 | } | |
427f6077 | 606 | |
543c86ce | 607 | strncpy(server, servers[i]->content, sizeof(server)); |
427f6077 | 608 | |
543c86ce | 609 | pos = strchr(server, ','); |
427f6077 | 610 | |
543c86ce GB |
611 | if(!pos) { |
612 | Error("trusts_policy", ERR_INFO, "Server line is missing password: %s", server); | |
613 | continue; | |
614 | } | |
427f6077 | 615 | |
543c86ce | 616 | *pos = '\0'; |
427f6077 | 617 | |
543c86ce GB |
618 | trustaccounts[i].used = 1; |
619 | strncpy(trustaccounts[i].server, server, SERVERLEN); | |
620 | strncpy(trustaccounts[i].password, pos+1, TRUSTPASSLEN); | |
621 | } | |
622 | } | |
427f6077 GB |
623 | } |
624 | ||
625 | static void trustaccounts_rehash(int hooknum, void *arg) { | |
626 | loadtrustaccounts(); | |
627 | } | |
628 | ||
629 | void _init(void) { | |
630 | sstring *m; | |
631 | int trustport; | |
632 | ||
633 | countext = registertgext("count"); | |
634 | if(countext == -1) | |
635 | return; | |
636 | ||
b3a88c21 GB |
637 | m = getconfigitem("trusts_policy", "enforcepolicy_irc"); |
638 | if(m) | |
639 | enforcepolicy_irc = atoi(m->content); | |
640 | ||
641 | m = getconfigitem("trusts_policy", "enforcepolicy_auth"); | |
427f6077 | 642 | if(m) |
b3a88c21 | 643 | enforcepolicy_auth = atoi(m->content); |
427f6077 GB |
644 | |
645 | m = getconfigitem("trusts_policy", "trustport"); | |
646 | if(m) | |
647 | trustport = atoi(m->content); | |
648 | else | |
649 | trustport = DEFAULT_TRUSTPORT; | |
650 | ||
651 | if(trustport) | |
652 | listenerfd = createlistenersock(trustport); | |
653 | ||
654 | loadtrustaccounts(); | |
655 | ||
543c86ce | 656 | registerhook(HOOK_TRUSTS_NEWNICK, policycheck_irc); |
427f6077 GB |
657 | registerhook(HOOK_TRUSTS_LOSTNICK, &policycheck_irc); |
658 | registerhook(HOOK_CORE_REHASH, trustaccounts_rehash); | |
24954f63 | 659 | |
b3a88c21 GB |
660 | registercontrolhelpcmd("trustpolicyirc", NO_DEVELOPER, 1, trusts_cmdtrustpolicyirc, "Usage: trustpolicyirc ?1|0?\nEnables or disables policy enforcement (IRC). Shows current status when no parameter is specified."); |
661 | registercontrolhelpcmd("trustpolicyauth", NO_DEVELOPER, 1, trusts_cmdtrustpolicyauth, "Usage: trustpolicyauth ?1|0?\nEnables or disables policy enforcement (IAuth). Shows current status when no parameter is specified."); | |
6a7133c8 | 662 | registercontrolhelpcmd("trustsockets", NO_DEVELOPER, 0, trusts_cmdtrustsockets, "Usage: trustsockets\nLists all currently active TRUST sockets."); |
543c86ce GB |
663 | |
664 | schedulerecurring(time(NULL)+1, 0, 5, trustdotimeout, NULL); | |
665 | ||
666 | urandom = fopen("/dev/urandom", "rb"); | |
667 | if(!urandom) | |
668 | Error("trusts_policy", ERR_ERROR, "Couldn't open /dev/urandom."); | |
cfe0e8e4 CP |
669 | } |
670 | ||
671 | void _fini(void) { | |
6a7133c8 GB |
672 | trustsocket *sock, *next; |
673 | ||
2129448c CP |
674 | if(countext == -1) |
675 | return; | |
676 | ||
677 | releasetgext(countext); | |
678 | ||
543c86ce GB |
679 | deregisterhook(HOOK_TRUSTS_NEWNICK, policycheck_irc); |
680 | deregisterhook(HOOK_TRUSTS_LOSTNICK, policycheck_irc); | |
427f6077 | 681 | deregisterhook(HOOK_CORE_REHASH, trustaccounts_rehash); |
24954f63 | 682 | |
b3a88c21 GB |
683 | deregistercontrolcmd("trustpolicyirc", trusts_cmdtrustpolicyirc); |
684 | deregistercontrolcmd("trustpolicyauth", trusts_cmdtrustpolicyauth); | |
6a7133c8 | 685 | deregistercontrolcmd("trustsockets", trusts_cmdtrustsockets); |
543c86ce | 686 | |
6a7133c8 GB |
687 | deleteallschedules(trustdotimeout); |
688 | ||
543c86ce GB |
689 | if (urandom) |
690 | fclose(urandom); | |
6a7133c8 GB |
691 | |
692 | if (listenerfd != -1) | |
693 | deregisterhandler(listenerfd, 1); | |
694 | ||
695 | for(sock=tslist;sock;) { | |
696 | next = sock->next; | |
697 | ||
698 | trustkillconnection(sock, "Unloading module."); | |
0bd28986 | 699 | trustfreeconnection(sock, 0); |
6a7133c8 GB |
700 | |
701 | sock = next; | |
702 | } | |
cfe0e8e4 | 703 | } |