-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathirc.c
More file actions
1210 lines (1052 loc) · 32.7 KB
/
irc.c
File metadata and controls
1210 lines (1052 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LIRC - IRC Client Library for C
*
* Copyright (C) 2023, Naveen Albert
*
* Naveen Albert <bbs@phreaknet.org>
*
* This library is free software, distributed under the terms of
* the GNU Lesser General Public License Version 2.1. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief TLS/SASL-capable IRC client library
*
* \author Naveen Albert <bbs@phreaknet.org>
*/
#define _GNU_SOURCE 1
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> /* use sockaddr_in */
#include <netdb.h>
#include <arpa/inet.h>
/* Compile the library with TLS support, using OpenSSL */
#define HAVE_OPENSSL
#ifdef HAVE_OPENSSL
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifndef ROOT_CERT_PATH
#define ROOT_CERT_PATH "/etc/ssl/certs/ca-certificates.crt" /* Debian */
#endif /* ROOT_CERT_PATH */
#endif /* HAVE_OPENSSL */
#include <assert.h>
#define EXPOSE_IRC_MSG
/* Compiling without the headers installed in the system directories yet,
* just use a relative path since it should be in the same directory. */
#include "irc.h"
/*! \brief A client for one IRC server. Use multiple clients for multiple servers or for multiple clients on the same server */
struct irc_client {
int sfd; /*!< Client socket file descriptor */
#ifdef HAVE_OPENSSL
SSL* ssl;
SSL_CTX* ctx;
#endif
const char *hostname; /*!< IRC server hostname */
unsigned int port; /*!< IRC server port */
const char *username; /*!< IRC client username */
const char *password; /*!< IRC client password */
char *nickname; /*!< IRC client nickname */
char *autojoin; /*!< Comma-separated list of channels to autojoin */
/* Flags */
unsigned int tls:1; /*!< Whether to use TLS */
unsigned int tlsverify:1; /*!< Whether to verify the server */
unsigned int sasl:1; /*!< Whether to use SASL authentication */
/* Internal */
unsigned int active:1; /*!< Whether client is currently actively connected to a server */
/* Flexible Struct Member */
char data[];
};
static void (*log_callback)(enum irc_log_level level, int sublevel, const char *file, int line, const char *func, const char *msg) = NULL;
void irc_log_callback(void (*callback)(enum irc_log_level level, int sublevel, const char *file, int line, const char *func, const char *msg))
{
log_callback = callback;
}
#define irc_err(fmt, ...) __irc_log(IRC_LOG_ERR, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define irc_warn(fmt, ...) __irc_log(IRC_LOG_WARN, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define irc_info(fmt, ...) __irc_log(IRC_LOG_INFO, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define irc_verbose(fmt, ...) __irc_log(IRC_LOG_VERBOSE, 0, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
#define irc_debug(level, fmt, ...) __irc_log(IRC_LOG_DEBUG, level, __FILE__, __LINE__, __FUNCTION__, fmt, ## __VA_ARGS__)
static void __attribute__ ((format (gnu_printf, 6, 7))) __irc_log(enum irc_log_level level, int sublevel, const char *file, int line, const char *func, const char *fmt, ...)
{
char *buf = NULL;
int len = 0;
va_list ap;
if (!log_callback) {
return;
}
/* Action Name and ID */
va_start(ap, fmt);
len = vasprintf(&buf, fmt, ap);
va_end(ap);
if (len < 0 || !buf) {
return; /* Can't log */
}
log_callback(level, sublevel, file, line, func, buf);
free(buf);
}
struct irc_client *irc_client_new(const char *hostname, unsigned int port, const char *username, const char *password)
{
struct irc_client *client;
size_t hostlen, userlen, passlen;
if (!hostname) {
irc_err("Missing hostname\n");
return NULL;
} else if (!username) {
irc_err("Missing username\n");
return NULL;
} else if (!password) {
irc_err("Missing password\n");
return NULL;
}
hostlen = strlen(hostname);
userlen = strlen(username);
passlen = strlen(password);
/* Use a single allocation rather than strdup'ing each field */
client = calloc(1, sizeof(*client) + hostlen + userlen + passlen + 3); /* 3 NULs */
if (!client) {
irc_err("calloc failed\n");
return NULL;
}
client->port = port;
client->sfd = -1;
client->hostname = client->data;
strcpy(client->data, hostname); /* Safe */
client->username = client->hostname + hostlen + 1;
strcpy(client->data + hostlen + 1, username); /* Safe */
client->password = client->hostname + hostlen + userlen + 2;
strcpy(client->data + hostlen + 1 + userlen + 1, password); /* Safe */
client->nickname = strdup(client->username); /* Default nick to username */
return client;
}
void irc_client_destroy(struct irc_client *client)
{
#ifdef HAVE_OPENSSL
if (client->ssl) {
SSL_shutdown(client->ssl);
SSL_free(client->ssl);
client->ssl = NULL;
}
if (client->ctx) {
SSL_CTX_free(client->ctx);
client->ctx = NULL;
}
#endif
if (client->sfd != -1) { /* If a client creates a client but never connects, this will be -1 at destroy time */
close(client->sfd);
client->sfd = -1;
}
if (client->autojoin) { /* If we added an autojoin but never actually authenticated, then this will still be set */
free(client->autojoin);
}
if (client->nickname) {
free(client->nickname);
}
free(client);
}
const char *irc_client_hostname(struct irc_client *client)
{
return client->hostname;
}
const char *irc_client_username(struct irc_client *client)
{
return client->username;
}
const char *irc_client_nickname(struct irc_client *client)
{
return client->nickname;
}
int irc_client_connected(struct irc_client *client)
{
return client->active;
}
int irc_client_autojoin(struct irc_client *client, const char *autojoin)
{
if (client->autojoin) {
free(client->autojoin);
}
if (!autojoin) {
return 0; /* Deleting autojoin */
}
client->autojoin = strdup(autojoin);
return client->autojoin ? 0 : -1;
}
#define SET_FLAG_IF_SET(client, flags, flag, flagname) \
if (flags & flagname) { \
irc_debug(6, "Client %p %s %d -> %d\n", client, #flagname, client->tls, 1); \
client->flag = 1; \
} else if (flags & ~flagname) { \
irc_debug(6, "Client %p %s %d -> %d\n", client, #flagname, client->tls, 0); \
client->flag = 0; \
}
int irc_client_set_flags(struct irc_client *client, int flags)
{
SET_FLAG_IF_SET(client, flags, tls, IRC_CLIENT_USE_TLS);
SET_FLAG_IF_SET(client, flags, tlsverify, IRC_CLIENT_VERIFY_SERVER);
SET_FLAG_IF_SET(client, flags, sasl, IRC_CLIENT_USE_SASL);
#ifndef HAVE_OPENSSL
if (client->tls) {
client->tls = 0;
irc_err("TLS is not supported (OpenSSL unavailable)\n");
return -1;
}
#endif
if (client->sasl && !client->tls) {
irc_warn("SASL authentication without TLS is not secure\n");
}
if (client->tlsverify && !client->tls) {
client->tlsverify = 0;
irc_err("Cannot verify server when TLS is disabled\n");
return -1;
}
return 0;
}
int irc_client_connect(struct irc_client *client)
{
char ip[256];
int e;
struct addrinfo hints, *res, *ai;
struct sockaddr_in *saddr_in; /* IPv4 */
struct sockaddr_in6 *saddr_in6; /* IPv6 */
if (client->sfd != -1) {
irc_err("IRC client %p is currently connected (socket fd %d)\n", client, client->sfd);
return -1;
}
if (!client->port) {
client->port = client->tls ? IRC_DEFAULT_TLS_PORT : IRC_DEFAULT_PORT;
}
/* Resolve the hostname */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* TCP */
e = getaddrinfo(client->hostname, NULL, &hints, &res);
if (e) {
irc_err("getaddrinfo (%s): %s\n", client->hostname, gai_strerror(e));
return -1;
}
for (ai = res; ai; ai = ai->ai_next) {
ip[0] = '\0'; /* Avoid possibly uninitialized usage warning */
if (ai->ai_family == AF_INET) {
saddr_in = (struct sockaddr_in *) ai->ai_addr;
saddr_in->sin_port = htons((uint16_t) client->port);
inet_ntop(ai->ai_family, &saddr_in->sin_addr, ip, sizeof(ip)); /* Print IPv4*/
} else if (ai->ai_family == AF_INET6) {
saddr_in6 = (struct sockaddr_in6 *) ai->ai_addr;
saddr_in6->sin6_port = htons((uint16_t) client->port);
inet_ntop(ai->ai_family, &saddr_in6->sin6_addr, ip, sizeof(ip)); /* Print IPv6 */
}
if (!strcmp(ip, "130.185.232.126")) {
continue; /* Bad IP for libera chat */
}
client->sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (client->sfd == -1) {
irc_err("socket: %s\n", strerror(errno));
continue;
}
irc_info("Attempting %s connection to %s:%d\n", client->tls ? "secure" : "insecure", ip, client->port);
if (connect(client->sfd, ai->ai_addr, ai->ai_addrlen)) {
irc_err("connect: %s\n", strerror(errno));
close(client->sfd);
client->sfd = -1;
continue;
}
break; /* Use the 1st one that works */
}
freeaddrinfo(res);
if (client->sfd == -1) {
return -1;
}
irc_debug(1, "Connected to %s:%d\n", client->hostname, client->port);
#ifdef HAVE_OPENSSL
if (client->tls) {
X509 *server_cert;
long verify_result;
char *str;
OpenSSL_add_ssl_algorithms();
SSL_load_error_strings();
client->ctx = SSL_CTX_new(TLS_client_method());
if (!client->ctx) {
irc_err("Failed to setup new SSL context\n");
return -1;
}
SSL_CTX_set_verify(client->ctx, SSL_VERIFY_PEER, NULL);
SSL_CTX_load_verify_locations(client->ctx, ROOT_CERT_PATH, NULL);
SSL_CTX_set_options(client->ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); /* Only use TLS */
client->ssl = SSL_new(client->ctx);
if (!client->ssl) {
irc_err("Failed to create new SSL\n");
SSL_CTX_free(client->ctx);
client->ctx = NULL;
return -1;
}
if (SSL_set_fd(client->ssl, client->sfd) != 1) {
irc_err("Failed to connect SSL: %s\n", ERR_error_string(ERR_get_error(), NULL));
goto sslcleanup;
}
if (SSL_connect(client->ssl) == -1) {
irc_err("Failed to connect SSL: %s\n", ERR_error_string(ERR_get_error(), NULL));
goto sslcleanup;
}
/* Verify cert */
server_cert = SSL_get_peer_certificate(client->ssl);
if (!server_cert) {
irc_err("Failed to get peer certificate\n");
goto sslcleanup;
}
str = X509_NAME_oneline(X509_get_subject_name(server_cert), 0, 0);
if (!str) {
irc_err("Failed to get peer certificate\n");
goto sslcleanup;
}
irc_debug(8, "TLS SN: %s\n", str);
OPENSSL_free(str);
str = X509_NAME_oneline(X509_get_issuer_name (server_cert), 0, 0);
if (!str) {
irc_err("Failed to get peer certificate\n");
goto sslcleanup;
}
irc_debug(8, "TLS Issuer: %s\n", str);
OPENSSL_free(str);
X509_free(server_cert);
verify_result = SSL_get_verify_result(client->ssl);
if (verify_result != X509_V_OK) {
if (client->tlsverify) {
irc_err("SSL verify failed: %ld (%s)\n", verify_result, X509_verify_cert_error_string(verify_result));
goto sslcleanup; /* If told to verify, then this is fatal */
}
irc_warn("SSL verify failed: %ld (%s)\n", verify_result, X509_verify_cert_error_string(verify_result));
} else {
irc_debug(4, "TLS verification successful\n");
}
}
#endif
client->active = 1;
return 0;
sslcleanup:
#ifdef HAVE_OPENSSL
SSL_CTX_free(client->ctx);
SSL_free(client->ssl);
client->ctx = NULL;
client->ssl = NULL;
#endif
close(client->sfd);
return -1;
}
void irc_loop(struct irc_client *client, FILE *logfile, void (*cb)(void *data, struct irc_msg *msg), void *data)
{
ssize_t res = 0;
char readbuf[IRC_MAX_MSG_LEN + 1];
struct irc_msg msg;
char *prevbuf, *mybuf = readbuf;
size_t prevlen, mylen = sizeof(readbuf) - 1;
char *start, *eom;
start = readbuf;
for (;;) {
begin:
if (mylen <= 1) {
/* IRC max message is 512, but we could have received multiple messages in one read() */
char *a;
/* Shift current message to beginning of the whole buffer */
for (a = readbuf; *start; a++, start++) {
*a = *start;
}
*a = '\0';
mybuf = a;
mylen = sizeof(readbuf) - 1 - (size_t) (mybuf - readbuf);
start = readbuf;
if (mylen <= 1) { /* Couldn't shift, whole buffer was full */
/* Could happen but this would not be valid. Abort read and reset. */
irc_err("Buffer truncation!\n");
start = readbuf;
mybuf = readbuf;
mylen = sizeof(readbuf) - 1;
}
}
/* Wait for data from server */
if (res != sizeof(readbuf) - 1) {
/* XXX We don't poll if we read() into an entirely full buffer and there's still more data to read.
* poll() won't return until there's even more data (but it feels like it should). */
res = irc_poll(client, -1, -1);
if (res <= 0) {
break;
}
}
prevbuf = mybuf;
prevlen = mylen;
res = irc_read(client, mybuf, mylen);
if (res <= 0) {
break;
}
mybuf[res] = '\0'; /* Safe */
do {
eom = strstr(mybuf, "\r\n");
if (!eom) {
/* read returned incomplete message */
mybuf = prevbuf + res;
mylen = prevlen - (size_t) res;
goto begin; /* In a double loop, can't continue */
}
/* Got more than one message? */
if (*(eom + 2)) {
*(eom + 1) = '\0'; /* Null terminate before the next message starts */
}
memset(&msg, 0, sizeof(msg));
if (logfile) {
fprintf(logfile, "%s\n", start); /* Append to log file */
}
if (!irc_parse_msg(&msg, start) && !irc_parse_msg_type(&msg)) {
cb(data, &msg);
}
mylen -= (unsigned long) (eom + 2 - mybuf);
start = mybuf = eom + 2;
} while (mybuf && *mybuf);
start = mybuf = readbuf; /* Reset to beginning */
mylen = sizeof(readbuf) - 1;
}
}
int irc_disconnect(struct irc_client *client)
{
return shutdown(client->sfd, SHUT_RDWR);
}
int irc_poll(struct irc_client *client, int ms, int fd)
{
int res;
struct pollfd pfds[2];
assert(client->sfd != -1);
pfds[0].fd = client->sfd;
pfds[0].events = POLLIN;
if (fd != -1) {
pfds[1].fd = fd;
pfds[1].events = POLLIN;
}
for (;;) {
pfds[0].revents = 0;
pfds[1].revents = 0;
res = poll(pfds, fd == -1 ? 1 : 2, ms);
if (res < 0) {
if (errno == EINTR) {
continue;
}
irc_err("poll returned error: %s\n", strerror(errno));
client->active = 0;
}
if (pfds[0].revents & POLLIN) {
return 1;
} else if (pfds[1].revents & POLLIN) {
return 2;
}
if (pfds[0].revents) {
irc_debug(1, "Exceptional poll activity on client fd\n");
} else if (pfds[1].revents) {
irc_debug(1, "Exceptional poll activity on custom fd\n");
} else {
return 0; /* Nothing happened */
}
break;
}
return -1;
}
ssize_t irc_read(struct irc_client *client, char *buf, size_t len)
{
ssize_t bytes;
#ifdef HAVE_OPENSSL
if (client->tls) {
bytes = SSL_read(client->ssl, buf, len);
} else
#endif
{
bytes = read(client->sfd, buf, len);
}
if (bytes > 0) {
irc_debug(10, "<= %s %.*s", irc_client_hostname(client), (int) bytes, buf); /* Should already end in LF, additional one not needed */
} else {
irc_debug(1, "read returned %ld%s%s\n", bytes, bytes == -1 ? ": " : "", bytes == -1 ? strerror(errno) : "");
client->active = 0;
}
return bytes;
}
ssize_t irc_write(struct irc_client *client, const char *buf, size_t len)
{
const char *origbuf = buf;
size_t origlen = len;
size_t written = 0;
/* All IRC commands must end in CR LF. If not, the command will fail. */
if (len < 2 || *(buf + len - 2) != '\r' || *(buf + len - 1) != '\n') {
irc_err("Message '%.*s' does not end in CR LF\n", (int) len, buf);
return -1;
}
while (len > 0) {
ssize_t res;
#ifdef HAVE_OPENSSL
if (client->tls) {
res = SSL_write(client->ssl, buf, len);
} else
#endif
{
res = write(client->sfd, buf, len);
}
if (res <= 0) {
written = res;
break;
}
buf += res;
len -= res;
written += res;
}
if (written <= 0 || written != origlen) {
irc_debug(1, "write returned %ld\n", written);
}
irc_debug(10, "=> %s [%lu] %.*s", irc_client_hostname(client), origlen, (int) origlen, origbuf); /* Don't add our own LF at the end, the message already ends in one */
return written;
}
#define IRC_SEND_FIXED(client, s) irc_write(client, s "\r\n", strlen(s "\r\n"))
ssize_t __attribute__ ((format (gnu_printf, 2, 3))) irc_write_fmt(struct irc_client *client, const char *fmt, ...)
{
ssize_t res;
char buf[IRC_MAX_MSG_LEN + 1];
int len = 0;
va_list ap;
assert(client->sfd != -1);
if (!strstr(fmt, "\r\n")) {
irc_err("Format string '%s' does not end in CR LF\n", fmt);
return -1;
}
/* Action Name and ID */
va_start(ap, fmt);
/* No need for dynamic allocation, since a valid command is at most 512 bytes. */
len = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (len >= (int) sizeof(buf)) {
irc_warn("Truncation occured trying to send %d-byte command\n", len);
}
res = irc_write(client, buf, (size_t) len);
return res;
}
static char encoding_table[] =
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
static int mod_table[] = {0, 2, 1};
/*! \brief Based on https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c/6782480#6782480 */
static char *base64_encode(const char *data, int input_length, int *outlen)
{
char *encoded_data;
int i, j, output_len;
output_len = 4 * ((input_length + 2) / 3);
encoded_data = malloc((size_t) output_len);
if (!encoded_data) {
return NULL;
}
for (i = 0, j = 0; i < input_length; ) {
uint32_t octet_a = i < input_length ? (unsigned char) data[i++] : 0;
uint32_t octet_b = i < input_length ? (unsigned char) data[i++] : 0;
uint32_t octet_c = i < input_length ? (unsigned char) data[i++] : 0;
uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (i = 0; i < mod_table[input_length % 3]; i++) {
encoded_data[output_len - 1 - i] = '=';
}
*outlen = output_len;
return encoded_data;
}
int irc_client_auth(struct irc_client *client, const char *username, const char *password, const char *realname)
{
int res = 0;
if (strchr(username, ' ')) {
irc_err("IRC username %s is invalid\n", username);
return -1;
}
/* PASS must be sent before both USER and JOIN, if it exists */
if (password && *password) {
res |= irc_send(client, "PASS %s", password); /* Password, if applicable (not actually used all that much) */
}
/* Confused about the difference between the two? See https://stackoverflow.com/questions/31666247/ */
res |= irc_send(client, "NICK %s", username); /* Actual IRC nickname */
res |= irc_send(client, "USER %s 0 * :%s", username, realname ? realname : username); /* User part of hostmask, mode, unused, real name for WHOIS */
/* If we didn't already have a nickname set, set it now. */
if (!*client->nickname) {
irc_client_set_nick(client, username);
}
return res;
}
static int irc_client_nickserv_login(struct irc_client *client, const char *username, const char *password)
{
int res = 0;
if (strchr(username, ' ')) {
irc_err("IRC username %s is invalid\n", username);
return -1;
}
if (strchr(password, ' ')) {
irc_err("IRC password is invalid\n");
return -1;
}
/* Confused about the difference between the two? See https://stackoverflow.com/questions/31666247/ */
res |= irc_send(client, "PRIVMSG NickServ :IDENTIFY %s %s", username, password); /* Actual IRC nickname */
return res;
}
static int wait_for_response(struct irc_client *client, char *buf, size_t len, int ms, const char *s)
{
ssize_t bytes;
for (;;) {
int pres;
pres = irc_poll(client, ms, -1);
if (pres <= 0) {
return -1;
}
/* Read it into the caller's buffer, so that if further checks
* need to be done when we return, they can be done. */
bytes = irc_read(client, buf, len - 1);
if (bytes <= 0) {
return -1;
}
/* NUL terminate so we can use strstr */
buf[bytes] = '\0'; /* Safe */
/* Print out whatever we receive, typically these are NOTICE messages.
* If it already has a newline, don't add one, but if it doesn't, do. */
irc_verbose("%s%s", buf, strchr(buf, '\n') ? "" : "\n");
if (strstr(buf, s)) {
return 0;
}
}
return -1;
}
static int do_sasl_auth(struct irc_client *client)
{
int res, len, outlen;
char *encoded;
char decoded[256];
char readbuf[256];
/* General References:
* https://ircv3.net/specs/extensions/sasl-3.1.html */
IRC_SEND_FIXED(client, "CAP LS 302"); /* Begin capability negotiation */
if (irc_client_auth(client, client->username, NULL, NULL)) { /* Immediately send NICK and USER (but not PASS) */
return -1;
}
if (wait_for_response(client, readbuf, sizeof(readbuf), 10000, "CAP * LS")) { /* Wait for CAP * LS response */
return -1;
}
/*! \todo This is hardcoded to use PLAIN, we should properly support other modes as well
* For now this is a fine starting point, since with TLS this is not insecure. */
if (!strstr(readbuf, "multi-prefix sasl") || !strstr(readbuf, "PLAIN")) {
irc_err("Server does not support PLAIN authentication\n");
return -1;
}
IRC_SEND_FIXED(client, "CAP REQ :multi-prefix sasl");
if (wait_for_response(client, readbuf, sizeof(readbuf), 5000, "ACK")) { /* ACK :multi-prefix sasl */
return -1;
}
IRC_SEND_FIXED(client, "AUTHENTICATE PLAIN"); /* This is secure if the connection is using TLS */
if (wait_for_response(client, readbuf, sizeof(readbuf), 5000, "AUTHENTICATE +")) { /* Expect: AUTHENTICATE + */
return -1;
}
/* Plain SASL: https://www.rfc-editor.org/rfc/rfc4616.html
* Base64 encode authentication identity, authorization identity, password (nick, name, password, separated by NUL, but not ending in it) */
len = snprintf(decoded, sizeof(decoded), "%s%c%s%c%s", client->username, '\0', client->username, '\0', client->password);
encoded = base64_encode(decoded, len, &outlen);
if (!encoded) {
irc_err("base64 encoding failed\n");
return -1;
}
res = irc_send(client, "AUTHENTICATE %.*s", outlen, encoded);
free(encoded);
if (res || wait_for_response(client, readbuf, sizeof(readbuf), 5000, "903")) { /* Expect: 903... SASL authentication successful */
return -1;
}
/* End capability negotiation */
IRC_SEND_FIXED(client, "CAP END");
return 0;
}
static int do_autojoin(struct irc_client *client)
{
char *next, *all = client->autojoin;
while ((next = strsep(&all, ","))) {
irc_client_channel_join(client, next);
}
free(client->autojoin); /* This string has been eaten by strsep anyways, it's no longer useful */
client->autojoin = NULL;
return 0;
}
int irc_client_login(struct irc_client *client)
{
if (client->sasl) { /* Some IRC servers require SASL from certain IPs to mitigate spam. */
irc_debug(3, "Performing SASL authentication\n");
if (do_sasl_auth(client)) {
irc_err("SASL authentication failed\n");
return -1;
}
} else if (client->password) { /* Authenticate to NickServ if we have a password */
char readbuf[256];
irc_debug(3, "Performing NickServ authentication\n");
/* We can skip this if we authenticated with SASL, since SASL will log in us */
if (irc_client_nickserv_login(client, client->username, client->password)) {
irc_err("Failed to authenticate for username %s\n", client->username);
return -1;
}
if (wait_for_response(client, readbuf, sizeof(readbuf), 5000, "You are now logged in")) { /* Expect: 900... You are now logged in as... */
irc_err("Failed to get authentication response from NickServ\n");
return -1;
}
} else {
irc_err("Cannot authenticate in current state\n");
return -1;
}
irc_info("Logged in to %s as %s successfully\n", client->hostname, client->username);
/* Don't join any channels until we're fully logged in.
* This ensures that if we have a cloak, it gets applied, so we don't leak our IP address to other clients. */
if (client->autojoin) {
do_autojoin(client);
}
return 0;
}
/* A valid channel name starts with a symbol, not directly with an alphanumeric character */
#define VALID_CHANNEL_NAME(c) (!isalnum(*c))
int irc_client_channel_join(struct irc_client *client, const char *channel)
{
if (!channel) {
irc_err("Missing channel name(s)\n");
return -1;
}
if (!VALID_CHANNEL_NAME(channel)) {
irc_err("Channel name '%s' is invalid, must begin with a symbol\n", channel);
return -1;
}
return irc_send(client, "JOIN %s", channel);
}
int irc_client_channel_leave(struct irc_client *client, const char *channel)
{
if (!channel) {
irc_err("Missing channel name(s)\n");
return -1;
}
if (!VALID_CHANNEL_NAME(channel)) {
irc_err("Channel name '%s' is invalid, must begin with a symbol\n", channel);
return -1;
}
return irc_send(client, "PART %s", channel);
}
int irc_client_quit(struct irc_client *client, const char *msg)
{
return irc_send(client, "QUIT :%s", msg ? msg : "");
}
int irc_client_msg(struct irc_client *client, const char *channel, const char *msg)
{
/* When the last parameter is prefixed with a colon character,
* the value of that parameter will be the remainder of the message (including space characters)
* http://chi.cs.uchicago.edu/chirc/irc.html */
return irc_send(client, "PRIVMSG %s :%s", channel, msg);
}
int irc_client_notice(struct irc_client *client, const char *channel, const char *msg)
{
return irc_send(client, "NOTICE %s :%s", channel, msg);
}
int irc_client_ping(struct irc_client *client, const char *pingmsg)
{
return irc_send(client, "PING :%s", pingmsg);
}
int irc_client_pong(struct irc_client *client, struct irc_msg *msg)
{
/* Reply with the same data that it sent us (some servers may actually require that) */
return irc_send(client, "PONG :%s", irc_msg_body(msg) ? irc_msg_body(msg) + 1 : ""); /* If there's a body, skip the : and bounce the rest back */
}
const char *irc_ctcp_name(enum irc_ctcp_type ctcp)
{
switch (ctcp) {
case CTCP_ACTION:
return "ACTION";
case CTCP_VERSION:
return "VERSION";
case CTCP_TIME:
return "TIME";
case CTCP_PING:
return "PING";
case CTCP_DCC:
return "DCC";
default:
break;
}
return NULL;
}
enum irc_ctcp_type irc_ctcp_from_string(const char *s)
{
if (!strcasecmp(s, "ACTION")) {
return CTCP_ACTION;
} else if (!strcasecmp(s, "VERSION")) {
return CTCP_VERSION;
} else if (!strcasecmp(s, "TIME")) {
return CTCP_TIME;
} else if (!strcasecmp(s, "PING")) {
return CTCP_PING;
} else if (!strcasecmp(s, "DCC")) {
return CTCP_DCC;
} else {
irc_warn("Unknown CTCP code: %s\n", s);
return CTCP_UNKNOWN;
}
}
int irc_client_ctcp_request(struct irc_client *client, const char *user, enum irc_ctcp_type ctcp)
{
const char *msg, *ctcp_name = irc_ctcp_name(ctcp);
if (!ctcp_name) {
irc_err("Unknown CTCP command\n");
return -1;
}
switch (ctcp) {
case CTCP_ACTION:
irc_err("Use irc_client_action instead\n");
return -1;
case CTCP_PING:
msg = "123456789";
break;
case CTCP_TIME:
case CTCP_VERSION:
case CTCP_DCC:
default:
msg = NULL;
break;
}
return irc_send(client, "PRIVMSG %s :" "\x01" "%s%s%s" "\x01", user, ctcp_name, msg ? " " : "", msg ? msg : "");
}
int irc_client_ctcp_reply(struct irc_client *client, const char *username, enum irc_ctcp_type ctcp, const char *data)
{
const char *ctcp_name = irc_ctcp_name(ctcp);
if (!ctcp_name) {
irc_err("Unknown CTCP command\n");
return -1;
}
return irc_send(client, "NOTICE %s :" "\x01" "%s%s%s" "\x01", username, ctcp_name, data ? " " : "", data ? data : "");
}
int irc_client_action(struct irc_client *client, const char *channel, const char *msg)
{
/* CTCP ACTION command
* https://www.irchelp.org/protocol/ctcpspec.html */
return irc_send(client, "PRIVMSG %s :" "\x01" "ACTION %s" "\x01", channel, msg); /* Keep the \001's separate to avoid escaping subsequent text */
}
int irc_client_change_nick(struct irc_client *client, const char *nick)
{
return irc_send(client, "NICK %s", nick);
}
int irc_client_set_nick(struct irc_client *client, const char *nick)
{
if (client->nickname) {
free(client->nickname);
}
client->nickname = strdup(nick);
return client->nickname ? 0 : -1;
}
int irc_client_set_channel_topic(struct irc_client *client, const char *channel, const char *topic)
{
return irc_send(client, "TOPIC %s :%s", channel, topic);
}
int irc_client_list_channels(struct irc_client *client, const char *channels)
{
return irc_send(client, "LIST %s%s", channels && *channels ? ":" : "", channels);
}
int irc_client_invite_user(struct irc_client *client, const char *nickname, const char *channel)
{
return irc_send(client, "INVITE %s %s", nickname, channel);
}
#define PARSE_CHANNEL() \
/* Format of msg->body here is CHANNEL :BODY */ \
msg->channel = strsep(&msg->body, " "); \
if (msg->body && *msg->body == ':') { \
msg->body++; /* Skip : */ \