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
;
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
, char *hint
) {
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 - %s.", trusts_cidr2str(ip
, th
->nodebits
), tg
->name
->content
, nodecount
, th
->maxpernode
, hint
);
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: '%s', %d connected, %d max. - %s.", tg
->name
->content
, tg
->count
, tg
->trustedfor
, hint
);
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) - %s.", username
, IPtostr(*ip
), tg
->name
->content
, hint
);
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 - %s.", username
, IPtostr(*ip
), tg
->name
->content
, identcount
, tg
->maxperident
, hint
);
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
, char *hint
) {
145 struct irc_in_addr ip_canonicalized
;
146 ip_canonicalize_tunnel(&ip_canonicalized
, ip
);
148 return checkconnectionth(username
, &ip_canonicalized
, th_getbyhost(ip
), hooknum
, cloneadjustment
, message
, messagelen
, hint
);
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
), "enforcing with IAuth");
183 if (verdict
== POLICY_SUCCESS
) {
187 return trustdowrite(sock
, "PASS %s %s", sequence_id
, message
);
189 return trustdowrite(sock
, "PASS %s", sequence_id
);
193 return trustdowrite(sock
, "KILL %s %s", sequence_id
, message
);
197 static int trustkillconnection(trustsocket
*sock
, char *reason
) {
198 trustdowrite(sock
, "QUIT %s", reason
);
202 static void trustfreeconnection(trustsocket
*sock
) {
203 trustsocket
**pnext
= &tslist
;
205 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
) {
207 deregisterhandler(sock
->fd
, 1);
209 nsfree(POOL_TRUSTS
, sock
);
213 pnext
= &(sock
->next
);
217 static int handletrustauth(trustsocket
*sock
, char *server_name
, char *mac
) {
219 char *password
= NULL
;
220 unsigned char digest
[16];
221 char noncehexbuf
[NONCELEN
* 2 + 1];
222 char hexbuf
[sizeof(digest
) * 2 + 1];
224 for(i
=0;i
<MAXSERVERS
;i
++) {
225 if(trustaccounts
[i
].used
&& strcmp(trustaccounts
[i
].server
, server_name
) == 0) {
226 password
= trustaccounts
[i
].password
;
232 return trustkillconnection(sock
, "Invalid servername.");
235 hmacmd5_init(&h
, (unsigned char *)password
, strlen(password
));
236 hmacmd5_update(&h
, (unsigned char *)hmac_printhex(sock
->nonce
, noncehexbuf
, NONCELEN
), NONCELEN
* 2);
237 hmacmd5_final(&h
, digest
);
238 if(hmac_strcmp(mac
, hmac_printhex(digest
, hexbuf
, sizeof(digest
))))
239 return trustkillconnection(sock
, "Bad MAC.");
242 strncpy(sock
->authuser
, server_name
, SERVERLEN
);
243 return trustdowrite(sock
, "AUTHOK");
247 static int handletrustline(trustsocket
*sock
, char *line
) {
248 char *command
, *p
, *lastpos
;
249 char *tokens
[MAXTOKENS
];
250 int tokensfound
= -1;
252 for(command
=lastpos
=p
=line
;*p
;p
++) {
255 if(tokensfound
== MAXTOKENS
)
256 return trustkillconnection(sock
, "too many tokens");
258 if(tokensfound
>= 0) {
259 tokens
[tokensfound
++] = lastpos
;
267 if(tokensfound
== MAXTOKENS
)
268 return trustkillconnection(sock
, "too many tokens");
269 tokens
[tokensfound
++] = lastpos
;
272 if(!sock
->authed
&& !strcmp("AUTH", command
)) {
274 return trustkillconnection(sock
, "incorrect arg count for command.");
276 return handletrustauth(sock
, tokens
[0], tokens
[1]);
277 } else if(sock
->authed
&& !strcmp("CHECK", command
)) {
279 return trustkillconnection(sock
, "incorrect arg count for command.");
281 policycheck_auth(sock
, tokens
[0], tokens
[1], tokens
[2]);
284 Error("trusts_policy", ERR_WARNING
, "Bad command: %s", command
);
289 static trustsocket
*findtrustsocketbyfd(int fd
) {
290 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
)
297 static int handletrustclient(trustsocket
*sock
) {
298 int r
, remaining
= TRUSTBUFSIZE
- sock
->size
, i
;
302 trustkillconnection(sock
, "Buffer overflow.");
306 r
= read(sock
->fd
, sock
->buf
+ sock
->size
, remaining
);
313 for(c
=sock
->buf
,i
=0;i
<sock
->size
;i
++,c
++) {
317 if(!handletrustline(sock
, lastpos
))
320 lastpos
= c
+ 1; /* is this ok? */
322 sock
->size
-=lastpos
- sock
->buf
;
323 memmove(sock
->buf
, lastpos
, sock
->size
);
328 static void processtrustclient(int fd
, short events
) {
329 trustsocket
*sock
= findtrustsocketbyfd(fd
);
335 if(!handletrustclient(sock
))
336 trustfreeconnection(sock
);
339 static void trustdotimeout(void *arg
) {
340 time_t t
= time(NULL
);
341 trustsocket
**pnext
, *next
, *sock
;
345 for(sock
=tslist
;sock
;) {
348 if(!sock
->authed
&& t
>= sock
->timeout
) {
349 trustkillconnection(sock
, "Auth timeout.");
350 deregisterhandler(sock
->fd
, 1);
352 nsfree(POOL_TRUSTS
, sock
);
354 pnext
= &(sock
->next
);
361 static void processtrustlistener(int fd
, short events
) {
362 if(events
& POLLIN
) {
364 char buf
[NONCELEN
* 2 + 1];
366 int newfd
= accept(fd
, NULL
, NULL
), flags
;
370 flags
= fcntl(newfd
, F_GETFL
, 0);
372 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
377 if(fcntl(fd
, F_SETFL
, flags
|O_NONBLOCK
) < 0) {
378 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
383 registerhandler(newfd
, POLLIN
|POLLERR
|POLLHUP
, processtrustclient
);
385 sock
= nsmalloc(POOL_TRUSTS
, sizeof(trustsocket
));
387 deregisterhandler(newfd
, 1);
395 if(fread((char *)sock
->nonce
, 1, NONCELEN
, urandom
) != NONCELEN
) {
396 Error("trusts_policy", ERR_WARNING
, "Error getting random bytes.");
397 deregisterhandler(newfd
, 1);
399 nsfree(POOL_TRUSTS
, sock
);
403 sock
->connected
= time(NULL
);
404 sock
->timeout
= time(NULL
) + 30;
407 if(!trustdowrite(sock
, "AUTH %s", hmac_printhex(sock
->nonce
, buf
, NONCELEN
))) {
408 Error("trusts_policy", ERR_WARNING
, "Error writing auth to fd %d.", newfd
);
409 deregisterhandler(newfd
, 1);
411 nsfree(POOL_TRUSTS
, sock
);
418 static int createlistenersock(int port
) {
419 struct sockaddr_in s
;
423 memset(&s
, 0, sizeof(struct sockaddr_in
));
424 s
.sin_family
= AF_INET
;
425 s
.sin_addr
.s_addr
= INADDR_ANY
;
426 s
.sin_port
= htons(port
);
428 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
430 Error("trusts_policy", ERR_WARNING
, "Unable to get socket for trustfd.");
435 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof(optval
));
437 if(bind(fd
, (struct sockaddr
*)&s
, sizeof(struct sockaddr_in
)) < 0) {
438 Error("trusts_policy", ERR_WARNING
, "Unable to bind trustfd.");
443 if(listen(fd
, 5) < 0) {
444 Error("trusts_policy", ERR_WARNING
, "Unable to listen on trustfd.");
449 registerhandler(fd
, POLLIN
, processtrustlistener
);
454 static void policycheck_irc(int hooknum
, void *arg
) {
457 long moving
= (long)args
[1];
466 hint
= "enforcing with glines";
468 hint
= "not enforcing";
470 verdict
= checkconnectionth(np
->ident
, &np
->p_nodeaddr
, gettrusthost(np
), hooknum
, 0, message
, sizeof(message
), hint
);
476 case POLICY_FAILURE_NODECOUNT
:
477 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_IGNORE_TRUST
);
479 case POLICY_FAILURE_IDENTD
:
480 glinebyip("~*", &np
->p_ipaddr
, 128, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
);
482 case POLICY_FAILURE_IDENTCOUNT
:
483 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
);
488 static int trusts_cmdtrustpolicy(void *source
, int cargc
, char **cargv
) {
489 nick
*sender
= source
;
492 controlreply(sender
, "Trust policy enforcement is currently %s.", enforcepolicy
?"enabled":"disabled");
496 enforcepolicy
= atoi(cargv
[0]);
497 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s trust policy enforcement.", controlid(sender
), enforcepolicy
?"enabled":"disabled");
498 controlreply(sender
, "Trust policy enforcement is now %s.", enforcepolicy
?"enabled":"disabled");
503 static int trusts_cmdtrustsockets(void *source
, int cargc
, char **cargv
) {
504 nick
*sender
= source
;
510 controlreply(sender
, "Server Connected for Accepted Rejected");
512 for(sock
=tslist
;sock
;sock
=sock
->next
)
513 controlreply(sender
, "%-20s %20s %10d %15d", sock
->authed
?sock
->authuser
:"<not authenticated>", longtoduration(now
- sock
->connected
, 0), sock
->accepted
, sock
->rejected
);
515 controlreply(sender
, "-- End of list.");
524 countext
= registertgext("count");
528 m
= getconfigitem("trusts_policy", "enforcepolicy");
530 enforcepolicy
= atoi(m
->content
);
532 m
= getconfigitem("trusts_policy", "trustport");
534 trustport
= atoi(m
->content
);
536 trustport
= DEFAULT_TRUSTPORT
;
539 listenerfd
= createlistenersock(trustport
);
541 accts
= getconfigitems("trusts_policy", "server");
543 Error("trusts_policy", ERR_INFO
, "No servers added.");
545 sstring
**servers
= (sstring
**)(accts
->content
);
547 for(i
=0;i
<accts
->cursi
;i
++) {
552 Error("trusts_policy", ERR_INFO
, "Too many servers specified.");
556 strncpy(server
, servers
[i
]->content
, sizeof(server
));
558 pos
= strchr(server
, ',');
561 Error("trusts_policy", ERR_INFO
, "Server line is missing password: %s", server
);
567 trustaccounts
[i
].used
= 1;
568 strncpy(trustaccounts
[i
].server
, server
, SERVERLEN
);
569 strncpy(trustaccounts
[i
].password
, pos
+1, TRUSTPASSLEN
);
573 registerhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
574 registerhook(HOOK_TRUSTS_LOSTNICK
, policycheck_irc
);
576 registercontrolhelpcmd("trustpolicy", NO_DEVELOPER
, 1, trusts_cmdtrustpolicy
, "Usage: trustpolicy ?1|0?\nEnables or disables policy enforcement. Shows current status when no parameter is specified.");
577 registercontrolhelpcmd("trustsockets", NO_DEVELOPER
, 0, trusts_cmdtrustsockets
, "Usage: trustsockets\nLists all currently active TRUST sockets.");
579 schedulerecurring(time(NULL
)+1, 0, 5, trustdotimeout
, NULL
);
581 urandom
= fopen("/dev/urandom", "rb");
583 Error("trusts_policy", ERR_ERROR
, "Couldn't open /dev/urandom.");
587 trustsocket
*sock
, *next
;
592 releasetgext(countext
);
594 deregisterhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
595 deregisterhook(HOOK_TRUSTS_LOSTNICK
, policycheck_irc
);
597 deregistercontrolcmd("trustpolicy", trusts_cmdtrustpolicy
);
598 deregistercontrolcmd("trustsockets", trusts_cmdtrustsockets
);
600 deleteallschedules(trustdotimeout
);
605 if (listenerfd
!= -1)
606 deregisterhandler(listenerfd
, 1);
608 for(sock
=tslist
;sock
;) {
611 trustkillconnection(sock
, "Unloading module.");
612 trustfreeconnection(sock
);