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
) {
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
, 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
, 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
, 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
), 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 (verdict
== POLICY_SUCCESS
) {
187 return trustdowrite(sock
, "PASS %s %s", sequence_id
, message
);
189 return trustdowrite(sock
, "PASS %s", sequence_id
);
193 controlwall(NO_OPER
, NL_TRUSTS
, "Rejected connection from %s@%s using IAuth: %s", username
, host
, message
);
194 return trustdowrite(sock
, "KILL %s %s", sequence_id
, message
);
198 static int trustkillconnection(trustsocket
*sock
, char *reason
) {
199 trustdowrite(sock
, "QUIT %s", reason
);
203 static void trustfreeconnection(trustsocket
*sock
) {
204 trustsocket
**pnext
= &tslist
;
206 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
) {
208 deregisterhandler(sock
->fd
, 1);
210 nsfree(POOL_TRUSTS
, sock
);
214 pnext
= &(sock
->next
);
218 static int handletrustauth(trustsocket
*sock
, char *server_name
, char *mac
) {
220 char *password
= NULL
;
221 unsigned char digest
[16];
222 char noncehexbuf
[NONCELEN
* 2 + 1];
223 char hexbuf
[sizeof(digest
) * 2 + 1];
225 for(i
=0;i
<MAXSERVERS
;i
++) {
226 if(trustaccounts
[i
].used
&& strcmp(trustaccounts
[i
].server
, server_name
) == 0) {
227 password
= trustaccounts
[i
].password
;
233 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid servername for policy socket: '%s'", server_name
);
234 return trustkillconnection(sock
, "Invalid servername.");
238 hmacmd5_init(&h
, (unsigned char *)password
, strlen(password
));
239 hmacmd5_update(&h
, (unsigned char *)hmac_printhex(sock
->nonce
, noncehexbuf
, NONCELEN
), NONCELEN
* 2);
240 hmacmd5_final(&h
, digest
);
241 if(hmac_strcmp(mac
, hmac_printhex(digest
, hexbuf
, sizeof(digest
)))) {
242 controlwall(NO_OPER
, NL_TRUSTS
, "Invalid password for policy socket with servername '%s'.", server_name
);
243 return trustkillconnection(sock
, "Bad MAC.");
247 strncpy(sock
->authuser
, server_name
, SERVERLEN
);
249 controlwall(NO_OPER
, NL_TRUSTS
, "Successful authentication for policy socket with servername '%s'.", server_name
);
250 return trustdowrite(sock
, "AUTHOK");
254 static int handletrustline(trustsocket
*sock
, char *line
) {
255 char *command
, *p
, *lastpos
;
256 char *tokens
[MAXTOKENS
];
257 int tokensfound
= -1;
259 for(command
=lastpos
=p
=line
;*p
;p
++) {
262 if(tokensfound
== MAXTOKENS
)
263 return trustkillconnection(sock
, "too many tokens");
265 if(tokensfound
>= 0) {
266 tokens
[tokensfound
++] = lastpos
;
274 if(tokensfound
== MAXTOKENS
)
275 return trustkillconnection(sock
, "too many tokens");
276 tokens
[tokensfound
++] = lastpos
;
279 if(!sock
->authed
&& !strcmp("AUTH", command
)) {
281 return trustkillconnection(sock
, "incorrect arg count for command.");
283 return handletrustauth(sock
, tokens
[0], tokens
[1]);
284 } else if(sock
->authed
&& !strcmp("CHECK", command
)) {
286 return trustkillconnection(sock
, "incorrect arg count for command.");
288 policycheck_auth(sock
, tokens
[0], tokens
[1], tokens
[2]);
291 Error("trusts_policy", ERR_WARNING
, "Bad command: %s", command
);
296 static trustsocket
*findtrustsocketbyfd(int fd
) {
297 for(trustsocket
*ts
=tslist
;ts
;ts
=ts
->next
)
304 static int handletrustclient(trustsocket
*sock
) {
305 int r
, remaining
= TRUSTBUFSIZE
- sock
->size
, i
;
309 trustkillconnection(sock
, "Buffer overflow.");
313 r
= read(sock
->fd
, sock
->buf
+ sock
->size
, remaining
);
320 for(c
=sock
->buf
,i
=0;i
<sock
->size
;i
++,c
++) {
324 if(!handletrustline(sock
, lastpos
))
327 lastpos
= c
+ 1; /* is this ok? */
329 sock
->size
-=lastpos
- sock
->buf
;
330 memmove(sock
->buf
, lastpos
, sock
->size
);
335 static void processtrustclient(int fd
, short events
) {
336 trustsocket
*sock
= findtrustsocketbyfd(fd
);
342 if(!handletrustclient(sock
))
343 trustfreeconnection(sock
);
346 static void trustdotimeout(void *arg
) {
347 time_t t
= time(NULL
);
348 trustsocket
**pnext
, *next
, *sock
;
352 for(sock
=tslist
;sock
;) {
355 if(!sock
->authed
&& t
>= sock
->timeout
) {
356 trustkillconnection(sock
, "Auth timeout.");
357 deregisterhandler(sock
->fd
, 1);
359 nsfree(POOL_TRUSTS
, sock
);
361 pnext
= &(sock
->next
);
368 static void processtrustlistener(int fd
, short events
) {
369 if(events
& POLLIN
) {
371 char buf
[NONCELEN
* 2 + 1];
373 int newfd
= accept(fd
, NULL
, NULL
), flags
;
377 flags
= fcntl(newfd
, F_GETFL
, 0);
379 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
384 if(fcntl(fd
, F_SETFL
, flags
|O_NONBLOCK
) < 0) {
385 Error("trusts_policy", ERR_WARNING
, "Unable to set socket non-blocking.");
390 registerhandler(newfd
, POLLIN
|POLLERR
|POLLHUP
, processtrustclient
);
392 sock
= nsmalloc(POOL_TRUSTS
, sizeof(trustsocket
));
394 deregisterhandler(newfd
, 1);
402 if(fread((char *)sock
->nonce
, 1, NONCELEN
, urandom
) != NONCELEN
) {
403 Error("trusts_policy", ERR_WARNING
, "Error getting random bytes.");
404 deregisterhandler(newfd
, 1);
406 nsfree(POOL_TRUSTS
, sock
);
410 sock
->connected
= time(NULL
);
411 sock
->timeout
= time(NULL
) + 30;
414 if(!trustdowrite(sock
, "AUTH %s", hmac_printhex(sock
->nonce
, buf
, NONCELEN
))) {
415 Error("trusts_policy", ERR_WARNING
, "Error writing auth to fd %d.", newfd
);
416 deregisterhandler(newfd
, 1);
418 nsfree(POOL_TRUSTS
, sock
);
425 static int createlistenersock(int port
) {
426 struct sockaddr_in s
;
430 memset(&s
, 0, sizeof(struct sockaddr_in
));
431 s
.sin_family
= AF_INET
;
432 s
.sin_addr
.s_addr
= INADDR_ANY
;
433 s
.sin_port
= htons(port
);
435 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
437 Error("trusts_policy", ERR_WARNING
, "Unable to get socket for trustfd.");
442 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &optval
, sizeof(optval
));
444 if(bind(fd
, (struct sockaddr
*)&s
, sizeof(struct sockaddr_in
)) < 0) {
445 Error("trusts_policy", ERR_WARNING
, "Unable to bind trustfd.");
450 if(listen(fd
, 5) < 0) {
451 Error("trusts_policy", ERR_WARNING
, "Unable to listen on trustfd.");
456 registerhandler(fd
, POLLIN
, processtrustlistener
);
461 static void policycheck_irc(int hooknum
, void *arg
) {
464 long moving
= (long)args
[1];
471 verdict
= checkconnectionth(np
->ident
, &np
->p_nodeaddr
, gettrusthost(np
), hooknum
, 0, message
, sizeof(message
));
477 case POLICY_FAILURE_NODECOUNT
:
478 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_IGNORE_TRUST
, "trusts_policy");
480 case POLICY_FAILURE_IDENTD
:
481 glinebyip("~*", &np
->p_ipaddr
, 128, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
483 case POLICY_FAILURE_IDENTCOUNT
:
484 glinebynick(np
, POLICY_GLINE_DURATION
, message
, GLINE_ALWAYS_USER
|GLINE_IGNORE_TRUST
, "trusts_policy");
489 static int trusts_cmdtrustpolicy(void *source
, int cargc
, char **cargv
) {
490 nick
*sender
= source
;
493 controlreply(sender
, "Trust policy enforcement is currently %s.", enforcepolicy
?"enabled":"disabled");
497 enforcepolicy
= atoi(cargv
[0]);
498 controlwall(NO_OPER
, NL_TRUSTS
, "%s %s trust policy enforcement.", controlid(sender
), enforcepolicy
?"enabled":"disabled");
499 controlreply(sender
, "Trust policy enforcement is now %s.", enforcepolicy
?"enabled":"disabled");
504 static int trusts_cmdtrustsockets(void *source
, int cargc
, char **cargv
) {
505 nick
*sender
= source
;
511 controlreply(sender
, "Server Connected for Accepted Rejected");
513 for(sock
=tslist
;sock
;sock
=sock
->next
)
514 controlreply(sender
, "%-20s %20s %10d %15d", sock
->authed
?sock
->authuser
:"<not authenticated>", longtoduration(now
- sock
->connected
, 0), sock
->accepted
, sock
->rejected
);
516 controlreply(sender
, "-- End of list.");
520 void loadtrustaccounts(void) {
523 memset(trustaccounts
, 0, sizeof(trustaccounts
));
525 accts
= getconfigitems("trusts_policy", "server");
527 Error("trusts_policy", ERR_INFO
, "No servers added.");
529 sstring
**servers
= (sstring
**)(accts
->content
);
531 for(i
=0;i
<accts
->cursi
;i
++) {
536 Error("trusts_policy", ERR_INFO
, "Too many servers specified.");
540 strncpy(server
, servers
[i
]->content
, sizeof(server
));
542 pos
= strchr(server
, ',');
545 Error("trusts_policy", ERR_INFO
, "Server line is missing password: %s", server
);
551 trustaccounts
[i
].used
= 1;
552 strncpy(trustaccounts
[i
].server
, server
, SERVERLEN
);
553 strncpy(trustaccounts
[i
].password
, pos
+1, TRUSTPASSLEN
);
558 static void trustaccounts_rehash(int hooknum
, void *arg
) {
566 countext
= registertgext("count");
570 m
= getconfigitem("trusts_policy", "enforcepolicy");
572 enforcepolicy
= atoi(m
->content
);
574 m
= getconfigitem("trusts_policy", "trustport");
576 trustport
= atoi(m
->content
);
578 trustport
= DEFAULT_TRUSTPORT
;
581 listenerfd
= createlistenersock(trustport
);
585 registerhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
586 registerhook(HOOK_TRUSTS_LOSTNICK
, &policycheck_irc
);
587 registerhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
589 registercontrolhelpcmd("trustpolicy", NO_DEVELOPER
, 1, trusts_cmdtrustpolicy
, "Usage: trustpolicy ?1|0?\nEnables or disables policy enforcement. Shows current status when no parameter is specified.");
590 registercontrolhelpcmd("trustsockets", NO_DEVELOPER
, 0, trusts_cmdtrustsockets
, "Usage: trustsockets\nLists all currently active TRUST sockets.");
592 schedulerecurring(time(NULL
)+1, 0, 5, trustdotimeout
, NULL
);
594 urandom
= fopen("/dev/urandom", "rb");
596 Error("trusts_policy", ERR_ERROR
, "Couldn't open /dev/urandom.");
600 trustsocket
*sock
, *next
;
605 releasetgext(countext
);
607 deregisterhook(HOOK_TRUSTS_NEWNICK
, policycheck_irc
);
608 deregisterhook(HOOK_TRUSTS_LOSTNICK
, policycheck_irc
);
609 deregisterhook(HOOK_CORE_REHASH
, trustaccounts_rehash
);
611 deregistercontrolcmd("trustpolicy", trusts_cmdtrustpolicy
);
612 deregistercontrolcmd("trustsockets", trusts_cmdtrustsockets
);
614 deleteallschedules(trustdotimeout
);
619 if (listenerfd
!= -1)
620 deregisterhandler(listenerfd
, 1);
622 for(sock
=tslist
;sock
;) {
625 trustkillconnection(sock
, "Unloading module.");
626 trustfreeconnection(sock
);