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
|
/*
* Copyright (c) 2005 Atheme Development Group
* Rights to this code are documented in doc/LICENSE.
*
* This file contains the main() routine.
*
*/
#include "atheme.h"
DECLARE_MODULE_V1
(
"saslserv/main", false, _modinit, _moddeinit,
PACKAGE_STRING,
"Atheme Development Group <http://www.atheme.org>"
);
mowgli_list_t saslserv_conftable;
mowgli_list_t sessions;
mowgli_list_t sasl_mechanisms;
sasl_session_t *find_session(char *uid);
sasl_session_t *make_session(char *uid);
void destroy_session(sasl_session_t *p);
static void sasl_logcommand(sasl_session_t *p, myuser_t *login, int level, const char *fmt, ...);
static void sasl_input(sasl_message_t *smsg);
static void sasl_packet(sasl_session_t *p, char *buf, int len);
static void sasl_write(char *target, char *data, int length);
int login_user(sasl_session_t *p);
static void sasl_newuser(hook_user_nick_t *data);
static void delete_stale(void *vptr);
/* main services client routine */
static void saslserv(sourceinfo_t *si, int parc, char *parv[])
{
char *cmd;
char *text;
char orig[BUFSIZE];
/* this should never happen */
if (parv[0][0] == '&')
{
slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]);
return;
}
/* make a copy of the original for debugging */
strlcpy(orig, parv[parc - 1], BUFSIZE);
/* lets go through this to get the command */
cmd = strtok(parv[parc - 1], " ");
text = strtok(NULL, "");
if (!cmd)
return;
if (*cmd == '\001')
{
handle_ctcp_common(si, cmd, text);
return;
}
command_fail(si, fault_noprivs, "This service exists to identify "
"connecting clients to the network. It has no "
"public interface.");
}
service_t *saslsvs = NULL;
void _modinit(module_t *m)
{
hook_add_event("sasl_input");
hook_add_sasl_input(sasl_input);
hook_add_event("user_add");
hook_add_user_add(sasl_newuser);
event_add("sasl_delete_stale", delete_stale, NULL, 30);
saslsvs = service_add("saslserv", saslserv, &saslserv_conftable);
authservice_loaded++;
}
void _moddeinit(void)
{
mowgli_node_t *n, *tn;
hook_del_sasl_input(sasl_input);
hook_del_user_add(sasl_newuser);
event_delete(delete_stale, NULL);
if (saslsvs != NULL)
service_delete(saslsvs);
authservice_loaded--;
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head)
{
destroy_session(n->data);
mowgli_node_delete(n, &sessions);
mowgli_node_free(n);
}
}
/*
* Begin SASL-specific code
*/
/* find an existing session by uid */
sasl_session_t *find_session(char *uid)
{
sasl_session_t *p;
mowgli_node_t *n;
MOWGLI_ITER_FOREACH(n, sessions.head)
{
p = n->data;
if(!strcmp(p->uid, uid))
return p;
}
return NULL;
}
/* create a new session if it does not already exist */
sasl_session_t *make_session(char *uid)
{
sasl_session_t *p = find_session(uid);
mowgli_node_t *n;
if(p)
return p;
p = malloc(sizeof(sasl_session_t));
memset(p, 0, sizeof(sasl_session_t));
strlcpy(p->uid, uid, IDLEN);
n = mowgli_node_create();
mowgli_node_add(p, n, &sessions);
return p;
}
/* free a session and all its contents */
void destroy_session(sasl_session_t *p)
{
mowgli_node_t *n, *tn;
myuser_t *mu;
if (p->flags & ASASL_NEED_LOG && p->username != NULL)
{
mu = myuser_find(p->username);
if (mu != NULL)
sasl_logcommand(p, mu, CMDLOG_LOGIN, "LOGIN (session timed out)");
}
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head)
{
if(n->data == p)
{
mowgli_node_delete(n, &sessions);
mowgli_node_free(n);
}
}
free(p->buf);
p->buf = p->p = NULL;
if(p->mechptr)
p->mechptr->mech_finish(p); /* Free up any mechanism data */
p->mechptr = NULL; /* We're not freeing the mechanism, just "dereferencing" it */
free(p->username);
free(p);
}
/* interpret an AUTHENTICATE message */
static void sasl_input(sasl_message_t *smsg)
{
sasl_session_t *p = make_session(smsg->uid);
int len = strlen(smsg->buf);
/* Abort packets, or maybe some other kind of (D)one */
if(smsg->mode == 'D')
{
destroy_session(p);
return;
}
if(smsg->mode != 'S' && smsg->mode != 'C')
return;
if(p->buf == NULL)
{
p->buf = (char *)malloc(len + 1);
p->p = p->buf;
p->len = len;
}
else
{
if(p->len + len + 1 > 8192) /* This is a little much... */
{
sasl_sts(p->uid, 'D', "F");
destroy_session(p);
return;
}
p->buf = (char *)realloc(p->buf, p->len + len + 1);
p->p = p->buf + p->len;
p->len += len;
}
memcpy(p->p, smsg->buf, len);
/* Messages not exactly 400 bytes are the end of a packet. */
if(len < 400)
{
p->buf[p->len] = '\0';
sasl_packet(p, p->buf, p->len);
free(p->buf);
p->buf = p->p = NULL;
p->len = 0;
}
}
/* find a mechanism by name */
static sasl_mechanism_t *find_mechanism(char *name)
{
mowgli_node_t *n;
sasl_mechanism_t *mptr;
MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head)
{
mptr = n->data;
if(!strcmp(mptr->name, name))
return mptr;
}
slog(LG_DEBUG, "find_mechanism(): cannot find mechanism `%s'!", name);
return NULL;
}
/* given an entire sasl message, advance session by passing data to mechanism
* and feeding returned data back to client.
*/
static void sasl_packet(sasl_session_t *p, char *buf, int len)
{
int rc;
size_t tlen = 0;
char *cloak, *out = NULL;
char *temp;
char mech[21];
int out_len = 0;
metadata_t *md;
/* First piece of data in a session is the name of
* the SASL mechanism that will be used.
*/
if(!p->mechptr)
{
if(len > 20)
{
sasl_sts(p->uid, 'D', "F");
destroy_session(p);
return;
}
memcpy(mech, buf, len);
mech[len] = '\0';
if(!(p->mechptr = find_mechanism(mech)))
{
/* Generate a list of supported mechanisms (disabled since charybdis doesn't support this yet). */
/*
char temp[400], *ptr = temp;
int l = 0;
mowgli_node_t *n;
MOWGLI_ITER_FOREACH(n, sasl_mechanisms.head)
{
sasl_mechanism_t *mptr = n->data;
if(l + strlen(mptr->name) > 510)
break;
strcpy(ptr, mptr->name);
ptr += strlen(mptr->name);
*ptr++ = ',';
l += strlen(mptr->name) + 1;
}
if(l)
ptr--;
*ptr = '\0';
sasl_sts(p->uid, 'M', temp);
*/
sasl_sts(p->uid, 'D', "F");
destroy_session(p);
return;
}
rc = p->mechptr->mech_start(p, &out, &out_len);
}else{
if(base64_decode_alloc(buf, len, &temp, &tlen))
{
rc = p->mechptr->mech_step(p, temp, tlen, &out, &out_len);
free(temp);
}else
rc = ASASL_FAIL;
}
/* Some progress has been made, reset timeout. */
p->flags &= ~ASASL_MARKED_FOR_DELETION;
if(rc == ASASL_DONE)
{
myuser_t *mu = myuser_find(p->username);
if(mu && login_user(p))
{
if ((md = metadata_find(mu, "private:usercloak")))
cloak = md->value;
else
cloak = "*";
if (!(mu->flags & MU_WAITAUTH))
svslogin_sts(p->uid, "*", "*", cloak, entity(mu)->name);
sasl_sts(p->uid, 'D', "S");
}
else
sasl_sts(p->uid, 'D', "F");
/* Will destroy session on introduction of user to net. */
return;
}
else if(rc == ASASL_MORE)
{
if(out_len)
{
if(base64_encode_alloc(out, out_len, &temp))
{
sasl_write(p->uid, temp, strlen(temp));
free(temp);
free(out);
return;
}
}
else
{
sasl_sts(p->uid, 'C', "+");
free(out);
return;
}
}
free(out);
sasl_sts(p->uid, 'D', "F");
destroy_session(p);
}
/* output an arbitrary amount of data to the SASL client */
static void sasl_write(char *target, char *data, int length)
{
char out[401];
int last = 400, rem = length;
while(rem)
{
int nbytes = rem > 400 ? 400 : rem;
memcpy(out, data, nbytes);
out[nbytes] = '\0';
sasl_sts(target, 'C', out);
data += nbytes;
rem -= nbytes;
last = nbytes;
}
/* The end of a packet is indicated by a string not of length 400.
* If last piece is exactly 400 in size, send an empty string to
* finish the transaction.
* Also if there is no data at all.
*/
if(last == 400)
sasl_sts(target, 'C', "+");
}
static void sasl_logcommand(sasl_session_t *p, myuser_t *mu, int level, const char *fmt, ...)
{
va_list args;
char lbuf[BUFSIZE];
va_start(args, fmt);
vsnprintf(lbuf, BUFSIZE, fmt, args);
slog(level, "%s %s:%s %s", saslsvs->internal_name, mu ? entity(mu)->name : "", p->uid, lbuf);
va_end(args);
}
/* authenticated, now double check that their account is ok for login */
int login_user(sasl_session_t *p)
{
myuser_t *mu = myuser_find(p->username);
if(mu == NULL) /* WTF? */
return 0;
if (metadata_find(mu, "private:freeze:freezer"))
{
sasl_logcommand(p, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (frozen)", entity(mu)->name);
return 0;
}
if (MOWGLI_LIST_LENGTH(&mu->logins) >= me.maxlogins)
{
sasl_logcommand(p, NULL, CMDLOG_LOGIN, "failed LOGIN to \2%s\2 (too many logins)", entity(mu)->name);
return 0;
}
/* Log it with the full n!u@h later */
p->flags |= ASASL_NEED_LOG;
return 1;
}
/* clean up after a user who is finally on the net */
static void sasl_newuser(hook_user_nick_t *data)
{
user_t *u = data->u;
sasl_session_t *p;
myuser_t *mu;
/* If the user has been killed, don't do anything. */
if (!u)
return;
p = find_session(u->uid);
/* Not concerned unless it's a SASL login. */
if(p == NULL)
return;
/* We will log it ourselves, if needed */
p->flags &= ~ASASL_NEED_LOG;
/* Find the account */
mu = p->username ? myuser_find(p->username) : NULL;
if (mu == NULL)
{
notice(saslsvs->nick, u->nick, "Account %s dropped, login cancelled",
p->username ? p->username : "??");
destroy_session(p);
/* We'll remove their ircd login in handle_burstlogin() */
return;
}
destroy_session(p);
myuser_login(saslsvs, u, mu, false);
logcommand_user(saslsvs, u, CMDLOG_LOGIN, "LOGIN");
}
/* This function is run approximately once every 30 seconds.
* It looks for flagged sessions, and deletes them, while
* flagging all the others. This way stale sessions are deleted
* after no more than 60 seconds.
*/
static void delete_stale(void *vptr)
{
sasl_session_t *p;
mowgli_node_t *n, *tn;
MOWGLI_ITER_FOREACH_SAFE(n, tn, sessions.head)
{
p = n->data;
if(p->flags & ASASL_MARKED_FOR_DELETION)
{
mowgli_node_delete(n, &sessions);
destroy_session(p);
mowgli_node_free(n);
} else
p->flags |= ASASL_MARKED_FOR_DELETION;
}
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
|