aboutsummaryrefslogtreecommitdiffstats
path: root/lib/md5.c
blob: e6273585be997a181f4faf8b05eb803515bcd29a (plain)
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
/*
  Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

  L. Peter Deutsch
  ghost@aladdin.com

 */
/*
  Independent implementation of MD5 (RFC 1321).

  This code implements the MD5 Algorithm defined in RFC 1321.
  It is derived directly from the text of the RFC and not from the
  reference implementation.

  The original and principal author of md5.c is L. Peter Deutsch
  <ghost@aladdin.com>.  Other authors are noted in the change history
  that follows (in reverse chronological order):

  1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
  1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
  1999-05-03 lpd Original version.
 */

#include "md5.h"
#include <string.h>

#ifdef TEST
/*
 * Compile with -DTEST to create a self-contained executable test program.
 * The test program should print out the same values as given in section
 * A.5 of RFC 1321, reproduced below.
 */
#include <string.h>
main()
{
    static const char *const test[7] = {
	"", /*d41d8cd98f00b204e9800998ecf8427e*/
	"945399884.61923487334tuvga", /*0cc175b9c0f1b6a831c399e269772661*/
	"abc", /*900150983cd24fb0d6963f7d28e17f72*/
	"message digest", /*f96b697d7cb7938d525a2f31aaf161d0*/
	"abcdefghijklmnopqrstuvwxyz", /*c3fcd3d76192e4007dfb496cca67e13b*/
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
				/*d174ab98d277d9f5a5611c2c9f419d9f*/
	"12345678901234567890123456789012345678901234567890123456789012345678901234567890" /*57edf4a22be3c955ac49da2e2107b67a*/
    };
    int i;

    for (i = 0; i < 7; ++i) {
	md5_state_t state;
	md5_byte_t digest[16];
	int di;

	md5_init(&state);
	md5_append(&state, (const md5_byte_t *)test[i], strlen(test[i]));
	md5_finish(&state, digest);
	printf("MD5 (\"%s\") = ", test[i]);
	for (di = 0; di < 16; ++di)
	    printf("%02x", digest[di]);
	printf("\n");
    }
    return 0;
}
#endif /* TEST */


/*
 * For reference, here is the program that computed the T values.
 */
#if 0
#include <math.h>
main()
{
    int i;
    for (i = 1; i <= 64; ++i) {
	unsigned long v = (unsigned long)(4294967296.0 * fabs(sin((double)i)));
	printf("#define T%d 0x%08lx\n", i, v);
    }
    return 0;
}
#endif
/*
 * End of T computation program.
 */
#define T1 0xd76aa478
#define T2 0xe8c7b756
#define T3 0x242070db
#define T4 0xc1bdceee
#define T5 0xf57c0faf
#define T6 0x4787c62a
#define T7 0xa8304613
#define T8 0xfd469501
#define T9 0x698098d8
#define T10 0x8b44f7af
#define T11 0xffff5bb1
#define T12 0x895cd7be
#define T13 0x6b901122
#define T14 0xfd987193
#define T15 0xa679438e
#define T16 0x49b40821
#define T17 0xf61e2562
#define T18 0xc040b340
#define T19 0x265e5a51
#define T20 0xe9b6c7aa
#define T21 0xd62f105d
#define T22 0x02441453
#define T23 0xd8a1e681
#define T24 0xe7d3fbc8
#define T25 0x21e1cde6
#define T26 0xc33707d6
#define T27 0xf4d50d87
#define T28 0x455a14ed
#define T29 0xa9e3e905
#define T30 0xfcefa3f8
#define T31 0x676f02d9
#define T32 0x8d2a4c8a
#define T33 0xfffa3942
#define T34 0x8771f681
#define T35 0x6d9d6122
#define T36 0xfde5380c
#define T37 0xa4beea44
#define T38 0x4bdecfa9
#define T39 0xf6bb4b60
#define T40 0xbebfbc70
#define T41 0x289b7ec6
#define T42 0xeaa127fa
#define T43 0xd4ef3085
#define T44 0x04881d05
#define T45 0xd9d4d039
#define T46 0xe6db99e5
#define T47 0x1fa27cf8
#define T48 0xc4ac5665
#define T49 0xf4292244
#define T50 0x432aff97
#define T51 0xab9423a7
#define T52 0xfc93a039
#define T53 0x655b59c3
#define T54 0x8f0ccc92
#define T55 0xffeff47d
#define T56 0x85845dd1
#define T57 0x6fa87e4f
#define T58 0xfe2ce6e0
#define T59 0xa3014314
#define T60 0x4e0811a1
#define T61 0xf7537e82
#define T62 0xbd3af235
#define T63 0x2ad7d2bb
#define T64 0xeb86d391

static void
md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
{
    md5_word_t
	a = pms->abcd[0], b = pms->abcd[1],
	c = pms->abcd[2], d = pms->abcd[3];
    md5_word_t t;

#ifndef ARCH_IS_BIG_ENDIAN
# define ARCH_IS_BIG_ENDIAN 1	/* slower, default implementation */
#endif
#if ARCH_IS_BIG_ENDIAN

    /*
     * On big-endian machines, we must arrange the bytes in the right
     * order.  (This also works on machines of unknown byte order.)
     */
    md5_word_t X[16];
    const md5_byte_t *xp = data;
    int i;

    for (i = 0; i < 16; ++i, xp += 4)
	X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);

#else  /* !ARCH_IS_BIG_ENDIAN */

    /*
     * On little-endian machines, we can process properly aligned data
     * without copying it.
     */
    md5_word_t xbuf[16];
    const md5_word_t *X;

    if (!((data - (const md5_byte_t *)0) & 3)) {
	/* data are properly aligned */
	X = (const md5_word_t *)data;
    } else {
	/* not aligned */
	memcpy(xbuf, data, 64);
	X = xbuf;
    }
#endif

#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))

    /* Round 1. */
    /* Let [abcd k s i] denote the operation
       a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET(a, b, c, d, k, s, Ti)\
  t = a + F(b,c,d) + X[k] + Ti;\
  a = ROTATE_LEFT(t, s) + b
    /* Do the following 16 operations. */
    SET(a, b, c, d,  0,  7,  T1);
    SET(d, a, b, c,  1, 12,  T2);
    SET(c, d, a, b,  2, 17,  T3);
    SET(b, c, d, a,  3, 22,  T4);
    SET(a, b, c, d,  4,  7,  T5);
    SET(d, a, b, c,  5, 12,  T6);
    SET(c, d, a, b,  6, 17,  T7);
    SET(b, c, d, a,  7, 22,  T8);
    SET(a, b, c, d,  8,  7,  T9);
    SET(d, a, b, c,  9, 12, T10);
    SET(c, d, a, b, 10, 17, T11);
    SET(b, c, d, a, 11, 22, T12);
    SET(a, b, c, d, 12,  7, T13);
    SET(d, a, b, c, 13, 12, T14);
    SET(c, d, a, b, 14, 17, T15);
    SET(b, c, d, a, 15, 22, T16);
#undef SET

     /* Round 2. */
     /* Let [abcd k s i] denote the operation
          a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET(a, b, c, d, k, s, Ti)\
  t = a + G(b,c,d) + X[k] + Ti;\
  a = ROTATE_LEFT(t, s) + b
     /* Do the following 16 operations. */
    SET(a, b, c, d,  1,  5, T17);
    SET(d, a, b, c,  6,  9, T18);
    SET(c, d, a, b, 11, 14, T19);
    SET(b, c, d, a,  0, 20, T20);
    SET(a, b, c, d,  5,  5, T21);
    SET(d, a, b, c, 10,  9, T22);
    SET(c, d, a, b, 15, 14, T23);
    SET(b, c, d, a,  4, 20, T24);
    SET(a, b, c, d,  9,  5, T25);
    SET(d, a, b, c, 14,  9, T26);
    SET(c, d, a, b,  3, 14, T27);
    SET(b, c, d, a,  8, 20, T28);
    SET(a, b, c, d, 13,  5, T29);
    SET(d, a, b, c,  2,  9, T30);
    SET(c, d, a, b,  7, 14, T31);
    SET(b, c, d, a, 12, 20, T32);
#undef SET

     /* Round 3. */
     /* Let [abcd k s t] denote the operation
          a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define SET(a, b, c, d, k, s, Ti)\
  t = a + H(b,c,d) + X[k] + Ti;\
  a = ROTATE_LEFT(t, s) + b
     /* Do the following 16 operations. */
    SET(a, b, c, d,  5,  4, T33);
    SET(d, a, b, c,  8, 11, T34);
    SET(c, d, a, b, 11, 16, T35);
    SET(b, c, d, a, 14, 23, T36);
    SET(a, b, c, d,  1,  4, T37);
    SET(d, a, b, c,  4, 11, T38);
    SET(c, d, a, b,  7, 16, T39);
    SET(b, c, d, a, 10, 23, T40);
    SET(a, b, c, d, 13,  4, T41);
    SET(d, a, b, c,  0, 11, T42);
    SET(c, d, a, b,  3, 16, T43);
    SET(b, c, d, a,  6, 23, T44);
    SET(a, b, c, d,  9,  4, T45);
    SET(d, a, b, c, 12, 11, T46);
    SET(c, d, a, b, 15, 16, T47);
    SET(b, c, d, a,  2, 23, T48);
#undef SET

     /* Round 4. */
     /* Let [abcd k s t] denote the operation
          a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define SET(a, b, c, d, k, s, Ti)\
  t = a + I(b,c,d) + X[k] + Ti;\
  a = ROTATE_LEFT(t, s) + b
     /* Do the following 16 operations. */
    SET(a, b, c, d,  0,  6, T49);
    SET(d, a, b, c,  7, 10, T50);
    SET(c, d, a, b, 14, 15, T51);
    SET(b, c, d, a,  5, 21, T52);
    SET(a, b, c, d, 12,  6, T53);
    SET(d, a, b, c,  3, 10, T54);
    SET(c, d, a, b, 10, 15, T55);
    SET(b, c, d, a,  1, 21, T56);
    SET(a, b, c, d,  8,  6, T57);
    SET(d, a, b, c, 15, 10, T58);
    SET(c, d, a, b,  6, 15, T59);
    SET(b, c, d, a, 13, 21, T60);
    SET(a, b, c, d,  4,  6, T61);
    SET(d, a, b, c, 11, 10, T62);
    SET(c, d, a, b,  2, 15, T63);
    SET(b, c, d, a,  9, 21, T64);
#undef SET

     /* Then perform the following additions. (That is increment each
        of the four registers by the value it had before this block
        was started.) */
    pms->abcd[0] += a;
    pms->abcd[1] += b;
    pms->abcd[2] += c;
    pms->abcd[3] += d;
}

void
md5_init(md5_state_t *pms)
{
    pms->count[0] = pms->count[1] = 0;
    pms->abcd[0] = 0x67452301;
    pms->abcd[1] = 0xefcdab89;
    pms->abcd[2] = 0x98badcfe;
    pms->abcd[3] = 0x10325476;
}

void
md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
{
    const md5_byte_t *p = data;
    int left = nbytes;
    int offset = (pms->count[0] >> 3) & 63;
    md5_word_t nbits = (md5_word_t)(nbytes << 3);

    if (nbytes <= 0)
	return;

    /* Update the message length. */
    pms->count[1] += nbytes >> 29;
    pms->count[0] += nbits;
    if (pms->count[0] < nbits)
	pms->count[1]++;

    /* Process an initial partial block. */
    if (offset) {
	int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);

	memcpy(pms->buf + offset, p, copy);
	if (offset + copy < 64)
	    return;
	p += copy;
	left -= copy;
	md5_process(pms, pms->buf);
    }

    /* Process full blocks. */
    for (; left >= 64; p += 64, left -= 64)
	md5_process(pms, p);

    /* Process a final partial block. */
    if (left)
	memcpy(pms->buf, p, left);
}

void
md5_finish(md5_state_t *pms, md5_byte_t digest[16])
{
    static const md5_byte_t pad[64] = {
	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    };
    md5_byte_t data[8];
    int i;

    /* Save the length before padding. */
    for (i = 0; i < 8; ++i)
	data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
    /* Pad to 56 bytes mod 64. */
    md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
    /* Append the length. */
    md5_append(pms, data, 8);
    for (i = 0; i < 16; ++i)
	digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
}
e_free( irc->b ); while( irc->users ) irc_user_free( irc, (irc_user_t *) irc->users->data ); while( irc->channels ) irc_channel_free( irc->channels->data ); if( irc->ping_source_id > 0 ) b_event_remove( irc->ping_source_id ); if( irc->r_watch_source_id > 0 ) b_event_remove( irc->r_watch_source_id ); if( irc->w_watch_source_id > 0 ) b_event_remove( irc->w_watch_source_id ); closesocket( irc->fd ); irc->fd = -1; g_hash_table_foreach_remove( irc->nick_user_hash, irc_free_hashkey, NULL ); g_hash_table_destroy( irc->nick_user_hash ); g_hash_table_foreach_remove( irc->watches, irc_free_hashkey, NULL ); g_hash_table_destroy( irc->watches ); if( irc->iconv != (GIConv) -1 ) g_iconv_close( irc->iconv ); if( irc->oconv != (GIConv) -1 ) g_iconv_close( irc->oconv ); g_free( irc->sendbuffer ); g_free( irc->readbuffer ); g_free( irc->password ); g_free( irc ); if( global.conf->runmode == RUNMODE_INETD || global.conf->runmode == RUNMODE_FORKDAEMON || ( global.conf->runmode == RUNMODE_DAEMON && global.listen_socket == -1 && irc_connection_list == NULL ) ) b_main_quit(); } static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) { g_free( key ); return( TRUE ); } /* USE WITH CAUTION! Sets pass without checking */ void irc_setpass (irc_t *irc, const char *pass) { g_free (irc->password); if (pass) { irc->password = g_strdup (pass); } else { irc->password = NULL; } } static char *set_eval_password( set_t *set, char *value ) { irc_t *irc = set->data; if( irc->status & USTATUS_IDENTIFIED && value ) { irc_setpass( irc, value ); return NULL; } else { return SET_INVALID; } } static char **irc_splitlines( char *buffer ); void irc_process( irc_t *irc ) { char **lines, *temp, **cmd; int i; if( irc->readbuffer != NULL ) { lines = irc_splitlines( irc->readbuffer ); for( i = 0; *lines[i] != '\0'; i ++ ) { char *conv = NULL; /* [WvG] If the last line isn't empty, it's an incomplete line and we should wait for the rest to come in before processing it. */ if( lines[i+1] == NULL ) { temp = g_strdup( lines[i] ); g_free( irc->readbuffer ); irc->readbuffer = temp; i ++; break; } if( irc->iconv != (GIConv) -1 ) { gsize bytes_read, bytes_written; conv = g_convert_with_iconv( lines[i], -1, irc->iconv, &bytes_read, &bytes_written, NULL ); if( conv == NULL || bytes_read != strlen( lines[i] ) ) { /* GLib can do strange things if things are not in the expected charset, so let's be a little bit paranoid here: */ if( irc->status & USTATUS_LOGGED_IN ) { irc_rootmsg( irc, "Error: Charset mismatch detected. The charset " "setting is currently set to %s, so please make " "sure your IRC client will send and accept text in " "that charset, or tell BitlBee which charset to " "expect by changing the charset setting. See " "`help set charset' for more information. Your " "message was ignored.", set_getstr( &irc->b->set, "charset" ) ); g_free( conv ); conv = NULL; } else { irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, "Warning: invalid characters received at login time." ); conv = g_strdup( lines[i] ); for( temp = conv; *temp; temp ++ ) if( *temp & 0x80 ) *temp = '?'; } } lines[i] = conv; } if( lines[i] && ( cmd = irc_parse_line( lines[i] ) ) ) { irc_exec( irc, cmd ); g_free( cmd ); } g_free( conv ); /* Shouldn't really happen, but just in case... */ if( !g_slist_find( irc_connection_list, irc ) ) { g_free( lines ); return; } } if( lines[i] != NULL ) { g_free( irc->readbuffer ); irc->readbuffer = NULL; } g_free( lines ); } } /* Splits a long string into separate lines. The array is NULL-terminated and, unless the string contains an incomplete line at the end, ends with an empty string. Could use g_strsplit() but this one does it in-place. (So yes, it's destructive.) */ static char **irc_splitlines( char *buffer ) { int i, j, n = 3; char **lines; /* Allocate n+1 elements. */ lines = g_new( char *, n + 1 ); lines[0] = buffer; /* Split the buffer in several strings, and accept any kind of line endings, * knowing that ERC on Windows may send something interesting like \r\r\n, * and surely there must be clients that think just \n is enough... */ for( i = 0, j = 0; buffer[i] != '\0'; i ++ ) { if( buffer[i] == '\r' || buffer[i] == '\n' ) { while( buffer[i] == '\r' || buffer[i] == '\n' ) buffer[i++] = '\0'; lines[++j] = buffer + i; if( j >= n ) { n *= 2; lines = g_renew( char *, lines, n + 1 ); } if( buffer[i] == '\0' ) break; } } /* NULL terminate our list. */ lines[++j] = NULL; return lines; } /* Split an IRC-style line into little parts/arguments. */ char **irc_parse_line( char *line ) { int i, j; char **cmd; /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ if( line[0] == ':' ) { for( i = 0; line[i] && line[i] != ' '; i ++ ); line = line + i; } for( i = 0; line[i] == ' '; i ++ ); line = line + i; /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ if( line[0] == '\0') return NULL; /* Count the number of char **cmd elements we're going to need. */ j = 1; for( i = 0; line[i] != '\0'; i ++ ) { if( line[i] == ' ' ) { j ++; if( line[i+1] == ':' ) break; } } /* Allocate the space we need. */ cmd = g_new( char *, j + 1 ); cmd[j] = NULL; /* Do the actual line splitting, format is: * Input: "PRIVMSG #bitlbee :foo bar" * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL */ cmd[0] = line; for( i = 0, j = 0; line[i] != '\0'; i ++ ) { if( line[i] == ' ' ) { line[i] = '\0'; cmd[++j] = line + i + 1; if( line[i+1] == ':' ) { cmd[j] ++; break; } } } return cmd; } /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ char *irc_build_line( char **cmd ) { int i, len; char *s; if( cmd[0] == NULL ) return NULL; len = 1; for( i = 0; cmd[i]; i ++ ) len += strlen( cmd[i] ) + 1; if( strchr( cmd[i-1], ' ' ) != NULL ) len ++; s = g_new0( char, len + 1 ); for( i = 0; cmd[i]; i ++ ) { if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) strcat( s, ":" ); strcat( s, cmd[i] ); if( cmd[i+1] ) strcat( s, " " ); } strcat( s, "\r\n" ); return s; } void irc_write( irc_t *irc, char *format, ... ) { va_list params; va_start( params, format ); irc_vawrite( irc, format, params ); va_end( params ); return; } void irc_write_all( int now, char *format, ... ) { va_list params; GSList *temp; va_start( params, format ); temp = irc_connection_list; while( temp != NULL ) { irc_t *irc = temp->data; if( now ) { g_free( irc->sendbuffer ); irc->sendbuffer = g_strdup( "\r\n" ); } irc_vawrite( temp->data, format, params ); if( now ) { bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ); } temp = temp->next; } va_end( params ); return; } void irc_vawrite( irc_t *irc, char *format, va_list params ) { int size; char line[IRC_MAX_LINE+1]; /* Don't try to write anything new anymore when shutting down. */ if( irc->status & USTATUS_SHUTDOWN ) return; memset( line, 0, sizeof( line ) ); g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); strip_newlines( line ); if( irc->oconv != (GIConv) -1 ) { gsize bytes_read, bytes_written; char *conv; conv = g_convert_with_iconv( line, -1, irc->oconv, &bytes_read, &bytes_written, NULL ); if( bytes_read == strlen( line ) ) strncpy( line, conv, IRC_MAX_LINE - 2 ); g_free( conv ); } g_strlcat( line, "\r\n", IRC_MAX_LINE + 1 ); if( irc->sendbuffer != NULL ) { size = strlen( irc->sendbuffer ) + strlen( line ); irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); } else { irc->sendbuffer = g_strdup(line); } if( irc->w_watch_source_id == 0 ) { /* If the buffer is empty we can probably write, so call the write event handler immediately. If it returns TRUE, it should be called again, so add the event to the queue. If it's FALSE, we emptied the buffer and saved ourselves some work in the event queue. */ /* Really can't be done as long as the code doesn't do error checking very well: if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */ /* So just always do it via the event handler. */ irc->w_watch_source_id = b_input_add( irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc ); } return; } /* Flush sendbuffer if you can. If it fails, fail silently and let some I/O event handler clean up. */ void irc_flush( irc_t *irc ) { ssize_t n; size_t len; if( irc->sendbuffer == NULL ) return; len = strlen( irc->sendbuffer ); if( ( n = send( irc->fd, irc->sendbuffer, len, 0 ) ) == len ) { g_free( irc->sendbuffer ); irc->sendbuffer = NULL; b_event_remove( irc->w_watch_source_id ); irc->w_watch_source_id = 0; } else if( n > 0 ) { char *s = g_strdup( irc->sendbuffer + n ); g_free( irc->sendbuffer ); irc->sendbuffer = s; } /* Otherwise something went wrong and we don't currently care what the error was. We may or may not succeed later, we were just trying to flush the buffer immediately. */ } /* Meant for takeover functionality. Transfer an IRC connection to a different socket. */ void irc_switch_fd( irc_t *irc, int fd ) { irc_write( irc, "ERROR :Transferring session to a new connection" ); irc_flush( irc ); /* Write it now or forget about it forever. */ if( irc->sendbuffer ) { b_event_remove( irc->w_watch_source_id ); irc->w_watch_source_id = 0; g_free( irc->sendbuffer ); irc->sendbuffer = NULL; } b_event_remove( irc->r_watch_source_id ); closesocket( irc->fd ); irc->fd = fd; irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc ); } void irc_sync( irc_t *irc ) { GSList *l; irc_write( irc, ":%s!%s@%s MODE %s :+%s", irc->user->nick, irc->user->user, irc->user->host, irc->user->nick, irc->umode ); for( l = irc->channels; l; l = l->next ) { irc_channel_t *ic = l->data; if( ic->flags & IRC_CHANNEL_JOINED ) irc_send_join( ic, irc->user ); } /* We may be waiting for a PONG from the previous client connection. */ irc->pinging = FALSE; } void irc_desync( irc_t *irc ) { GSList *l; for( l = irc->channels; l; l = l->next ) irc_channel_del_user( l->data, irc->user, IRC_CDU_KICK, "Switching to old session" ); irc_write( irc, ":%s!%s@%s MODE %s :-%s", irc->user->nick, irc->user->user, irc->user->host, irc->user->nick, irc->umode ); } int irc_check_login( irc_t *irc ) { if( irc->user->user && irc->user->nick ) { if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) { irc_send_num( irc, 464, ":This server is password-protected." ); return 0; } else { irc_channel_t *ic; irc_user_t *iu = irc->user; irc->user = irc_user_new( irc, iu->nick ); irc->user->user = iu->user; irc->user->host = iu->host; irc->user->fullname = iu->fullname; irc->user->f = &irc_user_self_funcs; g_free( iu->nick ); g_free( iu ); if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname ); irc->status |= USTATUS_LOGGED_IN; irc_send_login( irc ); irc->umode[0] = '\0'; irc_umode_set( irc, "+" UMODE, TRUE ); ic = irc->default_channel = irc_channel_new( irc, ROOT_CHAN ); irc_channel_set_topic( ic, CONTROL_TOPIC, irc->root ); set_setstr( &ic->set, "auto_join", "true" ); irc_channel_auto_joins( irc, NULL ); irc->root->last_channel = irc->default_channel; irc_rootmsg( irc, "Welcome to the BitlBee gateway!\n\n" "If you've never used BitlBee before, please do read the help " "information using the \x02help\x02 command. Lots of FAQs are " "answered there.\n" "If you already have an account on this server, just use the " "\x02identify\x02 command to identify yourself." ); /* This is for bug #209 (use PASS to identify to NickServ). */ if( irc->password != NULL ) { char *send_cmd[] = { "identify", g_strdup( irc->password ), NULL }; irc_setpass( irc, NULL ); root_command( irc, send_cmd ); g_free( send_cmd[1] ); } return 1; } } else { /* More information needed. */ return 0; } } /* TODO: This is a mess, but this function is a bit too complicated to be converted to something more generic. */ void irc_umode_set( irc_t *irc, const char *s, gboolean allow_priv ) { /* allow_priv: Set to 0 if s contains user input, 1 if you want to set a "privileged" mode (+o, +R, etc). */ char m[128], st = 1; const char *t; int i; char changes[512], st2 = 2; char badflag = 0; memset( m, 0, sizeof( m ) ); /* Keep track of which modes are enabled in this array. */ for( t = irc->umode; *t; t ++ ) if( *t < sizeof( m ) ) m[(int)*t] = 1; i = 0; for( t = s; *t && i < sizeof( changes ) - 3; t ++ ) { if( *t == '+' || *t == '-' ) st = *t == '+'; else if( ( st == 0 && ( !strchr( UMODES_KEEP, *t ) || allow_priv ) ) || ( st == 1 && strchr( UMODES, *t ) ) || ( st == 1 && allow_priv && strchr( UMODES_PRIV, *t ) ) ) { if( m[(int)*t] != st) { /* If we're actually making a change, remember this for the response. */ if( st != st2 ) st2 = st, changes[i++] = st ? '+' : '-'; changes[i++] = *t; } m[(int)*t] = st; } else badflag = 1; } changes[i] = '\0'; /* Convert the m array back into an umode string. */ memset( irc->umode, 0, sizeof( irc->umode ) ); for( i = 'A'; i <= 'z' && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) if( m[i] ) irc->umode[strlen(irc->umode)] = i; if( badflag ) irc_send_num( irc, 501, ":Unknown MODE flag" ); if( *changes ) irc_write( irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, irc->user->user, irc->user->host, irc->user->nick, changes ); } /* Returns 0 if everything seems to be okay, a number >0 when there was a timeout. The number returned is the number of seconds we received no pongs from the user. When not connected yet, we don't ping but drop the connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ) { double now = gettime(); irc_t *irc = _irc; int fail = 0; if( !( irc->status & USTATUS_LOGGED_IN ) ) { if( now > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) fail = now - irc->last_pong; } else { if( now > ( irc->last_pong + global.conf->ping_timeout ) ) { fail = now - irc->last_pong; } else { irc_write( irc, "PING :%s", IRC_PING_STRING ); } } if( fail > 0 ) { irc_abort( irc, 0, "Ping Timeout: %d seconds", fail ); return FALSE; } return TRUE; } static char *set_eval_charset( set_t *set, char *value ) { irc_t *irc = (irc_t*) set->data; char *test; gsize test_bytes = 0; GIConv ic, oc; if( g_strcasecmp( value, "none" ) == 0 ) value = g_strdup( "utf-8" ); if( ( oc = g_iconv_open( value, "utf-8" ) ) == (GIConv) -1 ) { return NULL; } /* Do a test iconv to see if the user picked an IRC-compatible charset (for example utf-16 goes *horribly* wrong). */ if( ( test = g_convert_with_iconv( " ", 1, oc, NULL, &test_bytes, NULL ) ) == NULL || test_bytes > 1 ) { g_free( test ); g_iconv_close( oc ); irc_rootmsg( irc, "Unsupported character set: The IRC protocol " "only supports 8-bit character sets." ); return NULL; } g_free( test ); if( ( ic = g_iconv_open( "utf-8", value ) ) == (GIConv) -1 ) { g_iconv_close( oc ); return NULL; } if( irc->iconv != (GIConv) -1 ) g_iconv_close( irc->iconv ); if( irc->oconv != (GIConv) -1 ) g_iconv_close( irc->oconv ); irc->iconv = ic; irc->oconv = oc; return value; } /* Mostly meant for upgrades. If one of these is set to the non-default, set show_users of all channels to something with the same effect. */ static char *set_eval_bw_compat( set_t *set, char *value ) { irc_t *irc = set->data; char *val; GSList *l; irc_rootmsg( irc, "Setting `%s' is obsolete, use the `show_users' " "channel setting instead.", set->key ); if( strcmp( set->key, "away_devoice" ) == 0 && !bool2int( value ) ) val = "online,away"; else if( strcmp( set->key, "show_offline" ) == 0 && bool2int( value ) ) val = "online@,away+,offline"; else val = "online+,away"; for( l = irc->channels; l; l = l->next ) { irc_channel_t *ic = l->data; /* No need to check channel type, if the setting doesn't exist it will just be ignored. */ set_setstr( &ic->set, "show_users", val ); } return SET_INVALID; } void register_irc_plugin( const struct irc_plugin *p ) { irc_plugins = g_slist_prepend( irc_plugins, (gpointer) p ); }