6 #include <sys/socket.h>
7 #include <netinet/in.h>
10 #define __USE_MISC /* inet_aton */
13 #include <arpa/inet.h>
19 #include "../lib/hmac.h"
20 #include "../core/events.h"
21 #include "../core/schedule.h"
22 #include "../core/nsmalloc.h"
23 #include "../core/hooks.h"
24 #include "../core/config.h"
25 #include "../control/control.h"
26 #include "../lib/irc_string.h"
27 #include "../irc/irc.h"
28 #include "../glines/glines.h"
31 static int countext
, enforcepolicy_irc
, enforcepolicy_auth
;
33 #define TRUSTBUFSIZE 8192
34 #define TRUSTPASSLEN 128
37 typedef struct trustsocket
{
40 char authuser
[SERVERLEN
+1];
41 char buf
[TRUSTBUFSIZE
];
42 unsigned char nonce
[NONCELEN
];
49 struct trustsocket
*next
;
52 static trustsocket
*tslist
;
53 static int listenerfd
= -1;
56 typedef struct trustaccount
{
58 char server
[SERVERLEN
+1];
59 char password
[TRUSTPASSLEN
+1];
62 trustaccount trustaccounts
[MAXSERVERS
];
64 static int checkconnectionth(const char *username
, struct irc_in_addr
*ip
, trusthost
*th
, int hooknum
, int usercountadjustment
, char *message
, size_t messagelen
) {
70 if(!th
|| !trustsdbloaded
)
71 return POLICY_SUCCESS
;
76 * the purpose of this logic is to avoid spam like this:
77 * WARNING: tgX exceeded limit: 11 connected vs 10 max
78 * (goes back down to 10)
79 * WARNING: tgX exceeded limit: 11 connected vs 10 max
82 if(hooknum
== HOOK_TRUSTS_NEWNICK
) {
83 patricia_node_t
*head
, *node
;
86 head
= refnode(iptree
, ip
, th
->nodebits
);
87 PATRICIA_WALK(head
, node
)
89 nodecount
+= node
->usercount
;
92 derefnode(iptree
, head
);
94 if(th
->maxpernode
&& nodecount
+ usercountadjustment
> th
->maxpernode
) {
95 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
);
96 snprintf(message
, messagelen
, "Too many connections from your host (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
97 return POLICY_FAILURE_NODECOUNT
;
100 if(tg
->trustedfor
&& tg
->count
+ usercountadjustment
> tg
->trustedfor
) {
101 if(tg
->count
> (long)tg
->exts
[countext
]) {
102 tg
->exts
[countext
] = (void *)(long)tg
->count
;
104 controlwall(NO_OPER
, NL_TRUSTS
, "Hard connection limit exceeded (group %s): %d connected, %d max.", tg
->name
->content
, tg
->count
+ usercountadjustment
, tg
->trustedfor
);
105 snprintf(message
, messagelen
, "Too many connections from your trust (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
106 return POLICY_FAILURE_GROUPCOUNT
;
109 snprintf(message
, messagelen
, "Trust has %d out of %d allowed connections.", tg
->count
+ usercountadjustment
, tg
->trustedfor
);
112 if((tg
->flags
& TRUST_ENFORCE_IDENT
) && (username
[0] == '~')) {
113 controlwall(NO_OPER
, NL_TRUSTS
, "Ident required: %s@%s (group: %s).", username
, IPtostr(*ip
), tg
->name
->content
);
114 snprintf(message
, messagelen
, "IDENTD required from your host (%s) - see http://www.quakenet.org/help/trusts/connection-limit for details.", IPtostr(*ip
));
115 return POLICY_FAILURE_IDENTD
;
118 if(tg
->maxperident
> 0) {
123 for(th2
=tg
->hosts
;th2
;th2
=th2
->next
) {
124 for(tnp
=th2
->users
;tnp
;tnp
=nextbytrust(tnp
)) {
125 if(!ircd_strcmp(tnp
->ident
, username
))
130 if(identcount
+ usercountadjustment
> tg
->maxperident
) {
131 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
);
132 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
));
133 return POLICY_FAILURE_IDENTCOUNT
;
137 if(tg
->count
< tg
->maxusage
)
138 tg
->exts
[countext
] = (void *)(long)tg
->count
;
141 return POLICY_SUCCESS
;
144 static int checkconnection(const char *username
, struct irc_in_addr
*ip
, int hooknum
, int cloneadjustment
, char *message
, size_t messagelen
) {
145 struct irc_in_addr ip_canonicalized
;
146 ip_canonicalize_tunnel(&ip_canonicalized
, ip
);
148 return checkconnectionth(username
, &ip_canonicalized
, th_getbyhost(&ip_canonicalized
), hooknum
, cloneadjustment
, message
, messagelen
);
151 static int trustdowrite(trustsocket
*sock
, char *format
, ...) {
156 va_start(va
, format
);
157 r
= vsnprintf(buf
, sizeof(buf
), format
, va
);
165 if(write(sock
->fd
, buf
, r
+ 1) != r
+ 1)
170 static int policycheck_auth(trustsocket
*sock
, const char *sequence_id
, const char *username
, const char *host
) {
173 struct irc_in_addr ip
;
176 if(!ipmask_parse(host
, &ip
, &bits
)) {
178 return trustdowrite(sock
, "PASS %s", sequence_id
);
181 verdict
= checkconnection(username
, &ip
, HOOK_TRUSTS_NEWNICK
, 1, message
, sizeof(message
));
183 if(!enforcepolicy_auth
)
184 verdict
= POLICY_SUCCESS
;
186 if (verdict
== POLICY_SUCCESS
) {
190 return trustdowrite(sock
, "PASS %s %s", sequence_id
, message
);
192 return trustdowrite(sock
, "PASS %s", sequence_id
);
196 controlwall(NO_OPER
, NL_TRUSTS
, "Rejected connection from %s@%s using IAuth: %s", username
, host
, message
);
197 return trustdowrite(sock
, "KILL %s %s", sequence_id
, message
);
201 static int trustkillconnection(trustsocket
*sock
, char *reason
) {
202 trustdowrite(sock
, "QUIT %s", reason
);
206 static void trustfreeconnection(trustsocket
*sock
) {
207 trustsocket
**pnext
= &tslist
;
209 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
) {
211 deregisterhandler(sock
->fd
, 1);
213 nsfree(POOL_TRUSTS
, sock
);
217 pnext
= &(sock
->next
);
221 static int handletrustauth(trustsocket
*sock
, char *server_name
, char *mac
) {
223 char *password
= NULL
;
224 unsigned char digest
[16];
225 char noncehexbuf
[NONCELEN
* 2 + 1];
226 char hexbuf
[sizeof(digest
) * 2 + 1];
228 for(i
=0;i
<MAXSERVERS
;i
++) {
229 if(trustaccounts
[i
].used
&& strcmp(trustaccounts
[i
].server
, server_name
) == 0) {
230 password
= trustaccounts
[i
].password
;
236 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid servername for policy socket: '%s'", server_name
);
237 return trustkillconnection(sock
, "Invalid servername.");
241 hmacmd5_init(&h
, (unsigned char *)password
, strlen(password
));
242 hmacmd5_update(&h
, (unsigned char *)hmac_printhex(sock
->nonce
, noncehexbuf
, NONCELEN
), NONCELEN
* 2);
243 hmacmd5_final(&h
, digest
);
244 if(hmac_strcmp(mac
, hmac_printhex(digest
, hexbuf
, sizeof(digest
)))) {
245 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid password for policy socket with servername '%s'.", server_name
);
246 return trustkillconnection(sock
, "Bad MAC.");
250 strncpy(sock
->authuser
, server_name
, SERVERLEN
);
252 controlwall(NO_OPER
, NL_TRUSTS
, "Successful authentication for policy socket with servername '%s'.", server_name
);
253 return trustdowrite(sock
, "AUTHOK");
257 static int handletrustline(trustsocket
*sock
, char *line
) {
258 char *command
, *p
, *lastpos
;
259 char *tokens
[MAXTOKENS
];
260 int tokensfound
= -1;
262 for(command
=lastpos
=p
=line
;*p
;p
++) {
265 if(tokensfound
== MAXTOKENS
)
266 return trustkillconnection(sock
, "too many tokens");
268 if(tokensfound
>= 0) {
269 tokens
[tokensfound
++] = lastpos
;
277 if(tokensfound
== MAXTOKENS
)
278 return trustkillconnection(sock
, "too many tokens");
279 tokens
[tokensfound
++] = lastpos
;
282 if(!sock
->authed
&& !strcmp("AUTH", command
)) {
284 return trustkillconnection(sock
, "incorrect arg count for command.");
286 return handletrustauth(sock
, tokens
[0], tokens
[1]);
287 } else if(sock
->authed
&& !strcmp("CHECK", command
)) {
289 return trustkillconnection(sock
, "incorrect arg count for command.");
291 policycheck_auth(sock
, tokens
[0], tokens
[1], tokens
[2]);
294 Error("trusts_policy", ERR_WARNING
, "Bad command: %s", command
);
299 static trustsocket
*findtrustsocketbyfd(int fd
) {
300 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
)
307 static int handletrustclient(trustsocket
*sock
) {
308 int r
, remaining
= TRUSTBUFSIZE
- sock
->size
, i
;
312 trustkillconnection(sock
, "Buffer overflow.");
316 r
= read(sock
->fd
, sock
->buf
+ sock
->size
, remaining
);
323 for(c
=sock
->buf
,i
=0;i
<sock
->size
;i
++,c
++) {
327 if(!handletrustline(sock
, lastpos
))
330 lastpos
= c
+ 1; /* is this ok? */
332 sock
->size
-=lastpos
- sock
->buf
;
333 memmove(sock
->buf
, lastpos
, sock
->size
);
338 static void processtrustclient(int fd
, short events
) {
339 trustsocket
*sock
= findtrustsocketbyfd(fd
);
345 if(!handletrustclient(sock
))
346 trustfreeconnection(sock
);
349 static void trustdotimeout(void *arg
) {
350 time_t t
= time(NULL
);
351 trustsocket
**pnext
, *next
, *sock
;
355 for(sock
=tslist
;sock
;) {
358 if(!sock
->authed
&& t
>= sock
->timeout
) {
359 trustkillconnection(sock
, "Auth timeout.");
360 deregisterhandler(sock
->fd
, 1);
362 nsfree(POOL_TRUSTS
, sock
);
364 pnext
= &(sock
->next
);
371 static void processtrustlistener(int fd
, short events
) {
372 if(events
& POLLIN
) {
374 char buf
[NONCELEN
* 2 + 1];
376 int newfd
= accept(fd
, NULL
, NULL
), flags
;
380 flags
= fcntl(newfd
, F_GETFL
, 0);
382 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
387 if(fcntl(fd
, F_SETFL
, flags
|O_NONBLOCK
) < 0) {
388 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
393 registerhandler(newfd
, POLLIN
|POLLERR
|POLLHUP
, processtrustclient
);
395 sock
= nsmalloc(POOL_TRUSTS
, sizeof(trustsocket
));
397 deregisterhandler(newfd
, 1);
405 if(fread((char *)sock
->nonce
, 1, NONCELEN
, urandom
) != NONCELEN
) {
406 Error("trusts_policy", ERR_WARNING
, "Error getting random bytes.");
407 deregisterhandler(newfd
, 1);
409 nsfree(POOL_TRUSTS
, sock
);
413 sock
->connected
= time(NULL
);
414 sock
->timeout
= time(NULL
) + 30;
417 if(!trustdowrite(sock
, "AUTH %s", hmac_printhex(sock
->nonce
, buf
, NONCELEN
))) {
418 Error("trusts_policy", ERR_WARNING
, "Error writing auth to fd %d.", newfd
);
419 deregisterhandler(newfd
, 1);
421 nsfree(POOL_TRUSTS
, sock
);
428 static int createlistenersock(int port
) {
429 struct sockaddr_in s
;
433 memset(&s
, 0, sizeof(struct sockaddr_in
));
434 s
.sin_family
= AF_INET
;
435 s
.sin_addr
.s_addr
= INADDR_ANY
;
436 s
.sin_port
= htons(port
);
438 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
440 Error("trusts_policy", ERR_WARNING
, "Unable to get socket for trustfd.");
445 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof(optval
));
447 if(bind(fd
, (struct sockaddr
*)&s
, sizeof(struct sockaddr_in
)) < 0) {
448 Error("trusts_policy", ERR_WARNING
, "Unable to bind trustfd.");
453 if(listen(fd
, 5) < 0) {
454 Error("trusts_policy", ERR_WARNING
, "Unable to listen on trustfd.");
459 registerhandler(fd
, POLLIN
, processtrustlistener
);
464 static void policycheck_irc(int hooknum
, void *arg
) {
467 long moving
= (long)args
[1];
474 verdict
= checkconnectionth(np
->ident
, &np
->p_nodeaddr
, gettrusthost(np
), hooknum
, 0, message
, sizeof(message
));
476 if(!enforcepolicy_irc
)
477 verdict
= POLICY_SUCCESS
;
480 case POLICY_FAILURE_NODECOUNT
:
481 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_IGNORE_TRUST
, "trusts_policy");
483 case POLICY_FAILURE_IDENTD
:
484 glinebyip("~*", &np
->p_ipaddr
, 128, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
486 case POLICY_FAILURE_IDENTCOUNT
:
487 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
492 static int trusts_cmdtrustpolicyirc(void *source
, int cargc
, char **cargv
) {
493 nick
*sender
= source
;
496 controlreply(sender
, "Use of glines for trust policy enforcement is currently %s.", enforcepolicy_irc
?"enabled":"disabled");
500 enforcepolicy_irc
= atoi(cargv
[0]);
501 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s use of glines for trust policy enforcement.", controlid(sender
), enforcepolicy_irc
?"enabled":"disabled");
502 controlreply(sender
, "Use of glines for trust policy enforcement is now %s.", enforcepolicy_irc
?"enabled":"disabled");
507 static int trusts_cmdtrustpolicyauth(void *source
, int cargc
, char **cargv
) {
508 nick
*sender
= source
;
511 controlreply(sender
, "Trust policy enforcement with IAuth is currently %s.", enforcepolicy_auth
?"enabled":"disabled");
515 enforcepolicy_auth
= atoi(cargv
[0]);
516 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s trust policy enforcement with IAuth.", controlid(sender
), enforcepolicy_auth
?"enabled":"disabled");
517 controlreply(sender
, "Trust policy enforcement with IAuth is now %s.", enforcepolicy_auth
?"enabled":"disabled");
523 static int trusts_cmdtrustsockets(void *source
, int cargc
, char **cargv
) {
524 nick
*sender
= source
;
530 controlreply(sender
, "Server Connected for Accepted Rejected");
532 for(sock
=tslist
;sock
;sock
=sock
->next
)
533 controlreply(sender
, "%-20s %20s %10d %15d", sock
->authed
?sock
->authuser
:"<not authenticated>", longtoduration(now
- sock
->connected
, 0), sock
->accepted
, sock
->rejected
);
535 controlreply(sender
, "-- End of list.");
539 void loadtrustaccounts(void) {
542 memset(trustaccounts
, 0, sizeof(trustaccounts
));
544 accts
= getconfigitems("trusts_policy", "server");
546 Error("trusts_policy", ERR_INFO
, "No servers added.");
548 sstring
**servers
= (sstring
**)(accts
->content
);
550 for(i
=0;i
<accts
->cursi
;i
++) {
555 Error("trusts_policy", ERR_INFO
, "Too many servers specified.");
559 strncpy(server
, servers
[i
]->content
, sizeof(server
));
561 pos
= strchr(server
, ',');
564 Error("trusts_policy", ERR_INFO
, "Server line is missing password: %s", server
);
570 trustaccounts
[i
].used
= 1;
571 strncpy(trustaccounts
[i
].server
, server
, SERVERLEN
);
572 strncpy(trustaccounts
[i
].password
, pos
+1, TRUSTPASSLEN
);
577 static void trustaccounts_rehash(int hooknum
, void *arg
) {
585 countext
= registertgext("count");
589 m
= getconfigitem("trusts_policy", "enforcepolicy_irc");
591 enforcepolicy_irc
= atoi(m
->content
);
593 m
= getconfigitem("trusts_policy", "enforcepolicy_auth");
595 enforcepolicy_auth
= atoi(m
->content
);
597 m
= getconfigitem("trusts_policy", "trustport");
599 trustport
= atoi(m
->content
);
601 trustport
= DEFAULT_TRUSTPORT
;
604 listenerfd
= createlistenersock(trustport
);
608 registerhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
609 registerhook(HOOK_TRUSTS_LOSTNICK
, &policycheck_irc
);
610 registerhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
612 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.");
613 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.");
614 registercontrolhelpcmd("trustsockets", NO_DEVELOPER
, 0, trusts_cmdtrustsockets
, "Usage: trustsockets\nLists all currently active TRUST sockets.");
616 schedulerecurring(time(NULL
)+1, 0, 5, trustdotimeout
, NULL
);
618 urandom
= fopen("/dev/urandom", "rb");
620 Error("trusts_policy", ERR_ERROR
, "Couldn't open /dev/urandom.");
624 trustsocket
*sock
, *next
;
629 releasetgext(countext
);
631 deregisterhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
632 deregisterhook(HOOK_TRUSTS_LOSTNICK
, policycheck_irc
);
633 deregisterhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
635 deregistercontrolcmd("trustpolicyirc", trusts_cmdtrustpolicyirc
);
636 deregistercontrolcmd("trustpolicyauth", trusts_cmdtrustpolicyauth
);
637 deregistercontrolcmd("trustsockets", trusts_cmdtrustsockets
);
639 deleteallschedules(trustdotimeout
);
644 if (listenerfd
!= -1)
645 deregisterhandler(listenerfd
, 1);
647 for(sock
=tslist
;sock
;) {
650 trustkillconnection(sock
, "Unloading module.");
651 trustfreeconnection(sock
);