6 #include <sys/socket.h>
7 #include <netinet/in.h>
10 #define __USE_MISC /* inet_aton */
13 #include <arpa/inet.h>
19 #include "../lib/version.h"
20 #include "../lib/hmac.h"
21 #include "../core/events.h"
22 #include "../core/schedule.h"
23 #include "../core/nsmalloc.h"
24 #include "../core/hooks.h"
25 #include "../core/config.h"
26 #include "../control/control.h"
27 #include "../lib/irc_string.h"
28 #include "../irc/irc.h"
29 #include "../glines/glines.h"
34 static int countext
, enforcepolicy_irc
, enforcepolicy_auth
;
36 #define TRUSTBUFSIZE 8192
37 #define TRUSTPASSLEN 128
40 typedef struct trustsocket
{
43 char authuser
[SERVERLEN
+1];
44 char buf
[TRUSTBUFSIZE
];
45 unsigned char nonce
[NONCELEN
];
52 struct trustsocket
*next
;
55 static trustsocket
*tslist
;
56 static int listenerfd
= -1;
59 typedef struct trustaccount
{
61 char server
[SERVERLEN
+1];
62 char password
[TRUSTPASSLEN
+1];
65 trustaccount trustaccounts
[MAXSERVERS
];
67 static int checkconnectionth(const char *username
, struct irc_in_addr
*ip
, trusthost
*th
, int hooknum
, int usercountadjustment
, char *message
, size_t messagelen
) {
73 if(!th
|| !trustsdbloaded
)
74 return POLICY_SUCCESS
;
79 * the purpose of this logic is to avoid spam like this:
80 * WARNING: tgX exceeded limit: 11 connected vs 10 max
81 * (goes back down to 10)
82 * WARNING: tgX exceeded limit: 11 connected vs 10 max
85 if(hooknum
== HOOK_TRUSTS_NEWNICK
) {
86 patricia_node_t
*head
, *node
;
89 head
= refnode(iptree
, ip
, th
->nodebits
);
90 PATRICIA_WALK(head
, node
)
92 nodecount
+= node
->usercount
;
95 derefnode(iptree
, head
);
97 if(th
->maxpernode
&& nodecount
+ usercountadjustment
> th
->maxpernode
) {
98 controlwall(NO_OPER
, NL_TRUSTS
, "Hard connection limit exceeded on subnet: %s (group: %s): %d connected, %d max.", trusts_cidr2str(ip
, th
->nodebits
), tg
->name
->content
, nodecount
+ usercountadjustment
, th
->maxpernode
);
99 snprintf(message
, messagelen
, "Too many connections from your host (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
100 return POLICY_FAILURE_NODECOUNT
;
103 if(tg
->trustedfor
&& tg
->count
+ usercountadjustment
> tg
->trustedfor
) {
104 if(tg
->count
> (long)tg
->exts
[countext
]) {
105 tg
->exts
[countext
] = (void *)(long)tg
->count
;
107 controlwall(NO_OPER
, NL_TRUSTS
, "Hard connection limit exceeded (group %s): %d connected, %d max.", tg
->name
->content
, tg
->count
+ usercountadjustment
, tg
->trustedfor
);
108 snprintf(message
, messagelen
, "Too many connections from your trust (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
111 snprintf(message
, messagelen
, "Too many connections from your trust (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
112 return POLICY_FAILURE_GROUPCOUNT
;
115 if((tg
->flags
& TRUST_ENFORCE_IDENT
) && (username
[0] == '~')) {
116 controlwall(NO_OPER
, NL_TRUSTS
, "Ident required: %s@%s (group: %s).", username
, IPtostr(*ip
), tg
->name
->content
);
117 snprintf(message
, messagelen
, "IDENTD required from your host (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
118 return POLICY_FAILURE_IDENTD
;
121 if(tg
->maxperident
> 0) {
126 for(th2
=tg
->hosts
;th2
;th2
=th2
->next
) {
127 for(tnp
=th2
->users
;tnp
;tnp
=nextbytrust(tnp
)) {
128 if(!ircd_strcmp(tnp
->ident
, username
))
133 if(identcount
+ usercountadjustment
> tg
->maxperident
) {
134 controlwall(NO_OPER
, NL_TRUSTS
, "Hard ident limit exceeded: %s@%s (group: %s): %d connected, %d max.", username
, IPtostr(*ip
), tg
->name
->content
, identcount
+ usercountadjustment
, tg
->maxperident
);
135 snprintf(message
, messagelen
, "Too many connections from your username (%s@%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", username
, IPtostr(*ip
));
136 return POLICY_FAILURE_IDENTCOUNT
;
140 if(tg
->count
< tg
->maxusage
)
141 tg
->exts
[countext
] = (void *)(long)tg
->count
;
144 if(tg
->trustedfor
> 0)
145 snprintf(message
, messagelen
, "Trust has %d out of %d allowed connections.", tg
->count
+ usercountadjustment
, tg
->trustedfor
);
147 return POLICY_SUCCESS
;
150 static int checkconnection(const char *username
, struct irc_in_addr
*ip
, int hooknum
, int cloneadjustment
, char *message
, size_t messagelen
) {
151 struct irc_in_addr ip_canonicalized
;
152 ip_canonicalize_tunnel(&ip_canonicalized
, ip
);
154 return checkconnectionth(username
, &ip_canonicalized
, th_getbyhost(&ip_canonicalized
), hooknum
, cloneadjustment
, message
, messagelen
);
157 static int trustdowrite(trustsocket
*sock
, char *format
, ...) {
162 va_start(va
, format
);
163 r
= vsnprintf(buf
, sizeof(buf
), format
, va
);
171 if(write(sock
->fd
, buf
, r
+ 1) != r
+ 1)
176 static int policycheck_auth(trustsocket
*sock
, const char *sequence_id
, const char *username
, const char *host
) {
179 struct irc_in_addr ip
;
182 if(!ipmask_parse(host
, &ip
, &bits
)) {
184 return trustdowrite(sock
, "PASS %s", sequence_id
);
187 verdict
= checkconnection(username
, &ip
, HOOK_TRUSTS_NEWNICK
, 1, message
, sizeof(message
));
189 if(!enforcepolicy_auth
)
190 verdict
= POLICY_SUCCESS
;
192 if (verdict
== POLICY_SUCCESS
) {
196 return trustdowrite(sock
, "PASS %s %s", sequence_id
, message
);
198 return trustdowrite(sock
, "PASS %s", sequence_id
);
202 controlwall(NO_OPER
, NL_TRUSTS
, "Rejected connection from %s@%s using IAuth: %s", username
, host
, message
);
203 return trustdowrite(sock
, "KILL %s %s", sequence_id
, message
);
207 static int trustkillconnection(trustsocket
*sock
, char *reason
) {
208 trustdowrite(sock
, "QUIT %s", reason
);
212 static void trustfreeconnection(trustsocket
*sock
) {
213 trustsocket
**pnext
= &tslist
;
215 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
) {
217 deregisterhandler(sock
->fd
, 1);
219 nsfree(POOL_TRUSTS
, sock
);
223 pnext
= &(sock
->next
);
227 static int handletrustauth(trustsocket
*sock
, char *server_name
, char *mac
) {
229 char *password
= NULL
;
230 unsigned char digest
[16];
231 char noncehexbuf
[NONCELEN
* 2 + 1];
232 char hexbuf
[sizeof(digest
) * 2 + 1];
234 for(i
=0;i
<MAXSERVERS
;i
++) {
235 if(trustaccounts
[i
].used
&& strcmp(trustaccounts
[i
].server
, server_name
) == 0) {
236 password
= trustaccounts
[i
].password
;
242 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid servername for policy socket: '%s'", server_name
);
243 return trustkillconnection(sock
, "Invalid servername.");
247 hmacmd5_init(&h
, (unsigned char *)password
, strlen(password
));
248 hmacmd5_update(&h
, (unsigned char *)hmac_printhex(sock
->nonce
, noncehexbuf
, NONCELEN
), NONCELEN
* 2);
249 hmacmd5_final(&h
, digest
);
250 if(hmac_strcmp(mac
, hmac_printhex(digest
, hexbuf
, sizeof(digest
)))) {
251 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid password for policy socket with servername '%s'.", server_name
);
252 return trustkillconnection(sock
, "Bad MAC.");
256 strncpy(sock
->authuser
, server_name
, SERVERLEN
);
258 controlwall(NO_OPER
, NL_TRUSTS
, "Successful authentication for policy socket with servername '%s'.", server_name
);
259 return trustdowrite(sock
, "AUTHOK");
263 static int handletrustline(trustsocket
*sock
, char *line
) {
264 char *command
, *p
, *lastpos
;
265 char *tokens
[MAXTOKENS
];
266 int tokensfound
= -1;
268 for(command
=lastpos
=p
=line
;*p
;p
++) {
271 if(tokensfound
== MAXTOKENS
)
272 return trustkillconnection(sock
, "too many tokens");
274 if(tokensfound
>= 0) {
275 tokens
[tokensfound
++] = lastpos
;
283 if(tokensfound
== MAXTOKENS
)
284 return trustkillconnection(sock
, "too many tokens");
285 tokens
[tokensfound
++] = lastpos
;
288 if(!sock
->authed
&& !strcmp("AUTH", command
)) {
290 return trustkillconnection(sock
, "incorrect arg count for command.");
292 return handletrustauth(sock
, tokens
[0], tokens
[1]);
293 } else if(sock
->authed
&& !strcmp("CHECK", command
)) {
295 return trustkillconnection(sock
, "incorrect arg count for command.");
297 policycheck_auth(sock
, tokens
[0], tokens
[1], tokens
[2]);
300 Error("trusts_policy", ERR_WARNING
, "Bad command: %s", command
);
305 static trustsocket
*findtrustsocketbyfd(int fd
) {
306 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
)
313 static int handletrustclient(trustsocket
*sock
) {
314 int r
, remaining
= TRUSTBUFSIZE
- sock
->size
, i
;
318 trustkillconnection(sock
, "Buffer overflow.");
322 r
= read(sock
->fd
, sock
->buf
+ sock
->size
, remaining
);
329 for(c
=sock
->buf
,i
=0;i
<sock
->size
;i
++,c
++) {
333 if(!handletrustline(sock
, lastpos
))
336 lastpos
= c
+ 1; /* is this ok? */
338 sock
->size
-=lastpos
- sock
->buf
;
339 memmove(sock
->buf
, lastpos
, sock
->size
);
344 static void processtrustclient(int fd
, short events
) {
345 trustsocket
*sock
= findtrustsocketbyfd(fd
);
351 if(!handletrustclient(sock
))
352 trustfreeconnection(sock
);
355 static void trustdotimeout(void *arg
) {
356 time_t t
= time(NULL
);
357 trustsocket
**pnext
, *next
, *sock
;
361 for(sock
=tslist
;sock
;) {
364 if(!sock
->authed
&& t
>= sock
->timeout
) {
365 trustkillconnection(sock
, "Auth timeout.");
366 deregisterhandler(sock
->fd
, 1);
368 nsfree(POOL_TRUSTS
, sock
);
370 pnext
= &(sock
->next
);
377 static void processtrustlistener(int fd
, short events
) {
378 if(events
& POLLIN
) {
380 char buf
[NONCELEN
* 2 + 1];
382 int newfd
= accept(fd
, NULL
, NULL
), flags
;
386 flags
= fcntl(newfd
, F_GETFL
, 0);
388 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
393 if(fcntl(fd
, F_SETFL
, flags
|O_NONBLOCK
) < 0) {
394 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
399 registerhandler(newfd
, POLLIN
|POLLERR
|POLLHUP
, processtrustclient
);
401 sock
= nsmalloc(POOL_TRUSTS
, sizeof(trustsocket
));
403 deregisterhandler(newfd
, 1);
411 if(fread((char *)sock
->nonce
, 1, NONCELEN
, urandom
) != NONCELEN
) {
412 Error("trusts_policy", ERR_WARNING
, "Error getting random bytes.");
413 deregisterhandler(newfd
, 1);
415 nsfree(POOL_TRUSTS
, sock
);
419 sock
->connected
= time(NULL
);
420 sock
->timeout
= time(NULL
) + 30;
423 if(!trustdowrite(sock
, "AUTH %s", hmac_printhex(sock
->nonce
, buf
, NONCELEN
))) {
424 Error("trusts_policy", ERR_WARNING
, "Error writing auth to fd %d.", newfd
);
425 deregisterhandler(newfd
, 1);
427 nsfree(POOL_TRUSTS
, sock
);
434 static int createlistenersock(int port
) {
435 struct sockaddr_in s
;
439 memset(&s
, 0, sizeof(struct sockaddr_in
));
440 s
.sin_family
= AF_INET
;
441 s
.sin_addr
.s_addr
= INADDR_ANY
;
442 s
.sin_port
= htons(port
);
444 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
446 Error("trusts_policy", ERR_WARNING
, "Unable to get socket for trustfd.");
451 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof(optval
));
453 if(bind(fd
, (struct sockaddr
*)&s
, sizeof(struct sockaddr_in
)) < 0) {
454 Error("trusts_policy", ERR_WARNING
, "Unable to bind trustfd.");
459 if(listen(fd
, 5) < 0) {
460 Error("trusts_policy", ERR_WARNING
, "Unable to listen on trustfd.");
465 registerhandler(fd
, POLLIN
, processtrustlistener
);
470 static void policycheck_irc(int hooknum
, void *arg
) {
473 long moving
= (long)args
[1];
480 verdict
= checkconnectionth(np
->ident
, &np
->p_nodeaddr
, gettrusthost(np
), hooknum
, 0, message
, sizeof(message
));
482 if(!enforcepolicy_irc
)
483 verdict
= POLICY_SUCCESS
;
486 case POLICY_FAILURE_NODECOUNT
:
487 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_IGNORE_TRUST
, "trusts_policy");
489 case POLICY_FAILURE_IDENTD
:
490 glinebyip("~*", &np
->p_ipaddr
, 128, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
492 case POLICY_FAILURE_IDENTCOUNT
:
493 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
498 static int trusts_cmdtrustpolicyirc(void *source
, int cargc
, char **cargv
) {
499 nick
*sender
= source
;
502 controlreply(sender
, "Use of glines for trust policy enforcement is currently %s.", enforcepolicy_irc
?"enabled":"disabled");
506 enforcepolicy_irc
= atoi(cargv
[0]);
507 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s use of glines for trust policy enforcement.", controlid(sender
), enforcepolicy_irc
?"enabled":"disabled");
508 controlreply(sender
, "Use of glines for trust policy enforcement is now %s.", enforcepolicy_irc
?"enabled":"disabled");
513 static int trusts_cmdtrustpolicyauth(void *source
, int cargc
, char **cargv
) {
514 nick
*sender
= source
;
517 controlreply(sender
, "Trust policy enforcement with IAuth is currently %s.", enforcepolicy_auth
?"enabled":"disabled");
521 enforcepolicy_auth
= atoi(cargv
[0]);
522 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s trust policy enforcement with IAuth.", controlid(sender
), enforcepolicy_auth
?"enabled":"disabled");
523 controlreply(sender
, "Trust policy enforcement with IAuth is now %s.", enforcepolicy_auth
?"enabled":"disabled");
529 static int trusts_cmdtrustsockets(void *source
, int cargc
, char **cargv
) {
530 nick
*sender
= source
;
536 controlreply(sender
, "Server Connected for Accepted Rejected");
538 for(sock
=tslist
;sock
;sock
=sock
->next
)
539 controlreply(sender
, "%-20s %20s %10d %15d", sock
->authed
?sock
->authuser
:"<not authenticated>", longtoduration(now
- sock
->connected
, 0), sock
->accepted
, sock
->rejected
);
541 controlreply(sender
, "-- End of list.");
545 void loadtrustaccounts(void) {
548 memset(trustaccounts
, 0, sizeof(trustaccounts
));
550 accts
= getconfigitems("trusts_policy", "server");
552 Error("trusts_policy", ERR_INFO
, "No servers added.");
554 sstring
**servers
= (sstring
**)(accts
->content
);
556 for(i
=0;i
<accts
->cursi
;i
++) {
561 Error("trusts_policy", ERR_INFO
, "Too many servers specified.");
565 strncpy(server
, servers
[i
]->content
, sizeof(server
));
567 pos
= strchr(server
, ',');
570 Error("trusts_policy", ERR_INFO
, "Server line is missing password: %s", server
);
576 trustaccounts
[i
].used
= 1;
577 strncpy(trustaccounts
[i
].server
, server
, SERVERLEN
);
578 strncpy(trustaccounts
[i
].password
, pos
+1, TRUSTPASSLEN
);
583 static void trustaccounts_rehash(int hooknum
, void *arg
) {
591 countext
= registertgext("count");
595 m
= getconfigitem("trusts_policy", "enforcepolicy_irc");
597 enforcepolicy_irc
= atoi(m
->content
);
599 m
= getconfigitem("trusts_policy", "enforcepolicy_auth");
601 enforcepolicy_auth
= atoi(m
->content
);
603 m
= getconfigitem("trusts_policy", "trustport");
605 trustport
= atoi(m
->content
);
607 trustport
= DEFAULT_TRUSTPORT
;
610 listenerfd
= createlistenersock(trustport
);
614 registerhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
615 registerhook(HOOK_TRUSTS_LOSTNICK
, &policycheck_irc
);
616 registerhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
618 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.");
619 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.");
620 registercontrolhelpcmd("trustsockets", NO_DEVELOPER
, 0, trusts_cmdtrustsockets
, "Usage: trustsockets\nLists all currently active TRUST sockets.");
622 schedulerecurring(time(NULL
)+1, 0, 5, trustdotimeout
, NULL
);
624 urandom
= fopen("/dev/urandom", "rb");
626 Error("trusts_policy", ERR_ERROR
, "Couldn't open /dev/urandom.");
630 trustsocket
*sock
, *next
;
635 releasetgext(countext
);
637 deregisterhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
638 deregisterhook(HOOK_TRUSTS_LOSTNICK
, policycheck_irc
);
639 deregisterhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
641 deregistercontrolcmd("trustpolicyirc", trusts_cmdtrustpolicyirc
);
642 deregistercontrolcmd("trustpolicyauth", trusts_cmdtrustpolicyauth
);
643 deregistercontrolcmd("trustsockets", trusts_cmdtrustsockets
);
645 deleteallschedules(trustdotimeout
);
650 if (listenerfd
!= -1)
651 deregisterhandler(listenerfd
, 1);
653 for(sock
=tslist
;sock
;) {
656 trustkillconnection(sock
, "Unloading module.");
657 trustfreeconnection(sock
);