1 /**
2  * @file dnssrv.c
3  */
4 
5 /* purple
6  *
7  * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
22  */
23 #define _PURPLE_DNSSRV_C_
24 
25 #include "glibcompat.h"
26 #include "internal.h"
27 #include "util.h"
28 
29 #ifndef _WIN32
30 #include <arpa/nameser.h>
31 #include <resolv.h>
32 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
33 #include <arpa/nameser_compat.h>
34 #endif
35 #else /* WIN32 */
36 #include <windns.h>
37 /* Missing from the mingw headers */
38 #ifndef DNS_TYPE_SRV
39 # define DNS_TYPE_SRV PurpleDnsTypeSrv
40 #endif
41 #ifndef DNS_TYPE_TXT
42 # define DNS_TYPE_TXT PurpleDnsTypeTxt
43 #endif
44 #endif
45 
46 #ifndef T_SRV
47 #define T_SRV	PurpleDnsTypeSrv
48 #endif
49 #ifndef T_TXT
50 #define T_TXT	PurpleDnsTypeTxt
51 #endif
52 
53 #define MAX_ADDR_RESPONSE_LEN 1048576
54 
55 #include "debug.h"
56 #include "dnssrv.h"
57 #include "eventloop.h"
58 #include "network.h"
59 
60 static PurpleSrvTxtQueryUiOps *srv_txt_query_ui_ops = NULL;
61 
62 #ifndef _WIN32
63 typedef union {
64 	HEADER hdr;
65 	u_char buf[1024];
66 } queryans;
67 #endif
68 
69 struct _PurpleSrvTxtQueryData {
70 	union {
71 		PurpleSrvCallback srv;
72 		PurpleTxtCallback txt;
73 	} cb;
74 
75 	gpointer extradata;
76 	guint handle;
77 	int type;
78 	char *query;
79 #ifdef _WIN32
80 	GThread *resolver;
81 	char *error_message;
82 	GList *results;
83 #else
84 	int fd_in, fd_out;
85 	pid_t pid;
86 #endif
87 };
88 
89 typedef struct _PurpleSrvInternalQuery {
90 	int type;
91 	char query[256];
92 } PurpleSrvInternalQuery;
93 
94 typedef struct _PurpleSrvResponseContainer {
95 	PurpleSrvResponse *response;
96 	int sum;
97 } PurpleSrvResponseContainer;
98 
99 static gboolean purple_srv_txt_query_ui_resolve(PurpleSrvTxtQueryData *query_data);
100 
101 /**
102  * Sort by priority, then by weight.  Strictly numerically--no
103  * randomness.  Technically we only need to sort by pref and then
104  * make sure any records with weight 0 are at the beginning of
105  * their group, but it's just as easy to sort by weight.
106  */
107 static gint
responsecompare(gconstpointer ar,gconstpointer br)108 responsecompare(gconstpointer ar, gconstpointer br)
109 {
110 	PurpleSrvResponse *a = (PurpleSrvResponse*)ar;
111 	PurpleSrvResponse *b = (PurpleSrvResponse*)br;
112 
113 	if(a->pref == b->pref) {
114 		if(a->weight == b->weight)
115 			return 0;
116 		if(a->weight < b->weight)
117 			return -1;
118 		return 1;
119 	}
120 	if(a->pref < b->pref)
121 		return -1;
122 	return 1;
123 }
124 
125 /**
126  * Iterate over a list of PurpleSrvResponseContainer making the sum
127  * the running total of the sums.  Select a random integer in the range
128  * (1, sum+1), then find the first element greater than or equal to the
129  * number selected.  From RFC 2782.
130  *
131  * @param list The list of PurpleSrvResponseContainer.  This function
132  *        removes a node from this list and returns the new list.
133  * @param container_ptr The PurpleSrvResponseContainer that was chosen
134  *        will be returned here.
135  */
136 static GList *
select_random_response(GList * list,PurpleSrvResponseContainer ** container_ptr)137 select_random_response(GList *list, PurpleSrvResponseContainer **container_ptr)
138 {
139 	GList *cur;
140 	size_t runningtotal;
141 	int r;
142 
143 	runningtotal = 0;
144 	cur = list;
145 
146 	while (cur) {
147 		PurpleSrvResponseContainer *container = cur->data;
148 		runningtotal += container->response->weight;
149 		container->sum = runningtotal;
150 		cur = cur->next;
151 	}
152 
153 	/*
154 	 * If the running total is greater than 0, pick a number between
155 	 * 1 and the runningtotal inclusive. (This is not precisely what
156 	 * the RFC algorithm describes, but we wish to deal with integers
157 	 * and avoid floats.  This is functionally equivalent.)
158 	 * If running total is 0, then choose r = 0.
159 	 */
160 	r = runningtotal ? g_random_int_range(1, runningtotal + 1) : 0;
161 	cur = list;
162 	while (r > ((PurpleSrvResponseContainer *)cur->data)->sum) {
163 		if(cur->next == NULL) {
164 			break;
165 		}
166 
167 		cur = cur->next;
168 	}
169 
170 	/* Set the return parameter and remove cur from the list */
171 	*container_ptr = cur->data;
172 	return g_list_delete_link(list, cur);
173 }
174 
175 /**
176  * Reorder a GList of PurpleSrvResponses that have the same priority
177  * (aka "pref").
178  */
179 static void
srv_reorder(GList * list,int num)180 srv_reorder(GList *list, int num)
181 {
182 	int i;
183 	GList *cur, *container_list = NULL;
184 	PurpleSrvResponseContainer *container;
185 
186 	if (num < 2)
187 		/* Nothing to sort */
188 		return;
189 
190 	/* First build a list of container structs */
191 	for (i = 0, cur = list; i < num; i++, cur = cur->next) {
192 		container = g_new(PurpleSrvResponseContainer, 1);
193 		container->response = cur->data;
194 		container_list = g_list_prepend(container_list, container);
195 	}
196 	container_list = g_list_reverse(container_list);
197 
198 	/*
199 	 * Re-order the list that was passed in as a parameter.  We leave
200 	 * the list nodes in place, but replace their data pointers.
201 	 */
202 	cur = list;
203 	while (container_list) {
204 		container_list = select_random_response(container_list, &container);
205 
206 		if(container == NULL) {
207 			break;
208 		}
209 
210 		cur->data = container->response;
211 		g_free(container);
212 		cur = cur->next;
213 
214 		if(cur == NULL) {
215 			break;
216 		}
217 	}
218 }
219 
220 /**
221  * Sorts a GList of PurpleSrvResponses according to the
222  * algorithm described in RFC 2782.
223  *
224  * @param response GList of PurpleSrvResponse's
225  * @param The original list, resorted
226  */
227 static GList *
purple_srv_sort(GList * list)228 purple_srv_sort(GList *list)
229 {
230 	int count;
231 	GList *cur, *start;
232 
233 	if (!list || !list->next) {
234 		/* Nothing to sort */
235 		return list;
236 	}
237 
238 	list = g_list_sort(list, responsecompare);
239 
240 	start = cur = list;
241 	count = 1;
242 	while (cur) {
243 		PurpleSrvResponse *next_response;
244 		PurpleSrvResponse *resp = (PurpleSrvResponse *)cur->data;
245 		next_response = cur->next ? cur->next->data : NULL;
246 
247 		if(resp == NULL) {
248 			continue;
249 		}
250 
251 		if (!next_response || next_response->pref != resp->pref) {
252 			/*
253 			 * The 'count' records starting at 'start' all have the same
254 			 * priority.  Sort them by weight.
255 			 */
256 			srv_reorder(start, count);
257 			start = cur->next;
258 			count = 0;
259 		}
260 		count++;
261 		cur = cur->next;
262 	}
263 
264 	return list;
265 }
266 
267 static PurpleSrvTxtQueryData *
query_data_new(int type,gchar * query,gpointer extradata)268 query_data_new(int type, gchar *query, gpointer extradata)
269 {
270 	PurpleSrvTxtQueryData *query_data = g_new0(PurpleSrvTxtQueryData, 1);
271 	query_data->type = type;
272 	query_data->extradata = extradata;
273 	query_data->query = query;
274 #ifndef _WIN32
275 	query_data->fd_in = -1;
276 	query_data->fd_out = -1;
277 #endif
278 	return query_data;
279 }
280 
281 void
purple_srv_txt_query_destroy(PurpleSrvTxtQueryData * query_data)282 purple_srv_txt_query_destroy(PurpleSrvTxtQueryData *query_data)
283 {
284 	PurpleSrvTxtQueryUiOps *ops = purple_srv_txt_query_get_ui_ops();
285 
286 	if (ops && ops->destroy)
287 		ops->destroy(query_data);
288 
289 	if (query_data->handle > 0)
290 		purple_input_remove(query_data->handle);
291 #ifdef _WIN32
292 	if (query_data->resolver != NULL)
293 	{
294 		/*
295 		 * It's not really possible to kill a thread.  So instead we
296 		 * just set the callback to NULL and let the DNS lookup
297 		 * finish.
298 		 */
299 		query_data->cb.srv = NULL;
300 		return;
301 	}
302 	g_free(query_data->error_message);
303 #else
304 	if (query_data->fd_out != -1)
305 		close(query_data->fd_out);
306 	if (query_data->fd_in != -1)
307 		close(query_data->fd_in);
308 #endif
309 	g_free(query_data->query);
310 	g_free(query_data);
311 }
312 
313 #ifdef USE_IDN
314 static gboolean
dns_str_is_ascii(const char * name)315 dns_str_is_ascii(const char *name)
316 {
317 	guchar *c;
318 	for (c = (guchar *)name; c && *c; ++c) {
319 		if (*c > 0x7f)
320 			return FALSE;
321 	}
322 
323 	return TRUE;
324 }
325 #endif
326 
327 #ifndef _WIN32
328 static void
write_to_parent(int in,int out,gconstpointer data,gsize size)329 write_to_parent(int in, int out, gconstpointer data, gsize size)
330 {
331 	const guchar *buf = data;
332 	gssize w;
333 
334 	do {
335 		w = write(out, buf, size);
336 		if (w > 0) {
337 			buf += w;
338 			size -= w;
339 		} else if (w < 0 && errno == EINTR) {
340 			/* Let's try some more; */
341 			w = 1;
342 		}
343 	} while (size > 0 && w > 0);
344 
345 	if (size != 0) {
346 		/* An error occurred */
347 		close(out);
348 		close(in);
349 		_exit(0);
350 	}
351 }
352 
353 /* Read size bytes to data. Dies if an error occurs. */
354 static void
read_from_parent(int in,int out,gpointer data,gsize size)355 read_from_parent(int in, int out, gpointer data, gsize size)
356 {
357 	guchar *buf = data;
358 	gssize r;
359 
360 	do {
361 		r = read(in, data, size);
362 		if (r > 0) {
363 			buf += r;
364 			size -= r;
365 		} else if (r < 0 && errno == EINTR) {
366 			/* Let's try some more; */
367 			r = 1;
368 		}
369 	} while (size > 0 && r > 0);
370 
371 	if (size != 0) {
372 		/* An error occurred */
373 		close(out);
374 		close(in);
375 		_exit(0);
376 	}
377 }
378 
379 
380 G_GNUC_NORETURN static void
resolve(int in,int out)381 resolve(int in, int out)
382 {
383 	GList *ret = NULL;
384 	PurpleSrvResponse *srvres;
385 	PurpleTxtResponse *txtres;
386 	queryans answer;
387 	int size, qdcount, ancount;
388 	guchar *end, *cp;
389 	gchar name[256];
390 	guint16 type, dlen, pref, weight, port;
391 	PurpleSrvInternalQuery query;
392 
393 #ifdef HAVE_SIGNAL_H
394 	purple_restore_default_signal_handlers();
395 #endif
396 
397 	read_from_parent(in, out, &query, sizeof(query));
398 
399 	size = res_query( query.query, C_IN, query.type, (u_char*)&answer, sizeof( answer));
400 	if (size == -1) {
401 		write_to_parent(in, out, &(query.type), sizeof(query.type));
402 		write_to_parent(in, out, &size, sizeof(size));
403 		close(out);
404 		close(in);
405 		_exit(0);
406 	}
407 
408 	qdcount = ntohs(answer.hdr.qdcount);
409 	ancount = ntohs(answer.hdr.ancount);
410 	cp = (guchar*)&answer + sizeof(HEADER);
411 	end = (guchar*)&answer + size;
412 
413 	/* skip over unwanted stuff */
414 	while (qdcount-- > 0 && cp < end) {
415 		size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
416 		if(size < 0) goto end;
417 		cp += size + QFIXEDSZ;
418 	}
419 
420 	while (ancount-- > 0 && cp < end) {
421 		size = dn_expand((unsigned char*)&answer, end, cp, name, 256);
422 		if(size < 0)
423 			goto end;
424 		cp += size;
425 		GETSHORT(type,cp);
426 
427 		/* skip ttl and class since we already know it */
428 		cp += 6;
429 
430 		GETSHORT(dlen,cp);
431 		if (type == T_SRV) {
432 			GETSHORT(pref,cp);
433 
434 			GETSHORT(weight,cp);
435 
436 			GETSHORT(port,cp);
437 
438 			size = dn_expand( (unsigned char*)&answer, end, cp, name, 256);
439 			if(size < 0 )
440 				goto end;
441 
442 			cp += size;
443 
444 			srvres = g_new0(PurpleSrvResponse, 1);
445 			if (strlen(name) > sizeof(srvres->hostname) - 1) {
446 				purple_debug_error("dnssrv", "hostname is longer than available buffer ('%s', %zd bytes)!",
447 				                   name, strlen(name));
448 			}
449 			g_strlcpy(srvres->hostname, name, sizeof(srvres->hostname));
450 			srvres->pref = pref;
451 			srvres->port = port;
452 			srvres->weight = weight;
453 
454 			ret = g_list_prepend(ret, srvres);
455 		} else if (type == T_TXT) {
456 			txtres = g_new0(PurpleTxtResponse, 1);
457 			txtres->content = g_strndup((gchar*)(++cp), dlen-1);
458 			ret = g_list_append(ret, txtres);
459 			cp += dlen - 1;
460 		} else {
461 			cp += dlen;
462 		}
463 	}
464 
465 end:
466 	size = g_list_length(ret);
467 
468 	if (query.type == T_SRV)
469 		ret = purple_srv_sort(ret);
470 
471 	write_to_parent(in, out, &(query.type), sizeof(query.type));
472 	write_to_parent(in, out, &size, sizeof(size));
473 	while (ret != NULL)
474 	{
475 		if (query.type == T_SRV)
476 			write_to_parent(in, out, ret->data, sizeof(PurpleSrvResponse));
477 		if (query.type == T_TXT) {
478 			PurpleTxtResponse *response = ret->data;
479 			gsize l = strlen(response->content) + 1 /* null byte */;
480 			write_to_parent(in, out, &l, sizeof(l));
481 			write_to_parent(in, out, response->content, l);
482 		}
483 
484 		g_free(ret->data);
485 		ret = g_list_delete_link(ret, ret);
486 	}
487 
488 	close(out);
489 	close(in);
490 
491 	_exit(0);
492 }
493 
494 static void
resolved(gpointer data,gint source,PurpleInputCondition cond)495 resolved(gpointer data, gint source, PurpleInputCondition cond)
496 {
497 	int size;
498 	int type;
499 	PurpleSrvTxtQueryData *query_data = (PurpleSrvTxtQueryData*)data;
500 	int i;
501 	int status;
502 
503 	if (read(source, &type, sizeof(type)) == sizeof(type)) {
504 		if (read(source, &size, sizeof(size)) == sizeof(size)) {
505 			if (size < -1 || size > MAX_ADDR_RESPONSE_LEN) {
506 				purple_debug_warning("dnssrv", "res_query returned invalid number\n");
507 				size = 0;
508 			}
509 			if (size == -1 || size == 0) {
510 				if (size == -1) {
511 					purple_debug_warning("dnssrv", "res_query returned an error\n");
512 					/* Re-read resolv.conf and friends in case DNS servers have changed */
513 					res_init();
514 				} else
515 					purple_debug_info("dnssrv", "Found 0 entries, errno is %i\n", errno);
516 
517 				if (type == T_SRV) {
518 					PurpleSrvCallback cb = query_data->cb.srv;
519 					cb(NULL, 0, query_data->extradata);
520 				} else if (type == T_TXT) {
521 					PurpleTxtCallback cb = query_data->cb.txt;
522 					cb(NULL, query_data->extradata);
523 				} else {
524 					purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
525 				}
526 
527 			} else if (size) {
528 				if (type == T_SRV) {
529 					PurpleSrvResponse *res;
530 					PurpleSrvResponse *tmp;
531 					PurpleSrvCallback cb = query_data->cb.srv;
532 					ssize_t red;
533 					purple_debug_info("dnssrv","found %d SRV entries\n", size);
534 					tmp = res = g_new0(PurpleSrvResponse, size);
535 					for (i = 0; i < size; i++) {
536 						red = read(source, tmp++, sizeof(PurpleSrvResponse));
537 						if (red != sizeof(PurpleSrvResponse)) {
538 							purple_debug_error("dnssrv","unable to read srv "
539 									"response: %s\n", g_strerror(errno));
540 							size = 0;
541 							g_free(res);
542 							res = NULL;
543 						}
544 					}
545 
546 					cb(res, size, query_data->extradata);
547 				} else if (type == T_TXT) {
548 					GList *responses = NULL;
549 					PurpleTxtResponse *res;
550 					PurpleTxtCallback cb = query_data->cb.txt;
551 					ssize_t red;
552 					purple_debug_info("dnssrv","found %d TXT entries\n", size);
553 					for (i = 0; i < size; i++) {
554 						gsize len;
555 
556 						red = read(source, &len, sizeof(len));
557 						if (red != sizeof(len)) {
558 							purple_debug_error("dnssrv","unable to read txt "
559 									"response length: %s\n", g_strerror(errno));
560 							size = 0;
561 							g_list_free_full(responses,
562 							                 (GDestroyNotify)purple_txt_response_destroy);
563 							responses = NULL;
564 							break;
565 						}
566 						if (len > MAX_ADDR_RESPONSE_LEN) {
567 							purple_debug_error("dnssrv", "we've read invalid number\n");
568 							size = 0;
569 							g_list_free_full(responses,
570 							                 (GDestroyNotify)purple_txt_response_destroy);
571 							responses = NULL;
572 							break;
573 						}
574 
575 						res = g_new0(PurpleTxtResponse, 1);
576 						res->content = g_new0(gchar, len);
577 
578 						red = read(source, res->content, len);
579 						if (red < 0 || (gsize)red != len) {
580 							purple_debug_error("dnssrv","unable to read txt "
581 									"response: %s\n", g_strerror(errno));
582 							size = 0;
583 							purple_txt_response_destroy(res);
584 							g_list_free_full(responses,
585 							                 (GDestroyNotify)purple_txt_response_destroy);
586 							responses = NULL;
587 							break;
588 						}
589 						responses = g_list_prepend(responses, res);
590 					}
591 
592 					responses = g_list_reverse(responses);
593 					cb(responses, query_data->extradata);
594 				} else {
595 					purple_debug_error("dnssrv", "type unknown of DNS result entry; errno is %i\n", errno);
596 				}
597 			}
598 		}
599 	}
600 
601 	waitpid(query_data->pid, &status, 0);
602 	purple_srv_txt_query_destroy(query_data);
603 }
604 
605 #else /* _WIN32 */
606 
607 /** The Jabber Server code was inspiration for parts of this. */
608 
609 static gboolean
res_main_thread_cb(gpointer data)610 res_main_thread_cb(gpointer data)
611 {
612 	PurpleSrvResponse *srvres = NULL;
613 	PurpleSrvTxtQueryData *query_data = data;
614 	if(query_data->error_message != NULL) {
615 		purple_debug_error("dnssrv", "%s", query_data->error_message);
616 		if (query_data->type == DNS_TYPE_SRV) {
617 			if (query_data->cb.srv)
618 				query_data->cb.srv(srvres, 0, query_data->extradata);
619 		} else if (query_data->type == DNS_TYPE_TXT) {
620 			if (query_data->cb.txt)
621 				query_data->cb.txt(NULL, query_data->extradata);
622 		}
623 	} else {
624 		if (query_data->type == DNS_TYPE_SRV) {
625 			PurpleSrvResponse *srvres_tmp = NULL;
626 			GList *lst = query_data->results;
627 			int size = g_list_length(lst);
628 
629 			if(query_data->cb.srv && size > 0)
630 				srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
631 			while (lst) {
632 				PurpleSrvResponse *lstdata = lst->data;
633 				lst = g_list_delete_link(lst, lst);
634 
635 				if(query_data->cb.srv)
636 					memcpy(srvres_tmp++, lstdata, sizeof(PurpleSrvResponse));
637 				g_free(lstdata);
638 			}
639 
640 			query_data->results = NULL;
641 
642 			purple_debug_info("dnssrv", "found %d SRV entries\n", size);
643 
644 			if(query_data->cb.srv) query_data->cb.srv(srvres, size, query_data->extradata);
645 		} else if (query_data->type == DNS_TYPE_TXT) {
646 			GList *lst = query_data->results;
647 
648 			purple_debug_info("dnssrv", "found %d TXT entries\n", g_list_length(lst));
649 
650 			if (query_data->cb.txt) {
651 				query_data->results = NULL;
652 				query_data->cb.txt(lst, query_data->extradata);
653 			}
654 		} else {
655 			purple_debug_error("dnssrv", "unknown query type");
656 		}
657 	}
658 
659 	query_data->resolver = NULL;
660 	query_data->handle = 0;
661 
662 	purple_srv_txt_query_destroy(query_data);
663 
664 	return FALSE;
665 }
666 
667 static gpointer
res_thread(gpointer data)668 res_thread(gpointer data)
669 {
670 	PDNS_RECORD dr = NULL;
671 	int type;
672 	DNS_STATUS ds;
673 	PurpleSrvTxtQueryData *query_data = data;
674 	type = query_data->type;
675 	ds = DnsQuery_UTF8(query_data->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL);
676 	if (ds != ERROR_SUCCESS) {
677 		gchar *msg = g_win32_error_message(ds);
678 		if (type == DNS_TYPE_SRV) {
679 			query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
680 		} else if (type == DNS_TYPE_TXT) {
681 			query_data->error_message = g_strdup_printf("Couldn't look up TXT record. %s (%lu).\n", msg, ds);
682 		}
683 		g_free(msg);
684 	} else {
685 		if (type == DNS_TYPE_SRV) {
686 			PDNS_RECORD dr_tmp;
687 			GList *lst = NULL;
688 			DNS_SRV_DATA *srv_data;
689 			PurpleSrvResponse *srvres;
690 
691 			for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
692 				/* Discard any incorrect entries. I'm not sure if this is necessary */
693 				if (dr_tmp->wType != type || !purple_strequal(dr_tmp->pName, query_data->query)) {
694 					continue;
695 				}
696 
697 				srv_data = &dr_tmp->Data.SRV;
698 				srvres = g_new0(PurpleSrvResponse, 1);
699 				strncpy(srvres->hostname, srv_data->pNameTarget, 255);
700 				srvres->hostname[255] = '\0';
701 				srvres->pref = srv_data->wPriority;
702 				srvres->port = srv_data->wPort;
703 				srvres->weight = srv_data->wWeight;
704 
705 				lst = g_list_prepend(lst, srvres);
706 			}
707 
708 			DnsRecordListFree(dr, DnsFreeRecordList);
709 			query_data->results = purple_srv_sort(lst);
710 		} else if (type == DNS_TYPE_TXT) {
711 			PDNS_RECORD dr_tmp;
712 			GList *lst = NULL;
713 			DNS_TXT_DATA *txt_data;
714 			PurpleTxtResponse *txtres;
715 
716 			for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
717 				GString *s;
718 				int i;
719 
720 				/* Discard any incorrect entries. I'm not sure if this is necessary */
721 				if (dr_tmp->wType != type || !purple_strequal(dr_tmp->pName, query_data->query)) {
722 					continue;
723 				}
724 
725 				txt_data = &dr_tmp->Data.TXT;
726 				txtres = g_new0(PurpleTxtResponse, 1);
727 
728 				s = g_string_new("");
729 				for (i = 0; i < txt_data->dwStringCount; ++i)
730 					s = g_string_append(s, txt_data->pStringArray[i]);
731 				txtres->content = g_string_free(s, FALSE);
732 
733 				lst = g_list_append(lst, txtres);
734 			}
735 
736 			DnsRecordListFree(dr, DnsFreeRecordList);
737 			query_data->results = lst;
738 		} else {
739 
740 		}
741 	}
742 
743 	/* back to main thread */
744 	/* Note: this should *not* be attached to query_data->handle - it will cause leakage */
745 	purple_timeout_add(0, res_main_thread_cb, query_data);
746 
747 	g_thread_exit(NULL);
748 	return NULL;
749 }
750 
751 #endif
752 
753 PurpleSrvTxtQueryData *
purple_srv_resolve(const char * protocol,const char * transport,const char * domain,PurpleSrvCallback cb,gpointer extradata)754 purple_srv_resolve(const char *protocol, const char *transport,
755 	const char *domain, PurpleSrvCallback cb, gpointer extradata)
756 {
757 	return purple_srv_resolve_account(NULL, protocol, transport, domain,
758 			cb, extradata);
759 }
760 
761 PurpleSrvTxtQueryData *
purple_srv_resolve_account(PurpleAccount * account,const char * protocol,const char * transport,const char * domain,PurpleSrvCallback cb,gpointer extradata)762 purple_srv_resolve_account(PurpleAccount *account, const char *protocol,
763 	const char *transport, const char *domain, PurpleSrvCallback cb,
764 	gpointer extradata)
765 {
766 	char *query;
767 	char *hostname;
768 	PurpleSrvTxtQueryData *query_data;
769 	PurpleProxyType proxy_type;
770 #ifndef _WIN32
771 	PurpleSrvInternalQuery internal_query;
772 	int in[2], out[2];
773 	int pid;
774 #else
775 	GError* err = NULL;
776 #endif
777 
778 	if (!protocol || !*protocol || !transport || !*transport || !domain || !*domain) {
779 		purple_debug_error("dnssrv", "Wrong arguments\n");
780 		cb(NULL, 0, extradata);
781 		g_return_val_if_reached(NULL);
782 	}
783 
784 	proxy_type = purple_proxy_info_get_type(
785 		purple_proxy_get_setup(account));
786 	if (proxy_type == PURPLE_PROXY_TOR) {
787 		purple_debug_info("dnssrv", "Aborting SRV lookup in Tor Proxy mode.\n");
788 		cb(NULL, 0, extradata);
789 		return NULL;
790 	}
791 
792 #ifdef USE_IDN
793 	if (!dns_str_is_ascii(domain)) {
794 		int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
795 		if (ret != 0) {
796 			purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
797 			cb(NULL, 0, extradata);
798 			return NULL;
799 		}
800 	} else /* Fallthru is intentional */
801 #endif
802 	hostname = g_strdup(domain);
803 
804 	query = g_strdup_printf("_%s._%s.%s", protocol, transport, hostname);
805 	purple_debug_info("dnssrv","querying SRV record for %s: %s\n", domain,
806 			query);
807 	g_free(hostname);
808 
809 	query_data = query_data_new(PurpleDnsTypeSrv, query, extradata);
810 	query_data->cb.srv = cb;
811 
812 	if (purple_srv_txt_query_ui_resolve(query_data))
813 	{
814 		return query_data;
815 	}
816 
817 #ifndef _WIN32
818 	if(pipe(in) || pipe(out)) {
819 		purple_debug_error("dnssrv", "Could not create pipe\n");
820 		g_free(query);
821 		g_free(query_data);
822 		cb(NULL, 0, extradata);
823 		return NULL;
824 	}
825 
826 	pid = fork();
827 	if (pid == -1) {
828 		purple_debug_error("dnssrv", "Could not create process!\n");
829 		g_free(query);
830 		g_free(query_data);
831 		cb(NULL, 0, extradata);
832 		return NULL;
833 	}
834 
835 	/* Child */
836 	if (pid == 0)
837 	{
838 		g_free(query);
839 		g_free(query_data);
840 
841 		close(out[0]);
842 		close(in[1]);
843 		resolve(in[0], out[1]);
844 		/* resolve() does not return */
845 	}
846 
847 	close(out[1]);
848 	close(in[0]);
849 
850 	internal_query.type = T_SRV;
851 	strncpy(internal_query.query, query, 255);
852 	internal_query.query[255] = '\0';
853 
854 	if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
855 		purple_debug_error("dnssrv", "Could not write to SRV resolver\n");
856 
857 	query_data->pid = pid;
858 	query_data->fd_out = out[0];
859 	query_data->fd_in = in[1];
860 	query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
861 
862 	return query_data;
863 #else
864 	query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
865 	if (query_data->resolver == NULL) {
866 		query_data->error_message = g_strdup_printf("SRV thread create failure: %s\n", (err && err->message) ? err->message : "");
867 		g_error_free(err);
868 	}
869 
870 	/* The query isn't going to happen, so finish the SRV lookup now.
871 	 * Asynchronously call the callback since stuff may not expect
872 	 * the callback to be called before this returns */
873 	if (query_data->error_message != NULL)
874 		query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
875 
876 	return query_data;
877 #endif
878 }
879 
purple_txt_resolve(const char * owner,const char * domain,PurpleTxtCallback cb,gpointer extradata)880 PurpleSrvTxtQueryData *purple_txt_resolve(const char *owner,
881 	const char *domain, PurpleTxtCallback cb, gpointer extradata)
882 {
883 	return purple_txt_resolve_account(NULL, owner, domain, cb, extradata);
884 }
885 
purple_txt_resolve_account(PurpleAccount * account,const char * owner,const char * domain,PurpleTxtCallback cb,gpointer extradata)886 PurpleSrvTxtQueryData *purple_txt_resolve_account(PurpleAccount *account,
887 	const char *owner, const char *domain, PurpleTxtCallback cb,
888 	gpointer extradata)
889 {
890 	char *query;
891 	char *hostname;
892 	PurpleSrvTxtQueryData *query_data;
893 	PurpleProxyType proxy_type;
894 #ifndef _WIN32
895 	PurpleSrvInternalQuery internal_query;
896 	int in[2], out[2];
897 	int pid;
898 #else
899 	GError* err = NULL;
900 #endif
901 
902 	proxy_type = purple_proxy_info_get_type(
903 		purple_proxy_get_setup(account));
904 	if (proxy_type == PURPLE_PROXY_TOR) {
905 		purple_debug_info("dnssrv", "Aborting TXT lookup in Tor Proxy mode.\n");
906 		cb(NULL, extradata);
907 		return NULL;
908 	}
909 
910 #ifdef USE_IDN
911 	if (!dns_str_is_ascii(domain)) {
912 		int ret = purple_network_convert_idn_to_ascii(domain, &hostname);
913 		if (ret != 0) {
914 			purple_debug_error("dnssrv", "IDNA ToASCII failed\n");
915 			cb(NULL, extradata);
916 			return NULL;
917 		}
918 	} else /* fallthru is intentional */
919 #endif
920 	hostname = g_strdup(domain);
921 
922 	query = g_strdup_printf("%s.%s", owner, hostname);
923 	purple_debug_info("dnssrv","querying TXT record for %s: %s\n", domain,
924 			query);
925 	g_free(hostname);
926 
927 	query_data = query_data_new(PurpleDnsTypeTxt, query, extradata);
928 	query_data->cb.txt = cb;
929 
930 	if (purple_srv_txt_query_ui_resolve(query_data)) {
931 		/* query intentionally not freed
932 		 */
933 		return query_data;
934 	}
935 
936 #ifndef _WIN32
937 	if(pipe(in) || pipe(out)) {
938 		purple_debug_error("dnssrv", "Could not create pipe\n");
939 		g_free(query);
940 		g_free(query_data);
941 		cb(NULL, extradata);
942 		return NULL;
943 	}
944 
945 	pid = fork();
946 	if (pid == -1) {
947 		purple_debug_error("dnssrv", "Could not create process!\n");
948 		g_free(query);
949 		g_free(query_data);
950 		cb(NULL, extradata);
951 		return NULL;
952 	}
953 
954 	/* Child */
955 	if (pid == 0)
956 	{
957 		g_free(query);
958 		g_free(query_data);
959 
960 		close(out[0]);
961 		close(in[1]);
962 		resolve(in[0], out[1]);
963 		/* resolve() does not return */
964 	}
965 
966 	close(out[1]);
967 	close(in[0]);
968 
969 	internal_query.type = T_TXT;
970 	strncpy(internal_query.query, query, 255);
971 	internal_query.query[255] = '\0';
972 
973 	if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
974 		purple_debug_error("dnssrv", "Could not write to TXT resolver\n");
975 
976 	query_data->pid = pid;
977 	query_data->fd_out = out[0];
978 	query_data->fd_in = in[1];
979 	query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
980 
981 	return query_data;
982 #else
983 	query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
984 	if (query_data->resolver == NULL) {
985 		query_data->error_message = g_strdup_printf("TXT thread create failure: %s\n", (err && err->message) ? err->message : "");
986 		g_error_free(err);
987 	}
988 
989 	/* The query isn't going to happen, so finish the TXT lookup now.
990 	 * Asynchronously call the callback since stuff may not expect
991 	 * the callback to be called before this returns */
992 	if (query_data->error_message != NULL)
993 		query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
994 
995 	return query_data;
996 #endif
997 }
998 
999 void
purple_txt_cancel(PurpleSrvTxtQueryData * query_data)1000 purple_txt_cancel(PurpleSrvTxtQueryData *query_data)
1001 {
1002 	purple_srv_txt_query_destroy(query_data);
1003 }
1004 
1005 void
purple_srv_cancel(PurpleSrvTxtQueryData * query_data)1006 purple_srv_cancel(PurpleSrvTxtQueryData *query_data)
1007 {
1008 	purple_srv_txt_query_destroy(query_data);
1009 }
1010 
1011 const gchar *
purple_txt_response_get_content(PurpleTxtResponse * resp)1012 purple_txt_response_get_content(PurpleTxtResponse *resp)
1013 {
1014 	g_return_val_if_fail(resp != NULL, NULL);
1015 
1016 	return resp->content;
1017 }
1018 
purple_txt_response_destroy(PurpleTxtResponse * resp)1019 void purple_txt_response_destroy(PurpleTxtResponse *resp)
1020 {
1021 	g_return_if_fail(resp != NULL);
1022 
1023 	g_free(resp->content);
1024 	g_free(resp);
1025 }
1026 
1027 /*
1028  * Only used as the callback for the ui ops.
1029  */
1030 static void
purple_srv_query_resolved(PurpleSrvTxtQueryData * query_data,GList * records)1031 purple_srv_query_resolved(PurpleSrvTxtQueryData *query_data, GList *records)
1032 {
1033 	GList *l;
1034 	PurpleSrvResponse *records_array;
1035 	int i = 0, length;
1036 
1037 	g_return_if_fail(records != NULL);
1038 
1039 	if (query_data->cb.srv == NULL) {
1040 		purple_srv_txt_query_destroy(query_data);
1041 
1042 		while (records) {
1043 			g_free(records->data);
1044 			records = g_list_delete_link(records, records);
1045 		}
1046 		return;
1047 	}
1048 
1049 	records = purple_srv_sort(records);
1050 	length = g_list_length(records);
1051 
1052 	purple_debug_info("dnssrv", "SRV records resolved for %s, count: %d\n",
1053 	                            query_data->query, length);
1054 
1055 	records_array = g_new(PurpleSrvResponse, length);
1056 	for (l = records; l; l = l->next, i++) {
1057 		records_array[i] = *(PurpleSrvResponse *)l->data;
1058 	}
1059 
1060 	query_data->cb.srv(records_array, length, query_data->extradata);
1061 
1062 	purple_srv_txt_query_destroy(query_data);
1063 
1064 	while (records) {
1065 		g_free(records->data);
1066 		records = g_list_delete_link(records, records);
1067 	}
1068 }
1069 
1070 /*
1071  * Only used as the callback for the ui ops.
1072  */
1073 static void
purple_txt_query_resolved(PurpleSrvTxtQueryData * query_data,GList * entries)1074 purple_txt_query_resolved(PurpleSrvTxtQueryData *query_data, GList *entries)
1075 {
1076 	g_return_if_fail(entries != NULL);
1077 
1078 	purple_debug_info("dnssrv", "TXT entries resolved for %s, count: %d\n", query_data->query, g_list_length(entries));
1079 
1080 	/* the callback should g_free the entries.
1081 	 */
1082 	if (query_data->cb.txt != NULL)
1083 		query_data->cb.txt(entries, query_data->extradata);
1084 	else {
1085 		while (entries) {
1086 			g_free(entries->data);
1087 			entries = g_list_delete_link(entries, entries);
1088 		}
1089 	}
1090 
1091 	purple_srv_txt_query_destroy(query_data);
1092 }
1093 
1094 static void
purple_srv_query_failed(PurpleSrvTxtQueryData * query_data,const gchar * error_message)1095 purple_srv_query_failed(PurpleSrvTxtQueryData *query_data, const gchar *error_message)
1096 {
1097 	purple_debug_error("dnssrv", "%s\n", error_message);
1098 
1099 	if (query_data->cb.srv != NULL)
1100 		query_data->cb.srv(NULL, 0, query_data->extradata);
1101 
1102 	purple_srv_txt_query_destroy(query_data);
1103 }
1104 
1105 static gboolean
purple_srv_txt_query_ui_resolve(PurpleSrvTxtQueryData * query_data)1106 purple_srv_txt_query_ui_resolve(PurpleSrvTxtQueryData *query_data)
1107 {
1108 	PurpleSrvTxtQueryUiOps *ops = purple_srv_txt_query_get_ui_ops();
1109 
1110 	if (ops && ops->resolve)
1111 		return ops->resolve(query_data, (query_data->type == T_SRV ? purple_srv_query_resolved : purple_txt_query_resolved), purple_srv_query_failed);
1112 
1113 	return FALSE;
1114 }
1115 
1116 void
purple_srv_txt_query_set_ui_ops(PurpleSrvTxtQueryUiOps * ops)1117 purple_srv_txt_query_set_ui_ops(PurpleSrvTxtQueryUiOps *ops)
1118 {
1119 	srv_txt_query_ui_ops = ops;
1120 }
1121 
1122 PurpleSrvTxtQueryUiOps *
purple_srv_txt_query_get_ui_ops(void)1123 purple_srv_txt_query_get_ui_ops(void)
1124 {
1125 	/* It is perfectly acceptable for srv_txt_query_ui_ops to be NULL; this just
1126 	 * means that the default platform-specific implementation will be used.
1127 	 */
1128 	return srv_txt_query_ui_ops;
1129 }
1130 
1131 char *
purple_srv_txt_query_get_query(PurpleSrvTxtQueryData * query_data)1132 purple_srv_txt_query_get_query(PurpleSrvTxtQueryData *query_data)
1133 {
1134 	g_return_val_if_fail(query_data != NULL, NULL);
1135 
1136 	return query_data->query;
1137 }
1138 
1139 
1140 int
purple_srv_txt_query_get_type(PurpleSrvTxtQueryData * query_data)1141 purple_srv_txt_query_get_type(PurpleSrvTxtQueryData *query_data)
1142 {
1143 	g_return_val_if_fail(query_data != NULL, 0);
1144 
1145 	return query_data->type;
1146 }
1147