3 Copyright (C) 2004-2007 Chris Porter.
10 #include <sys/types.h>
11 #include <sys/socket.h>
14 #include <sys/ioctl.h>
19 #include "../lib/sstring.h"
20 #include "../lib/irc_string.h"
21 #include "../core/config.h"
22 #include "../core/events.h"
23 #include "../lib/version.h"
25 #include "nterfacer.h"
28 MODULE_VERSION("1.1p" PROTOCOL_VERSION
);
30 struct service_node
*tree
= NULL
;
31 struct esocket_events nterfacer_events
;
32 struct esocket
*nterfacer_sock
;
33 struct rline
*rlines
= NULL
;
34 unsigned short nterfacer_token
= BLANK_TOKEN
;
35 struct nterface_auto_log
*nrl
;
37 struct service_node
*ping
= NULL
;
39 struct permitted
*permits
;
42 int ping_handler(struct rline
*ri
, int argc
, char **argv
);
46 int debug_mode
= getcopyconfigitemintpositive("nterfacer", "debug", 0);
48 nrl
= nterface_open_log("nterfacer", "logs/nterfacer.log", debug_mode
);
50 loaded
= load_permits();
52 nterface_log(nrl
, NL_ERROR
, "No permits loaded successfully.");
55 nterface_log(nrl
, NL_INFO
, "Loaded %d permit%s successfully.", loaded
, loaded
==1?"":"s");
58 nterfacer_events
.on_accept
= nterfacer_accept_event
;
59 nterfacer_events
.on_line
= nterfacer_line_event
;
60 nterfacer_events
.on_disconnect
= NULL
;
62 nterfacer_token
= esocket_token();
64 ping
= register_service("nterfacer");
68 register_handler(ping
, "ping", 0, ping_handler
);
71 accept_fd
= setup_listening_socket();
73 nterface_log(nrl
, NL_ERROR
, "Unable to setup listening socket!");
75 nterfacer_sock
= esocket_add(accept_fd
, ESOCKET_UNIX_DOMAIN
, &nterfacer_events
, nterfacer_token
);
78 /* the main unix domain socket must NOT have a disconnect event. */
79 nterfacer_events
.on_disconnect
= nterfacer_disconnect_event
;
82 void free_handler(struct handler
*hp
) {
83 freesstring(hp
->command
);
87 void free_handlers(struct service_node
*tp
) {
88 struct handler
*hp
, *lp
;
90 for(hp
=tp
->handlers
;hp
;) {
100 struct service_node
*tp
, *lp
;
104 deregister_service(ping
);
114 if((accept_fd
!= -1) && nterfacer_sock
) {
115 esocket_clean_by_token(nterfacer_token
);
116 nterfacer_sock
= NULL
;
120 if(permits
&& permit_count
) {
121 for(i
=0;i
<permit_count
;i
++) {
122 freesstring(permits
[i
].hostname
);
123 freesstring(permits
[i
].password
);
130 nrl
= nterface_close_log(nrl
);
131 nscheckfreeall(POOL_NTERFACER
);
134 int load_permits(void) {
135 int loaded_lines
= 0, i
, j
;
136 struct permitted
*new_permits
, *resized
, *item
;
137 struct hostent
*host
;
138 array
*hostnamesa
, *passwordsa
;
139 sstring
**hostnames
, **passwords
;
141 hostnamesa
= getconfigitems("nterfacer", "hostname");
142 passwordsa
= getconfigitems("nterfacer", "password");
143 if(!hostnamesa
|| !passwordsa
) {
144 nterface_log(nrl
, NL_ERROR
, "Unable to load hostnames/passwords.");
147 if(hostnamesa
->cursi
!= passwordsa
->cursi
) {
148 nterface_log(nrl
, NL_ERROR
, "Different number of hostnames/passwords in config file.");
152 hostnames
= (sstring
**)hostnamesa
->content
;
153 passwords
= (sstring
**)passwordsa
->content
;
155 new_permits
= ntmalloc(hostnamesa
->cursi
* sizeof(struct permitted
));
156 memset(new_permits
, 0, hostnamesa
->cursi
* sizeof(struct permitted
));
159 for(i
=0;i
<hostnamesa
->cursi
;i
++) {
160 item
->hostname
= getsstring(hostnames
[i
]->content
, hostnames
[i
]->length
);
162 host
= gethostbyname(item
->hostname
->content
);
164 nterface_log(nrl
, NL_WARNING
, "Couldn't resolve hostname: %s (item %d).", item
->hostname
->content
, i
+ 1);
165 freesstring(item
->hostname
);
169 item
->ihost
= (*(struct in_addr
*)host
->h_addr
).s_addr
;
170 for(j
=0;j
<loaded_lines
;j
++) {
171 if(new_permits
[j
].ihost
== item
->ihost
) {
172 nterface_log(nrl
, NL_WARNING
, "Host with items %d and %d is identical, dropping item %d.", j
+ 1, i
+ 1, i
+ 1);
178 freesstring(item
->hostname
);
182 item
->password
= getsstring(passwords
[i
]->content
, passwords
[i
]->length
);
183 nterface_log(nrl
, NL_DEBUG
, "Loaded permit, hostname: %s.", item
->hostname
->content
);
194 resized
= ntrealloc(new_permits
, sizeof(struct permitted
) * loaded_lines
);
201 permit_count
= loaded_lines
;
206 int setup_listening_socket(void) {
207 struct sockaddr_in sin
;
209 unsigned int opt
= 1;
211 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
213 /* also shamelessly ripped from proxyscan */
215 nterface_log(nrl
, NL_ERROR
, "Unable to open listening socket (%d).", errno
);
219 if(setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *) &opt
, sizeof(opt
)) != 0) {
220 nterface_log(nrl
, NL_ERROR
, "Unable to set SO_REUSEADDR on listen socket.");
224 /* Initialiase the addresses */
225 memset(&sin
, 0, sizeof(sin
));
226 sin
.sin_family
= AF_INET
;
227 sin
.sin_port
= htons(getcopyconfigitemintpositive("nterfacer", "port", NTERFACER_PORT
));
229 if(bind(fd
, (struct sockaddr
*) &sin
, sizeof(sin
))) {
230 nterface_log(nrl
, NL_ERROR
, "Unable to bind listen socket (%d).", errno
);
236 if(ioctl(fd
, FIONBIO
, &opt
)) {
237 nterface_log(nrl
, NL_ERROR
, "Unable to set listen socket non-blocking.");
244 struct service_node
*register_service(char *name
) {
245 struct service_node
*np
= ntmalloc(sizeof(service_node
));
248 np
->name
= getsstring(name
, strlen(name
));
262 struct handler
*register_handler(struct service_node
*service
, char *command
, int args
, handler_function fp
) {
263 struct handler
*hp
= ntmalloc(sizeof(handler
));
266 hp
->command
= getsstring(command
, strlen(command
));
276 hp
->next
= service
->handlers
;
277 hp
->service
= service
;
278 service
->handlers
= hp
;
283 void deregister_handler(struct handler
*hl
) {
284 struct service_node
*service
= (struct service_node
*)hl
->service
;
285 struct handler
*np
, *lp
= NULL
;
286 for(np
=service
->handlers
;np
;lp
=np
,np
=np
->next
) {
291 service
->handlers
= np
->next
;
299 void deregister_service(struct service_node
*service
) {
300 struct service_node
*sp
, *lp
= NULL
;
301 struct rline
*li
, *pi
= NULL
;
303 for(sp
=tree
;sp
;lp
=sp
,sp
=sp
->next
) {
314 if(!sp
) /* already freed */
317 free_handlers(service
);
320 if(li
->service
== service
) {
334 freesstring(service
->name
);
339 void nterfacer_accept_event(struct esocket
*socket
) {
340 struct sockaddr_in sin
;
341 unsigned int addrsize
= sizeof(sin
);
342 int newfd
= accept(socket
->fd
, (struct sockaddr
*)&sin
, &addrsize
), i
;
343 struct sconnect
*temp
;
344 struct permitted
*item
= NULL
;
345 struct esocket
*newsocket
;
346 unsigned int opt
= 1;
349 nterface_log(nrl
, NL_WARNING
, "Unable to accept nterfacer fd!");
353 if(ioctl(newfd
, FIONBIO
, &opt
)) {
354 nterface_log(nrl
, NL_ERROR
, "Unable to set accepted socket non-blocking.");
358 for(i
=0;i
<permit_count
;i
++) {
359 if(permits
[i
].ihost
== sin
.sin_addr
.s_addr
) {
366 /* Someone needs to figure out how to print the IP :) */
367 nterface_log(nrl
, NL_INFO
, "Unauthorised connection closed");
372 temp
= (struct sconnect
*)ntmalloc(sizeof(struct sconnect
));
379 /* do checks on hostname first */
381 newsocket
= esocket_add(newfd
, ESOCKET_UNIX_DOMAIN_CONNECTED
, &nterfacer_events
, nterfacer_token
);
387 newsocket
->tag
= temp
;
389 nterface_log(nrl
, NL_INFO
, "New connection from %s.", item
->hostname
->content
);
391 temp
->status
= SS_IDLE
;
394 esocket_write_line(newsocket
, "nterfacer " PROTOCOL_VERSION
);
397 void derive_key(unsigned char *out
, char *password
, char *segment
, unsigned char *noncea
, unsigned char *nonceb
, unsigned char *extra
, int extralen
) {
400 SHA256_Update(&c
, (unsigned char *)password
, strlen(password
));
401 SHA256_Update(&c
, (unsigned char *)":", 1);
402 SHA256_Update(&c
, (unsigned char *)segment
, strlen(segment
));
403 SHA256_Update(&c
, (unsigned char *)":", 1);
404 SHA256_Update(&c
, noncea
, 16);
405 SHA256_Update(&c
, (unsigned char *)":", 1);
406 SHA256_Update(&c
, nonceb
, 16);
407 SHA256_Update(&c
, (unsigned char *)":", 1);
408 SHA256_Update(&c
, extra
, extralen
);
409 SHA256_Final(out
, &c
);
412 SHA256_Update(&c
, out
, 32);
413 SHA256_Final(out
, &c
);
416 int nterfacer_line_event(struct esocket
*sock
, char *newline
) {
417 struct sconnect
*socket
= sock
->tag
;
418 char *response
, *theirnonceh
= NULL
, *theirivh
= NULL
;
419 unsigned char theirnonce
[16], theiriv
[16];
422 switch(socket
->status
) {
424 if(strcasecmp(newline
, ANTI_FULL_VERSION
)) {
425 nterface_log(nrl
, NL_INFO
, "Protocol mismatch from %s: %s", socket
->permit
->hostname
->content
, newline
);
428 unsigned char challenge
[32];
429 char ivhex
[16 * 2 + 1], noncehex
[16 * 2 + 1];
431 if(!get_entropy(challenge
, 32) || !get_entropy(socket
->iv
, 16)) {
432 nterface_log(nrl
, NL_ERROR
, "Unable to open challenge/IV entropy bin!");
436 int_to_hex(challenge
, socket
->challenge
, 32);
437 int_to_hex(socket
->iv
, ivhex
, 16);
439 memcpy(socket
->response
, challenge_response(socket
->challenge
, socket
->permit
->password
->content
), sizeof(socket
->response
));
440 socket
->response
[sizeof(socket
->response
) - 1] = '\0'; /* just in case */
442 socket
->status
= SS_VERSIONED
;
443 if(!generate_nonce(socket
->ournonce
, 1)) {
444 nterface_log(nrl
, NL_ERROR
, "Unable to generate nonce!");
447 int_to_hex(socket
->ournonce
, noncehex
, 16);
449 if(esocket_write_line(sock
, "%s %s %s", socket
->challenge
, ivhex
, noncehex
))
455 for(response
=newline
;*response
;response
++) {
456 if((*response
== ' ') && (*(response
+ 1))) {
458 theirivh
= response
+ 1;
464 for(response
=theirivh
;*response
;response
++) {
465 if((*response
== ' ') && (*(response
+ 1))) {
467 theirnonceh
= response
+ 1;
473 if(!theirivh
|| (strlen(theirivh
) != 32) || !hex_to_int(theirivh
, theiriv
, sizeof(theiriv
)) ||
474 !theirnonceh
|| (strlen(theirnonceh
) != 32) || !hex_to_int(theirnonceh
, theirnonce
, sizeof(theirnonce
))) {
475 nterface_log(nrl
, NL_INFO
, "Protocol error drop: %s", socket
->permit
->hostname
->content
);
479 if(!memcmp(socket
->ournonce
, theirnonce
, sizeof(theirnonce
))) {
480 nterface_log(nrl
, NL_INFO
, "Bad nonce drop: %s", socket
->permit
->hostname
->content
);
484 if(!strncasecmp(newline
, socket
->response
, sizeof(socket
->response
))) {
485 unsigned char theirkey
[32], ourkey
[32];
487 derive_key(ourkey
, socket
->permit
->password
->content
, socket
->challenge
, socket
->ournonce
, theirnonce
, (unsigned char *)"SERVER", 6);
489 derive_key(theirkey
, socket
->permit
->password
->content
, socket
->response
, theirnonce
, socket
->ournonce
, (unsigned char *)"CLIENT", 6);
490 nterface_log(nrl
, NL_INFO
, "Authed: %s", socket
->permit
->hostname
->content
);
491 socket
->status
= SS_AUTHENTICATED
;
492 switch_buffer_mode(sock
, ourkey
, socket
->iv
, theirkey
, theiriv
);
494 if(esocket_write_line(sock
, "Oauth"))
497 nterface_log(nrl
, NL_INFO
, "Bad CR drop: %s", socket
->permit
->hostname
->content
);
502 case SS_AUTHENTICATED
:
503 nterface_log(nrl
, NL_INFO
|NL_LOG_ONLY
, "L(%s): %s", socket
->permit
->hostname
->content
, newline
);
504 reason
= nterfacer_new_rline(newline
, sock
, &number
);
506 if(reason
== RE_SOCKET_ERROR
)
508 if(reason
!= RE_BAD_LINE
) {
509 if(esocket_write_line(sock
, "%d,E%d,%s", number
, reason
, request_error(reason
)))
522 int nterfacer_new_rline(char *line
, struct esocket
*socket
, int *number
) {
523 char *sp
, *p
, *parsebuf
= NULL
, *pp
, commandbuf
[MAX_BUFSIZE
], *args
[MAX_ARGS
], *newp
;
525 struct service_node
*service
;
526 struct rline
*prequest
;
530 if(!line
|| !line
[0] || (line
[0] == ','))
533 for(sp
=line
;*sp
;sp
++)
537 if(!*sp
|| !*(sp
+ 1))
542 for(service
=tree
;service
;service
=service
->next
)
543 if(!strcmp(service
->name
->content
, line
))
554 *number
= positive_atoi(sp
+ 1);
556 if((*number
< 1) || (*number
> 0xffff))
560 nterface_log(nrl
, NL_DEBUG
, "Unable to find service: %s", line
);
561 return RE_SERVICER_NOT_FOUND
;
566 for(pp
=p
+1;*pp
;pp
++) {
567 if((*pp
== '\\') && *(pp
+ 1)) {
568 if(*(pp
+ 1) == ',') {
570 } else if(*(pp
+ 1) == '\\') {
574 } else if(*pp
== ',') {
581 if(*pp
== '\0') { /* if we're at the end already, we have no arguments */
584 argcount
= 1; /* we have a comma, so we have one already */
589 for(hl
=service
->handlers
;hl
;hl
=hl
->next
)
590 if(!strncmp(hl
->command
->content
, commandbuf
, sizeof(commandbuf
)))
594 return RE_COMMAND_NOT_FOUND
;
597 parsebuf
= (char *)ntmalloc(strlen(pp
) + 1);
598 MemCheckR(parsebuf
, RE_MEM_ERROR
);
601 for(newp
=args
[0]=parsebuf
,pp
++;*pp
;pp
++) {
602 if((*pp
== '\\') && *(pp
+ 1)) {
603 if(*(pp
+ 1) == ',') {
605 } else if(*(pp
+ 1) == '\\') {
609 } else if(*pp
== ',') {
611 args
[argcount
++] = newp
;
612 if(argcount
> MAX_ARGS
) {
614 return RE_TOO_MANY_ARGS
;
622 if(argcount
< hl
->args
) {
623 if(argcount
&& parsebuf
)
625 return RE_WRONG_ARG_COUNT
;
628 prequest
= (struct rline
*)ntmalloc(sizeof(struct rline
));
631 if(argcount
&& parsebuf
)
636 prequest
->service
= service
;
637 prequest
->handler
= hl
;
638 prequest
->buf
[0] = '\0';
639 prequest
->curpos
= prequest
->buf
;
640 prequest
->tag
= NULL
;
641 prequest
->id
= *number
;
642 prequest
->next
= rlines
;
643 prequest
->socket
= socket
;
646 re
= (hl
->function
)(prequest
, argcount
, args
);
648 if(argcount
&& parsebuf
)
654 void nterfacer_disconnect_event(struct esocket
*sock
) {
655 struct sconnect
*socket
= sock
->tag
;
659 nterface_log(nrl
, NL_INFO
, "Disconnected from %s.", socket
->permit
->hostname
->content
);
662 for(li
=rlines
;li
;li
=li
->next
)
663 if(li
->socket
&& (li
->socket
->tag
== socket
))
669 int ri_append(struct rline
*li
, char *format
, ...) {
670 char buf
[MAX_BUFSIZE
], escapedbuf
[MAX_BUFSIZE
* 2 + 1], *p
, *tp
;
671 int sizeleft
= sizeof(li
->buf
) - (li
->curpos
- li
->buf
);
674 va_start(ap
, format
);
676 if(vsnprintf(buf
, sizeof(buf
), format
, ap
) >= sizeof(buf
)) {
683 for(tp
=escapedbuf
,p
=buf
;*p
||(*tp
='\0');*tp
++=*p
++)
684 if((*p
== ',') || (*p
== '\\'))
688 if(li
->curpos
== li
->buf
) {
689 li
->curpos
+=snprintf(li
->curpos
, sizeleft
, "%s", escapedbuf
);
691 li
->curpos
+=snprintf(li
->curpos
, sizeleft
, ",%s", escapedbuf
);
695 if(sizeof(li
->buf
) - (li
->curpos
- li
->buf
) > 0) {
702 int ri_error(struct rline
*li
, int error_code
, char *format
, ...) {
703 char buf
[MAX_BUFSIZE
], escapedbuf
[MAX_BUFSIZE
* 2 + 1], *p
, *tp
;
704 struct rline
*pp
, *lp
= NULL
;
709 va_start(ap
, format
);
710 vsnprintf(buf
, sizeof(buf
), format
, ap
);
713 for(tp
=escapedbuf
,p
=buf
;*p
||(*tp
='\0');*tp
++=*p
++)
714 if((*p
== ',') || (*p
== '\\'))
717 if(esocket_write_line(li
->socket
, "%d,OE%d,%s", li
->id
, error_code
, escapedbuf
))
718 retval
= RE_SOCKET_ERROR
;
721 for(pp
=rlines
;pp
;lp
=pp
,pp
=pp
->next
) {
736 int ri_final(struct rline
*li
) {
737 struct rline
*pp
, *lp
= NULL
;
741 if(esocket_write_line(li
->socket
, "%d,OO%s", li
->id
, li
->buf
))
742 retval
= RE_SOCKET_ERROR
;
744 for(pp
=rlines
;pp
;lp
=pp
,pp
=pp
->next
) {
759 int ping_handler(struct rline
*ri
, int argc
, char **argv
) {