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
|
/***************************************************************************\
* *
* BitlBee - An IRC to IM gateway *
* Jabber module - IQ packets *
* *
* Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
\***************************************************************************/
#include "jabber.h"
xt_status jabber_pkt_iq( struct xt_node *node, gpointer data )
{
struct gaim_connection *gc = data;
struct jabber_data *jd = gc->proto_data;
struct xt_node *query, *reply = NULL, *orig = NULL, *c;
char *s, *type, *xmlns;
int st;
query = xt_find_node( node->children, "query" );
type = xt_find_attr( node, "type" );
if( !type )
return XT_HANDLED; /* Ignore it for now, don't know what's best... */
xmlns = xt_find_attr( query, "xmlns" );
if( ( s = xt_find_attr( node, "id" ) ) )
orig = jabber_packet_from_cache( gc, s );
if( strcmp( type, "result" ) == 0 && xmlns && strcmp( xmlns, "jabber:iq:auth" ) == 0 )
{
/* Time to authenticate ourselves! */
reply = xt_new_node( "query", NULL, NULL );
xt_add_attr( reply, "xmlns", "jabber:iq:auth" );
xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) );
xt_add_child( reply, xt_new_node( "resource", set_getstr( &gc->acc->set, "resource" ), NULL ) );
if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) )
{
/* We can do digest authentication, it seems, and of
course we prefer that. */
SHA_CTX sha;
char hash_hex[40];
unsigned char hash[20];
int i;
shaInit( &sha );
shaUpdate( &sha, (unsigned char*) s, strlen( s ) );
shaUpdate( &sha, (unsigned char*) gc->acc->pass, strlen( gc->acc->pass ) );
shaFinal( &sha, hash );
for( i = 0; i < 20; i ++ )
sprintf( hash_hex + i * 2, "%02x", hash[i] );
xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) );
}
else if( xt_find_node( query->children, "password" ) )
{
/* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */
xt_add_child( reply, xt_new_node( "password", gc->acc->pass, NULL ) );
}
else
{
xt_free_node( reply );
hide_login_progress( gc, "Can't find suitable authentication method" );
signoff( gc );
return XT_ABORT;
}
reply = jabber_make_packet( "iq", "set", NULL, reply );
jabber_cache_packet( gc, reply );
st = jabber_write_packet( gc, reply );
return st ? XT_HANDLED : XT_ABORT;
}
if( strcmp( type, "result" ) == 0 && xmlns && strcmp( xmlns, "jabber:iq:roster" ) == 0 )
{
struct xt_node *node;
node = query->children;
while( ( node = xt_find_node( node, "item" ) ) )
{
char *jid = xt_find_attr( node, "jid" );
char *name = xt_find_attr( node, "name" );
char *sub = xt_find_attr( node, "subscription" );
if( jid && sub && ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) )
add_buddy( gc, NULL, jid, name );
node = node->next;
}
account_online( gc );
}
if( strcmp( type, "result" ) == 0 && xmlns && strcmp( xmlns, "jabber:iq:privacy" ) == 0 )
{
struct xt_node *node;
/* When receiving a list of lists: */
if( ( node = xt_find_node( query->children, "active" ) ) )
{
if( ( s = xt_find_attr( node, "name" ) ) )
{
set_t *set;
g_free( jd->privacy_active );
jd->privacy_active = g_strdup( s );
/* Save it so the user can see it. */
if( ( set = set_find( &gc->acc->set, "privacy_list" ) ) )
{
g_free( set->value );
set->value = g_strdup( s );
}
if( !jabber_get_privacy( gc ) )
return XT_ABORT;
}
}
/* When receiving an actual list: */
else if( ( node = xt_find_node( query->children, "list" ) ) )
{
xt_free_node( jd->privacy_list );
jd->privacy_list = xt_dup( node );
}
else if( query->children == NULL )
{
/* What to do here if there is no privacy list defined yet... */
}
}
else if( strcmp( type, "result" ) == 0 && orig )
{
struct xt_node *c;
if( !( jd->flags & JFLAG_AUTHENTICATED ) &&
( c = xt_find_node( orig->children, "query" ) ) &&
( c = xt_find_node( c->children, "username" ) ) &&
c->text_len )
{
/* This happens when we just successfully authenticated
the old (non-SASL) way. */
jd->flags |= JFLAG_AUTHENTICATED;
if( !jabber_get_roster( gc ) || !jabber_get_privacy( gc ) )
return XT_ABORT;
}
/* Tricky: Look for <bind> in the reply, because the server
should confirm the chosen resource string there. For
<session>, however, look in the cache, because the server
will probably not include it in its reply. */
else if( ( c = xt_find_node( node->children, "bind" ) ) ||
( c = xt_find_node( orig->children, "session" ) ) )
{
if( strcmp( c->name, "bind" ) == 0 )
{
c = xt_find_node( c->children, "jid" );
if( c && c->text_len && ( s = strchr( c->text, '/' ) ) &&
strcmp( s + 1, set_getstr( &gc->acc->set, "resource" ) ) != 0 )
serv_got_crap( gc, "Server changed session resource string to `%s'", s + 1 );
jd->flags &= ~JFLAG_WAIT_BIND;
}
else if( strcmp( c->name, "session" ) == 0 )
jd->flags &= ~JFLAG_WAIT_SESSION;
if( ( jd->flags & ( JFLAG_WAIT_BIND | JFLAG_WAIT_SESSION ) ) == 0 )
{
if( !jabber_get_roster( gc ) || !jabber_get_privacy( gc ) )
return XT_ABORT;
}
}
else if( ( c = xt_find_node( orig->children, "query" ) ) &&
( c = xt_find_node( c->children, "active" ) ) )
{
/* We just successfully activated a (different)
privacy list. Fetch it now. */
g_free( jd->privacy_active );
jd->privacy_active = g_strdup( xt_find_attr( c, "name" ) );
if( !jabber_get_privacy( gc ) )
return XT_ABORT;
}
}
else if( strcmp( type, "error" ) == 0 )
{
if( !( jd->flags & JFLAG_AUTHENTICATED ) &&
orig &&
( c = xt_find_node( orig->children, "query" ) ) &&
( c = xt_find_node( c->children, "username" ) ) &&
c->text_len )
{
hide_login_progress( gc, "Authentication failure" );
signoff( gc );
return XT_ABORT;
}
else if( ( xmlns && strcmp( xmlns, "jabber:iq:privacy" ) == 0 ) ||
( orig &&
( c = xt_find_node( orig->children, "query" ) ) &&
( s = xt_find_attr( c, "xmlns" ) ) &&
strcmp( s, "jabber:iq:privacy" ) == 0 ) )
{
/* All errors related to privacy lists. */
if( ( c = xt_find_node( node->children, "error" ) ) == NULL )
{
hide_login_progress_error( gc, "Received malformed error packet" );
signoff( gc );
return XT_ABORT;
}
if( xt_find_node( c->children, "item-not-found" ) )
{
serv_got_crap( gc, "Error while activating privacy list, maybe it doesn't exist" );
/* Should I do anything else here? */
}
else if( xt_find_node( c->children, "feature-not-implemented" ) )
{
jd->flags |= JFLAG_PRIVACY_BROKEN;
/* Probably there's no need to inform the user.
We can do that if the user ever tries to use
the block/allow commands. */
}
}
}
return XT_HANDLED;
}
int jabber_start_iq_auth( struct gaim_connection *gc )
{
struct jabber_data *jd = gc->proto_data;
struct xt_node *node;
int st;
node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) );
xt_add_attr( node, "xmlns", "jabber:iq:auth" );
node = jabber_make_packet( "iq", "get", NULL, node );
st = jabber_write_packet( gc, node );
xt_free_node( node );
return st;
}
int jabber_get_roster( struct gaim_connection *gc )
{
struct xt_node *node;
int st;
set_login_progress( gc, 1, "Authenticated, requesting buddy list" );
node = xt_new_node( "query", NULL, NULL );
xt_add_attr( node, "xmlns", "jabber:iq:roster" );
node = jabber_make_packet( "iq", "get", NULL, node );
st = jabber_write_packet( gc, node );
xt_free_node( node );
return st;
}
int jabber_add_to_roster( struct gaim_connection *gc, char *handle, char *name )
{
struct xt_node *node;
int st;
/* Build the item entry */
node = xt_new_node( "item", NULL, NULL );
xt_add_attr( node, "jid", handle );
if( name )
xt_add_attr( node, "name", name );
/* And pack it into a roster-add packet */
node = xt_new_node( "query", NULL, node );
xt_add_attr( node, "xmlns", "jabber:iq:roster" );
node = jabber_make_packet( "iq", "set", NULL, node );
st = jabber_write_packet( gc, node );
xt_free_node( node );
return st;
}
int jabber_remove_from_roster( struct gaim_connection *gc, char *handle )
{
struct xt_node *node;
int st;
/* Build the item entry */
node = xt_new_node( "item", NULL, NULL );
xt_add_attr( node, "jid", handle );
xt_add_attr( node, "subscription", "remove" );
/* And pack it into a roster-add packet */
node = xt_new_node( "query", NULL, node );
xt_add_attr( node, "xmlns", "jabber:iq:roster" );
node = jabber_make_packet( "iq", "set", NULL, node );
st = jabber_write_packet( gc, node );
xt_free_node( node );
return st;
}
/* Request the privacy list from the server. We need this, because every
time we remove/add something we have to send the whole new list to the
server again... If no privacy list is specified yet, this function will
first ask for the list of lists (XMPP supports multiple "privacy lists",
don't ask me why), later we can then fetch the list we want to use. */
int jabber_get_privacy( struct gaim_connection *gc )
{
struct jabber_data *jd = gc->proto_data;
struct xt_node *node = NULL;
char *name;
int st;
if( jd->privacy_active )
{
/* If we know what is the active list right now, fetch it. */
node = xt_new_node( "list", NULL, NULL );
xt_add_attr( node, "name", jd->privacy_active );
}
/* Okay, we don't know yet. If the user set a specific list, we'll
activate that one. Otherwise, we should figure out which list is
currently active. */
else if( ( name = set_getstr( &gc->acc->set, "privacy_list" ) ) )
{
return jabber_set_privacy( gc, name );
}
/* else: sending this packet without a <list/> element will give
a list of available lists and information about the currently
active list. */
node = xt_new_node( "query", NULL, node );
xt_add_attr( node, "xmlns", "jabber:iq:privacy" );
node = jabber_make_packet( "iq", "get", NULL, node );
jabber_cache_packet( gc, node );
st = jabber_write_packet( gc, node );
return st;
}
int jabber_set_privacy( struct gaim_connection *gc, char *name )
{
struct xt_node *node;
node = xt_new_node( "active", NULL, NULL );
xt_add_attr( node, "name", name );
node = xt_new_node( "query", NULL, node );
xt_add_attr( node, "xmlns", "jabber:iq:privacy" );
node = jabber_make_packet( "iq", "set", NULL, node );
jabber_cache_packet( gc, node );
return jabber_write_packet( gc, node );
}
char *set_eval_privacy_list( set_t *set, char *value )
{
account_t *acc = set->data;
struct jabber_data *jd = acc->gc->proto_data;
if( jd->flags & JFLAG_PRIVACY_BROKEN )
{
serv_got_crap( acc->gc, "Privacy lists not supported by this server" );
return NULL;
}
/* If we're on-line, return NULL and let the server decide if the
chosen list is valid. If we're off-line, just accept it and we'll
see later (when we connect). */
if( acc->gc )
jabber_set_privacy( acc->gc, value );
return acc->gc ? NULL : value;
}
|