~rcr/rirc

ref: 35db64b7273041f047de1910b48be9473ec06c0e rirc/src/handlers/ircv3.c -rw-r--r-- 15.0 KiB
35db64b7 — Richard Robbins add disconnect on CAP, AUTHENTICATE errors during registration 3 months ago
                                                                                
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
#include "src/handlers/ircv3.h"

#include "src/io.h"
#include "src/state.h"

#include "mbedtls/base64.h"

#include <string.h>

#define failf(S, ...) \
	do { server_error((S), __VA_ARGS__); \
	     return 1; \
	} while (0)

#define sendf(S, ...) \
	do { int ret; \
	     if ((ret = io_sendf((S)->connection, __VA_ARGS__))) \
	         failf((S), "Send fail: %s", io_err(ret)); \
	} while (0)

#define IRCV3_RECV_HANDLERS \
	X(LIST) \
	X(LS) \
	X(ACK) \
	X(NAK) \
	X(DEL) \
	X(NEW)

#define X(CMD) \
static int ircv3_recv_cap_##CMD(struct server*, struct irc_message*);
IRCV3_RECV_HANDLERS
#undef X

static int ircv3_cap_req_count(struct ircv3_caps*);
static int ircv3_cap_req_send(struct ircv3_caps*, struct server*);
static int ircv3_registered(struct server*);
static int ircv3_sasl_init(struct server*);

static int ircv3_recv_AUTHENTICATE_EXTERNAL(struct server*, struct irc_message*);
static int ircv3_recv_AUTHENTICATE_PLAIN(struct server*, struct irc_message*);

int
ircv3_recv_AUTHENTICATE(struct server *s, struct irc_message *m)
{
	if (s->ircv3_sasl.method == IRCV3_SASL_METHOD_NONE)
		failf(s, "AUTHENTICATE: no SASL method");

	if (s->ircv3_sasl.method == IRCV3_SASL_METHOD_EXTERNAL)
		return (ircv3_recv_AUTHENTICATE_EXTERNAL(s, m));

	if (s->ircv3_sasl.method == IRCV3_SASL_METHOD_PLAIN)
		return (ircv3_recv_AUTHENTICATE_PLAIN(s, m));

	fatal("unknown SASL authentication method");

	return 0;
}

int
ircv3_recv_CAP(struct server *s, struct irc_message *m)
{
	char *targ;
	char *cmnd;

	if (!irc_message_param(m, &targ))
		failf(s, "CAP: target is null");

	if (!irc_message_param(m, &cmnd))
		failf(s, "CAP: command is null");

	#define X(CMD) \
	if (!strcmp(cmnd, #CMD)) \
		return ircv3_recv_cap_##CMD(s, m);
	IRCV3_RECV_HANDLERS
	#undef X

	failf(s, "CAP: unrecognized subcommand '%s'", cmnd);
}

int
ircv3_numeric_900(struct server *s, struct irc_message *m)
{
	/* <nick>!<ident>@<host> <account> :You are now logged in as <user> */

	char *account;
	char *message;
	char *nick;

	if (!irc_message_param(m, &nick))
		failf(s, "RPL_LOGGEDIN: missing nick");

	if (!irc_message_param(m, &account))
		failf(s, "RPL_LOGGEDIN: missing account");

	irc_message_param(m, &message);

	if (message && *message)
		server_info(s, "SASL success: %s", message);
	else
		server_info(s, "SASL success: you are logged in as %s", account);

	return 0;
}

int
ircv3_numeric_901(struct server *s, struct irc_message *m)
{
	/* <nick>!<ident>@<host> :You are now logged out */

	char *message;
	char *nick;

	if (!irc_message_param(m, &nick))
		failf(s, "RPL_LOGGEDOUT: missing nick");

	irc_message_param(m, &message);

	if (message && *message)
		server_info(s, "%s", message);
	else
		server_info(s, "You are now logged out");

	return 0;
}

int
ircv3_numeric_902(struct server *s, struct irc_message *m)
{
	/* :You must use a nick assigned to you */

	char *message;

	irc_message_param(m, &message);

	if (message && *message)
		server_error(s, "%s", message);
	else
		server_error(s, "You must use a nick assigned to you");

	s->ircv3_sasl.state = IRCV3_SASL_STATE_NONE;

	if (!s->registered)
		io_dx(s->connection);

	return 0;
}

int
ircv3_numeric_903(struct server *s, struct irc_message *m)
{
	/* :SASL authentication successful */

	UNUSED(m);

	s->ircv3_sasl.state = IRCV3_SASL_STATE_AUTHENTICATED;

	if (ircv3_registered(s))
		sendf(s, "CAP END");

	return 0;
}

int
ircv3_numeric_904(struct server *s, struct irc_message *m)
{
	/* :SASL authentication failed */

	char *message;

	irc_message_param(m, &message);

	if (message && *message)
		server_error(s, "%s", message);
	else
		server_error(s, "SASL authentication failed");

	s->ircv3_sasl.state = IRCV3_SASL_STATE_NONE;

	if (!s->registered)
		io_dx(s->connection);

	return 0;
}

int
ircv3_numeric_905(struct server *s, struct irc_message *m)
{
	/* :SASL message too long */

	char *message;

	irc_message_param(m, &message);

	if (message && *message)
		server_error(s, "%s", message);
	else
		server_error(s, "SASL message too long");

	s->ircv3_sasl.state = IRCV3_SASL_STATE_NONE;

	if (!s->registered)
		io_dx(s->connection);

	return 0;
}

int
ircv3_numeric_906(struct server *s, struct irc_message *m)
{
	/* :SASL authentication aborted */

	char *message;

	irc_message_param(m, &message);

	if (message && *message)
		server_error(s, "%s", message);
	else
		server_error(s, "SASL authentication aborted");

	s->ircv3_sasl.state = IRCV3_SASL_STATE_NONE;

	if (!s->registered)
		io_dx(s->connection);

	return 0;
}

int
ircv3_numeric_907(struct server *s, struct irc_message *m)
{
	/* :You have already authenticated using SASL */

	char *message;

	irc_message_param(m, &message);

	if (message && *message)
		server_error(s, "%s", message);
	else
		server_error(s, "You have already authenticated using SASL");

	s->ircv3_sasl.state = IRCV3_SASL_STATE_NONE;

	if (!s->registered)
		io_dx(s->connection);

	return 0;
}

int
ircv3_numeric_908(struct server *s, struct irc_message *m)
{
	/* <mechanisms> :are available SASL mechanisms */

	char *mechanisms;
	char *message;

	if (!irc_message_param(m, &mechanisms))
		failf(s, "RPL_SASLMECHS: missing mechanisms");

	irc_message_param(m, &message);

	if (message && *message)
		server_info(s, "%s %s", message);
	else
		server_info(s, "%s are available SASL mechanisms");

	return 0;
}

static int
ircv3_recv_cap_LS(struct server *s, struct irc_message *m)
{
	/* If no capabilities are available, an empty
	 * parameter MUST be sent.
	 *
	 * Servers MAY send multiple lines in response to
	 * CAP LS and CAP LIST. If the reply contains
	 * multiple lines, all but the last reply MUST
	 * have a parameter containing only an asterisk (*)
	 * preceding the capability list
	 *
	 * CAP <targ> LS [*] :[<cap_1> [...]]
	 */

	char *cap_key;
	char *cap_val;
	char *caps;
	char *multiline;

	irc_message_param(m, &multiline);
	irc_message_param(m, &caps);

	if (!multiline)
		failf(s, "CAP LS: parameter is null");

	if (!strcmp(multiline, "*") && !caps)
		failf(s, "CAP LS: parameter is null");

	if (strcmp(multiline, "*") && caps)
		failf(s, "CAP LS: invalid parameters");

	if (!caps) {
		caps = multiline;
		multiline = NULL;
	}

	if (s->registered) {
		server_info(s, "CAP LS: %s", (*caps ? caps : "(no capabilities)"));
		return 0;
	}

	while ((cap_key = irc_strsep(&(caps)))) {

		struct ircv3_cap *c;

		if ((cap_val = strchr(cap_key, '=')))
			*cap_val++ = 0;

		if (!(c = ircv3_cap_get(&(s->ircv3_caps), cap_key)))
			continue;

		c->supported = 1;
		c->val = (cap_val ? strdup(cap_val) : NULL);

		if (c->req_auto)
			c->req = 1;
	}

	if (multiline)
		return 0;

	if (ircv3_cap_req_count(&(s->ircv3_caps)))
		return ircv3_cap_req_send(&(s->ircv3_caps), s);

	sendf(s, "CAP END");

	return 0;
}

static int
ircv3_recv_cap_LIST(struct server *s, struct irc_message *m)
{
	/* If no capabilities are available, an empty
	 * parameter MUST be sent.
	 *
	 * Servers MAY send multiple lines in response to
	 * CAP LS and CAP LIST. If the reply contains
	 * multiple lines, all but the last reply MUST
	 * have a parameter containing only an asterisk (*)
	 * preceding the capability list
	 *
	 * CAP <targ> LIST [*] :[<cap_1> [...]]
	 */

	char *caps;
	char *multiline;

	irc_message_param(m, &multiline);
	irc_message_param(m, &caps);

	if (!multiline)
		failf(s, "CAP LIST: parameter is null");

	if (multiline && caps && strcmp(multiline, "*"))
		failf(s, "CAP LIST: invalid parameters");

	if (!strcmp(multiline, "*") && !caps)
		failf(s, "CAP LIST: parameter is null");

	if (!caps)
		caps = multiline;

	server_info(s, "CAP LIST: %s", (*caps ? caps : "(no capabilities)"));

	return 0;
}

static int
ircv3_recv_cap_ACK(struct server *s, struct irc_message *m)
{
	/* Each capability name may be prefixed with a
	 * dash (-), indicating that this capability has
	 * been disabled as requested.
	 *
	 * If an ACK reply originating from the server is
	 * spread across multiple lines, a client MUST NOT
	 * change capabilities until the last ACK of the
	 * set is received. Equally, a server MUST NOT change
	 * the capabilities of the client until the last ACK
	 * of the set has been sent.
	 *
	 * CAP <targ> ACK :[-]<cap_1> [[-]<cap_2> [...]]
	 */

	char *cap;
	char *caps;
	int errors = 0;

	if (!irc_message_param(m, &caps))
		failf(s, "CAP ACK: parameter is null");

	if (!(cap = irc_strsep(&(caps))))
		failf(s, "CAP ACK: parameter is empty");

	do {
		int unset;
		struct ircv3_cap *c;

		if ((unset = (*cap == '-')))
			cap++;

		if (!(c = ircv3_cap_get(&(s->ircv3_caps), cap))) {
			server_error(s, "CAP ACK: '%s' not supported", cap);
			errors++;
			continue;
		}

		if (!c->req) {
			server_error(s, "CAP ACK: '%s%s' was not requested", (unset ? "-" : ""), cap);
			errors++;
			continue;
		}

		if (!unset && c->set) {
			server_error(s, "CAP ACK: '%s' was set", cap);
			errors++;
			continue;
		}

		if (unset && !c->set) {
			server_error(s, "CAP ACK: '%s' was not set", cap);
			errors++;
			continue;
		}

		c->req = 0;
		c->set = !unset;

		server_info(s, "capability change accepted: %s%s%s%s",
			(unset ? "-" : ""), cap, (c->val ? "=" : ""), (c->val ? c->val : ""));

		if (!strcmp(cap, "sasl"))
			ircv3_sasl_init(s);

	} while ((cap = irc_strsep(&(caps))));

	if (errors)
		failf(s, "CAP ACK: parameter errors");

	if (ircv3_registered(s))
		sendf(s, "CAP END");

	return 0;
}

static int
ircv3_recv_cap_NAK(struct server *s, struct irc_message *m)
{
	/* The server MUST NOT make any change to any
	 * capabilities if it replies with a NAK subcommand.
	 *
	 * CAP <targ> NAK :<cap_1> [<cap_2> [...]]
	 */

	char *cap;
	char *caps;

	if (!irc_message_param(m, &caps))
		failf(s, "CAP NAK: parameter is null");

	if (!(cap = irc_strsep(&(caps))))
		failf(s, "CAP NAK: parameter is empty");

	do {
		struct ircv3_cap *c;

		if ((c = ircv3_cap_get(&(s->ircv3_caps), cap)))
			c->req = 0;

		server_info(s, "capability change rejected: %s", cap);

	} while ((cap = irc_strsep(&(caps))));

	if (ircv3_registered(s))
		sendf(s, "CAP END");

	return 0;
}

static int
ircv3_recv_cap_DEL(struct server *s, struct irc_message *m)
{
	/* Upon receiving a CAP DEL message, the client MUST
	 * treat the listed capabilities as cancelled and no
	 * longer available. Clients SHOULD NOT send CAP REQ
	 * messages to cancel the capabilities in CAP DEL,
	 * as they have already been cancelled by the server.
	 *
	 * CAP <targ> DEL :<cap_1> [<cap_2> [...]]
	 */

	char *cap;
	char *caps;

	if (!irc_message_param(m, &caps))
		failf(s, "CAP DEL: parameter is null");

	if (!(cap = irc_strsep(&(caps))))
		failf(s, "CAP DEL: parameter is empty");

	do {
		struct ircv3_cap *c;

		if (!(c = ircv3_cap_get(&(s->ircv3_caps), cap)))
			continue;

		if (!c->supports_del)
			failf(s, "CAP DEL: '%s' doesn't support DEL", cap);

		c->req = 0;
		c->set = 0;
		c->supported = 0;

		server_info(s, "capability lost: %s", cap);

	} while ((cap = irc_strsep(&(caps))));

	return 0;
}

static int
ircv3_recv_cap_NEW(struct server *s, struct irc_message *m)
{
	/* Clients that support CAP NEW messages SHOULD respond
	 * with a CAP REQ message if they wish to enable one or
	 * more of the newly-offered capabilities.
	 *
	 * CAP <targ> NEW :<cap_1> [<cap_2> [...]]
	 */

	char *cap;
	char *caps;
	struct ircv3_caps batch_reqs = {0};

	if (!irc_message_param(m, &caps))
		failf(s, "CAP NEW: parameter is null");

	if (!(cap = irc_strsep(&(caps))))
		failf(s, "CAP NEW: parameter is empty");

	do {
		struct ircv3_cap *c1 = ircv3_cap_get(&(s->ircv3_caps), cap);
		struct ircv3_cap *c2 = ircv3_cap_get(&batch_reqs, cap);

		if (!c1 || !c2)
			continue;

		c1->supported = 1;

		if (c1->set || c1->req || !c1->req_auto) {
			server_info(s, "new capability: %s", cap);
		} else {
			c1->req = 1;
			c2->req = 1;
			server_info(s, "new capability: %s (auto-req)", cap);
		}
	} while ((cap = irc_strsep(&(caps))));

	if (ircv3_cap_req_count(&batch_reqs))
		return ircv3_cap_req_send(&batch_reqs, s);

	return 0;
}

static int
ircv3_recv_AUTHENTICATE_EXTERNAL(struct server *s, struct irc_message *m)
{
	/* C: AUTHENTICATE EXTERNAL
	 * S: +
	 * C: +
	 */

	if (s->ircv3_sasl.state != IRCV3_SASL_STATE_REQ_METHOD)
		failf(s, "Invalid SASL state for method EXTERNAL: %d", s->ircv3_sasl.state);

	if (strcmp(m->params, "+"))
		failf(s, "Invalid SASL response for method EXTERNAL: '%s'", m->params);

	sendf(s, "AUTHENTICATE +");

	return 0;
}

static int
ircv3_recv_AUTHENTICATE_PLAIN(struct server *s, struct irc_message *m)
{
	/* C: AUTHENTICATE PLAIN
	 * S: +
	 * C: AUTHENTICATE base64(<authzid><null><authcid><null><passwd>)
	 */

	/* (((4 * 300 / 3) + 3) & ~3) */
	unsigned char resp_dec[300];
	unsigned char resp_enc[400];
	size_t len;

	if (!s->ircv3_sasl.user || !*s->ircv3_sasl.user)
		failf(s, "SASL method PLAIN requires a username");

	if (!s->ircv3_sasl.pass || !*s->ircv3_sasl.pass)
		failf(s, "SASL method PLAIN requires a password");

	if (s->ircv3_sasl.state != IRCV3_SASL_STATE_REQ_METHOD)
		failf(s, "Invalid SASL state for method PLAIN: %d", s->ircv3_sasl.state);

	if (strcmp(m->params, "+"))
		failf(s, "Invalid SASL response for method PLAIN: '%s'", m->params);

	len = snprintf((char *)resp_dec, sizeof(resp_dec), "%s%c%s%c%s",
		s->ircv3_sasl.user, 0,
		s->ircv3_sasl.user, 0,
		s->ircv3_sasl.pass);

	if (len >= sizeof(resp_dec))
		failf(s, "SASL decoded auth message too long");

	if (mbedtls_base64_encode(resp_enc, sizeof(resp_enc), &len, resp_dec, len))
		failf(s, "SASL encoded auth message too long");

	sendf(s, "AUTHENTICATE %s", resp_enc);

	return 0;
}

static int
ircv3_cap_req_count(struct ircv3_caps *caps)
{
	#define X(CAP, VAR, ATTRS) + !!caps->VAR.req
	return 0 IRCV3_CAPS;
	#undef X
}

static int
ircv3_cap_req_send(struct ircv3_caps *caps, struct server *s)
{
	int mid = 0;
	int ret;

	#define X(CAP, VAR, ATTRS) \
	const char *sep_##VAR = (caps->VAR.req && mid++ ? " " : ""); \
	const char *str_##VAR = (caps->VAR.req          ? CAP : "");
	IRCV3_CAPS
	#undef X

	if ((ret = io_sendf(s->connection, "CAP REQ :"
		#define X(CAP, VAR, ATTRS) "%s%s"
		IRCV3_CAPS
		#undef X
		#define X(CAP, VAR, ATTRS) ,sep_##VAR,str_##VAR
		IRCV3_CAPS
		#undef X
	))) {
		failf(s, "Send fail: %s", io_err(ret));
	}

	return 0;
}

static int
ircv3_registered(struct server *s)
{
	/* Previously registered or doesn't support IRCv3 */
	if (s->registered)
		return 0;

	/* IRCv3 CAP negotiation in progress */
	if (ircv3_cap_req_count(&(s->ircv3_caps)))
		return 0;

	/* IRCv3 SASL authentication in progress */
	if (s->ircv3_sasl.state != IRCV3_SASL_STATE_NONE
	 && s->ircv3_sasl.state != IRCV3_SASL_STATE_AUTHENTICATED)
		return 0;

	return 1;
}

static int
ircv3_sasl_init(struct server *s)
{
	if (s->ircv3_sasl.method == IRCV3_SASL_METHOD_NONE)
		return 0;

	switch (s->ircv3_sasl.state) {

		/* Start authentication process */
		case IRCV3_SASL_STATE_NONE:
			break;

		/* Authentication in progress */
		case IRCV3_SASL_STATE_REQ_METHOD:
			return 0;

		/* Previously authenticated */
		case IRCV3_SASL_STATE_AUTHENTICATED:
			return 0;

		default:
			fatal("unknown SASL state");
	}

	switch (s->ircv3_sasl.method) {
		case IRCV3_SASL_METHOD_EXTERNAL:
			sendf(s, "AUTHENTICATE EXTERNAL");
			break;
		case IRCV3_SASL_METHOD_PLAIN:
			sendf(s, "AUTHENTICATE PLAIN");
			break;
		default:
			fatal("unknown SASL authentication method");
	}

	s->ircv3_sasl.state = IRCV3_SASL_STATE_REQ_METHOD;

	return 0;
}