summaryrefslogtreecommitdiffstats
path: root/tools/iksroster.c
blob: b3e4312aa9d8c55b78592cc45e7ff9d7bc85a5a3 (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
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2004 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include "common.h"
#include "iksemel.h"

#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif

#ifdef _WIN32
#include <winsock.h>
#endif

#ifdef HAVE_GETOPT_LONG
static struct option longopts[] = {
	{ "backup", required_argument, 0, 'b' },
	{ "restore", required_argument, 0, 'r' },
	{ "file", required_argument, 0, 'f' },
	{ "timeout", required_argument, 0, 't' },
	{ "secure", 0, 0, 's' },
	{ "sasl", 0, 0, 'a' },
	{ "plain", 0, 0, 'p' },
	{ "log", 0, 0, 'l' },
	{ "help", 0, 0, 'h' },
	{ "version", 0, 0, 'V' },
	{ 0, 0, 0, 0 }
};
#endif

static char *shortopts = "b:r:f:t:saplhV";

static void
print_usage (void)
{
	puts ("Usage: iksroster [OPTIONS]\n"
		"This is a backup tool for your jabber roster.\n"
		" -b, --backup=JID    Download roster from the server.\n"
		" -r, --restore=JID   Upload roster to the server.\n"
		" -f, --file=FILE     Load/Save roster to this file.\n"
		" -t, --timeout=SECS  Set network timeout.\n"
		" -s, --secure        Use encrypted connection.\n"
		" -a, --sasl          Use SASL authentication.\n"
		" -p, --plain         Use plain text authentication.\n"
		" -l, --log           Print exchanged xml data.\n"
		" -h, --help          Print this text and exit.\n"
		" -V, --version       Print version and exit.\n"
#ifndef HAVE_GETOPT_LONG
		"(long options are not supported on your system)\n"
#endif
#ifndef HAVE_GNUTLS
		"(secure connections are not supported on your system)\n"
#endif
		"Report bugs to <iksemel-dev@jabberstudio.org>.");
}

/* stuff we keep per session */
struct session {
	iksparser *prs;
	iksid *acc;
	char *pass;
	int features;
	int authorized;
	int counter;
	int set_roster;
	int job_done;
};

/* precious roster we'll deal with */
iks *my_roster;

/* out packet filter */
iksfilter *my_filter;

/* connection time outs if nothing comes for this much seconds */
int opt_timeout = 30;

/* connection flags */
int opt_use_tls;
int opt_use_sasl;
int opt_use_plain;
int opt_log;

void
j_error (char *msg)
{
	fprintf (stderr, "iksroster: %s\n", msg);
	exit (2);
}

int
on_result (struct session *sess, ikspak *pak)
{
	iks *x;

	if (sess->set_roster == 0) {
		x = iks_make_iq (IKS_TYPE_GET, IKS_NS_ROSTER);
		iks_insert_attrib (x, "id", "roster");
		iks_send (sess->prs, x);
		iks_delete (x);
	} else {
		iks_insert_attrib (my_roster, "type", "set");
		iks_send (sess->prs, my_roster);
	}
	return IKS_FILTER_EAT;
}

int
on_stream (struct session *sess, int type, iks *node)
{
	sess->counter = opt_timeout;

	switch (type) {
		case IKS_NODE_START:
			if (opt_use_tls && !iks_is_secure (sess->prs)) {
				iks_start_tls (sess->prs);
				break;
			}
			if (!opt_use_sasl) {
				iks *x;
				char *sid = NULL;

				if (!opt_use_plain) sid = iks_find_attrib (node, "id");
				x = iks_make_auth (sess->acc, sess->pass, sid);
				iks_insert_attrib (x, "id", "auth");
				iks_send (sess->prs, x);
				iks_delete (x);
			}
			break;

		case IKS_NODE_NORMAL:
			if (strcmp ("stream:features", iks_name (node)) == 0) {
				sess->features = iks_stream_features (node);
				if (opt_use_sasl) {
					if (opt_use_tls && !iks_is_secure (sess->prs)) break;
					if (sess->authorized) {
						iks *t;
						if (sess->features & IKS_STREAM_BIND) {
							t = iks_make_resource_bind (sess->acc);
							iks_send (sess->prs, t);
							iks_delete (t);
						}
						if (sess->features & IKS_STREAM_SESSION) {
							t = iks_make_session ();
							iks_insert_attrib (t, "id", "auth");
							iks_send (sess->prs, t);
							iks_delete (t);
						}
					} else {
						if (sess->features & IKS_STREAM_SASL_MD5)
							iks_start_sasl (sess->prs, IKS_SASL_DIGEST_MD5, sess->acc->user, sess->pass);
						else if (sess->features & IKS_STREAM_SASL_PLAIN)
							iks_start_sasl (sess->prs, IKS_SASL_PLAIN, sess->acc->user, sess->pass);
					}
				}
			} else if (strcmp ("failure", iks_name (node)) == 0) {
				j_error ("sasl authentication failed");
			} else if (strcmp ("success", iks_name (node)) == 0) {
				sess->authorized = 1;
				iks_send_header (sess->prs, sess->acc->server);
			} else {
				ikspak *pak;

				pak = iks_packet (node);
				iks_filter_packet (my_filter, pak);
				if (sess->job_done == 1) return IKS_HOOK;
			}
			break;

		case IKS_NODE_STOP:
			j_error ("server disconnected");

		case IKS_NODE_ERROR:
			j_error ("stream error");
	}

	if (node) iks_delete (node);
	return IKS_OK;
}

int
on_error (void *user_data, ikspak *pak)
{
	j_error ("authorization failed");
	return IKS_FILTER_EAT;
}

int
on_roster (struct session *sess, ikspak *pak)
{
	my_roster = pak->x;
	sess->job_done = 1;
	return IKS_FILTER_EAT;
}

void
on_log (struct session *sess, const char *data, size_t size, int is_incoming)
{
	if (iks_is_secure (sess->prs)) fprintf (stderr, "Sec");
	if (is_incoming) fprintf (stderr, "RECV"); else fprintf (stderr, "SEND");
	fprintf (stderr, "[%s]\n", data);
}

void
j_setup_filter (struct session *sess)
{
	if (my_filter) iks_filter_delete (my_filter);
	my_filter = iks_filter_new ();
	iks_filter_add_rule (my_filter, (iksFilterHook *) on_result, sess,
		IKS_RULE_TYPE, IKS_PAK_IQ,
		IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
		IKS_RULE_ID, "auth",
		IKS_RULE_DONE);
	iks_filter_add_rule (my_filter, on_error, sess,
		IKS_RULE_TYPE, IKS_PAK_IQ,
		IKS_RULE_SUBTYPE, IKS_TYPE_ERROR,
		IKS_RULE_ID, "auth",
		IKS_RULE_DONE);
	iks_filter_add_rule (my_filter, (iksFilterHook *) on_roster, sess,
		IKS_RULE_TYPE, IKS_PAK_IQ,
		IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
		IKS_RULE_ID, "roster",
		IKS_RULE_DONE);
}

void
j_connect (char *jabber_id, char *pass, int set_roster)
{
	struct session sess;
	int e;

	memset (&sess, 0, sizeof (sess));
	sess.prs = iks_stream_new (IKS_NS_CLIENT, &sess, (iksStreamHook *) on_stream);
	if (opt_log) iks_set_log_hook (sess.prs, (iksLogHook *) on_log);
	sess.acc = iks_id_new (iks_parser_stack (sess.prs), jabber_id);
	if (NULL == sess.acc->resource) {
		/* user gave no resource name, use the default */
		char *tmp;
		tmp = iks_malloc (strlen (sess.acc->user) + strlen (sess.acc->server) + 9 + 3);
		sprintf (tmp, "%s@%s/%s", sess.acc->user, sess.acc->server, "iksroster");
		sess.acc = iks_id_new (iks_parser_stack (sess.prs), tmp);
		iks_free (tmp);
	}
	sess.pass = pass;
	sess.set_roster = set_roster;

	j_setup_filter (&sess);

	e = iks_connect_tcp (sess.prs, sess.acc->server, IKS_JABBER_PORT);
	switch (e) {
		case IKS_OK:
			break;
		case IKS_NET_NODNS:
			j_error ("hostname lookup failed");
		case IKS_NET_NOCONN:
			j_error ("connection failed");
		default:
			j_error ("io error");
	}

	sess.counter = opt_timeout;
	while (1) {
		e = iks_recv (sess.prs, 1);
		if (IKS_HOOK == e) break;
		if (IKS_NET_TLSFAIL == e) j_error ("tls handshake failed");
		if (IKS_OK != e) j_error ("io error");
		sess.counter--;
		if (sess.counter == 0) j_error ("network timeout");
	}
	iks_parser_delete (sess.prs);
}

int
main (int argc, char *argv[])
{
	char *from = NULL;
	char *to = NULL;
	char *file = NULL;
	char from_pw[128], to_pw[128];
	int c;
#ifdef HAVE_GETOPT_LONG
	int i;

	while ((c = getopt_long (argc, argv, shortopts, longopts, &i)) != -1) {
#else
	while ((c = getopt (argc, argv, shortopts)) != -1) {
#endif
		switch (c) {
			case 'b':
				from = optarg;
				printf ("Password for %s: ", optarg);
				fflush (stdout);
				fgets (from_pw, 127, stdin);
				strtok (from_pw, "\r\n");
				break;
			case 'r':
				to = optarg;
				printf ("Password for %s: ", optarg);
				fflush (stdout);
				fgets (to_pw, 127, stdin);
				strtok (to_pw, "\r\n");
				break;
			case 'f':
				file = strdup (optarg);
				break;
			case 't':
				opt_timeout = atoi (optarg);
				if (opt_timeout < 10) opt_timeout = 10;
				break;
			case 's':
				if (!iks_has_tls ()) {
					puts ("Cannot make encrypted connections.");
					puts ("iksemel library is not compiled with GnuTLS support.");
					exit (1);
				}
				opt_use_tls = 1;
				break;
			case 'a':
				opt_use_sasl = 1;
				break;
			case 'p':
				opt_use_plain = 1;
				opt_use_sasl = 0;
				break;
			case 'l':
				opt_log = 1;
				break;
			case 'h':
				print_usage ();
				exit (0);
			case 'V':
				puts ("iksroster (iksemel) "VERSION);
				exit (0);
		}
	}
	if (from == NULL && to == NULL) {
		puts ("What I'm supposed to do?");
		print_usage ();
		exit (1);
	}
	if (to && (from == NULL && file == NULL)) {
		puts ("Store which roster?");
		print_usage ();
		exit (1);
	}

#ifdef _WIN32
	WSADATA wsaData;
	WSAStartup (MAKEWORD (1,1), &wsaData);
#endif

	if (from) {
		j_connect (from, from_pw, 0);
		if (file) {
			switch (iks_save (file, my_roster)) {
				case IKS_OK:
					break;
				case IKS_FILE_NOACCESS:
					j_error ("cannot write to file");
				default:
					j_error ("file io error");
			}
		}
	} else {
		switch (iks_load (file, &my_roster)) {
			case IKS_OK:
				break;
			case IKS_FILE_NOFILE:
				j_error ("file not found");
			case IKS_FILE_NOACCESS:
				j_error ("cannot read file");
			default:
				j_error ("file io error");
		}
	}
	if (to) {
		j_connect (to, to_pw, 1);
	}

#ifdef _WIN32
	WSACleanup ();
#endif

	return 0;
}