├── README.md
├── pidgin
├── .gitignore
├── COPYING
├── Makefile
├── README.md
├── cpim.c
├── cpim.h
├── gpg.c
├── gpg.h
├── kontalk.c
├── kontalk.h
├── tunnel.c
└── tunnel.h
└── tunnel
├── README.md
└── ssl_bridge.py
/README.md:
--------------------------------------------------------------------------------
1 | Kontalk client adapters
2 | =======================
3 |
4 | This is a collection of some client adapters for Kontalk (mainly because of
5 | the need for client certificate authentication).
6 |
7 | It includes:
8 |
9 | * generic SSL tunnel script
10 | * Pidgin plugin
11 |
12 | If you want to use Pidgin on the Kontalk network please refer to the
13 | Wiki page [How to configure Pidgin for Kontalk](//github.com/kontalk/client-adapters/wiki/Configure-Pidgin-for-Kontalk)
14 |
--------------------------------------------------------------------------------
/pidgin/.gitignore:
--------------------------------------------------------------------------------
1 | *.so
2 | *.o
3 |
--------------------------------------------------------------------------------
/pidgin/COPYING:
--------------------------------------------------------------------------------
1 |
2 | GNU GENERAL PUBLIC LICENSE
3 | Version 3, 29 June 2007
4 |
5 | Copyright (C) 2007 Free Software Foundation, Inc.
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The GNU General Public License is a free, copyleft license for
12 | software and other kinds of works.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | the GNU General Public License is intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users. We, the Free Software Foundation, use the
19 | GNU General Public License for most of our software; it applies also to
20 | any other work released this way by its authors. You can apply it to
21 | your programs, too.
22 |
23 | When we speak of free software, we are referring to freedom, not
24 | price. Our General Public Licenses are designed to make sure that you
25 | have the freedom to distribute copies of free software (and charge for
26 | them if you wish), that you receive source code or can get it if you
27 | want it, that you can change the software or use pieces of it in new
28 | free programs, and that you know you can do these things.
29 |
30 | To protect your rights, we need to prevent others from denying you
31 | these rights or asking you to surrender the rights. Therefore, you have
32 | certain responsibilities if you distribute copies of the software, or if
33 | you modify it: responsibilities to respect the freedom of others.
34 |
35 | For example, if you distribute copies of such a program, whether
36 | gratis or for a fee, you must pass on to the recipients the same
37 | freedoms that you received. You must make sure that they, too, receive
38 | or can get the source code. And you must show them these terms so they
39 | know their rights.
40 |
41 | Developers that use the GNU GPL protect your rights with two steps:
42 | (1) assert copyright on the software, and (2) offer you this License
43 | giving you legal permission to copy, distribute and/or modify it.
44 |
45 | For the developers' and authors' protection, the GPL clearly explains
46 | that there is no warranty for this free software. For both users' and
47 | authors' sake, the GPL requires that modified versions be marked as
48 | changed, so that their problems will not be attributed erroneously to
49 | authors of previous versions.
50 |
51 | Some devices are designed to deny users access to install or run
52 | modified versions of the software inside them, although the manufacturer
53 | can do so. This is fundamentally incompatible with the aim of
54 | protecting users' freedom to change the software. The systematic
55 | pattern of such abuse occurs in the area of products for individuals to
56 | use, which is precisely where it is most unacceptable. Therefore, we
57 | have designed this version of the GPL to prohibit the practice for those
58 | products. If such problems arise substantially in other domains, we
59 | stand ready to extend this provision to those domains in future versions
60 | of the GPL, as needed to protect the freedom of users.
61 |
62 | Finally, every program is threatened constantly by software patents.
63 | States should not allow patents to restrict development and use of
64 | software on general-purpose computers, but in those that do, we wish to
65 | avoid the special danger that patents applied to a free program could
66 | make it effectively proprietary. To prevent this, the GPL assures that
67 | patents cannot be used to render the program non-free.
68 |
69 | The precise terms and conditions for copying, distribution and
70 | modification follow.
71 |
72 | TERMS AND CONDITIONS
73 |
74 | 0. Definitions.
75 |
76 | "This License" refers to version 3 of the GNU General Public License.
77 |
78 | "Copyright" also means copyright-like laws that apply to other kinds of
79 | works, such as semiconductor masks.
80 |
81 | "The Program" refers to any copyrightable work licensed under this
82 | License. Each licensee is addressed as "you". "Licensees" and
83 | "recipients" may be individuals or organizations.
84 |
85 | To "modify" a work means to copy from or adapt all or part of the work
86 | in a fashion requiring copyright permission, other than the making of an
87 | exact copy. The resulting work is called a "modified version" of the
88 | earlier work or a work "based on" the earlier work.
89 |
90 | A "covered work" means either the unmodified Program or a work based
91 | on the Program.
92 |
93 | To "propagate" a work means to do anything with it that, without
94 | permission, would make you directly or secondarily liable for
95 | infringement under applicable copyright law, except executing it on a
96 | computer or modifying a private copy. Propagation includes copying,
97 | distribution (with or without modification), making available to the
98 | public, and in some countries other activities as well.
99 |
100 | To "convey" a work means any kind of propagation that enables other
101 | parties to make or receive copies. Mere interaction with a user through
102 | a computer network, with no transfer of a copy, is not conveying.
103 |
104 | An interactive user interface displays "Appropriate Legal Notices"
105 | to the extent that it includes a convenient and prominently visible
106 | feature that (1) displays an appropriate copyright notice, and (2)
107 | tells the user that there is no warranty for the work (except to the
108 | extent that warranties are provided), that licensees may convey the
109 | work under this License, and how to view a copy of this License. If
110 | the interface presents a list of user commands or options, such as a
111 | menu, a prominent item in the list meets this criterion.
112 |
113 | 1. Source Code.
114 |
115 | The "source code" for a work means the preferred form of the work
116 | for making modifications to it. "Object code" means any non-source
117 | form of a work.
118 |
119 | A "Standard Interface" means an interface that either is an official
120 | standard defined by a recognized standards body, or, in the case of
121 | interfaces specified for a particular programming language, one that
122 | is widely used among developers working in that language.
123 |
124 | The "System Libraries" of an executable work include anything, other
125 | than the work as a whole, that (a) is included in the normal form of
126 | packaging a Major Component, but which is not part of that Major
127 | Component, and (b) serves only to enable use of the work with that
128 | Major Component, or to implement a Standard Interface for which an
129 | implementation is available to the public in source code form. A
130 | "Major Component", in this context, means a major essential component
131 | (kernel, window system, and so on) of the specific operating system
132 | (if any) on which the executable work runs, or a compiler used to
133 | produce the work, or an object code interpreter used to run it.
134 |
135 | The "Corresponding Source" for a work in object code form means all
136 | the source code needed to generate, install, and (for an executable
137 | work) run the object code and to modify the work, including scripts to
138 | control those activities. However, it does not include the work's
139 | System Libraries, or general-purpose tools or generally available free
140 | programs which are used unmodified in performing those activities but
141 | which are not part of the work. For example, Corresponding Source
142 | includes interface definition files associated with source files for
143 | the work, and the source code for shared libraries and dynamically
144 | linked subprograms that the work is specifically designed to require,
145 | such as by intimate data communication or control flow between those
146 | subprograms and other parts of the work.
147 |
148 | The Corresponding Source need not include anything that users
149 | can regenerate automatically from other parts of the Corresponding
150 | Source.
151 |
152 | The Corresponding Source for a work in source code form is that
153 | same work.
154 |
155 | 2. Basic Permissions.
156 |
157 | All rights granted under this License are granted for the term of
158 | copyright on the Program, and are irrevocable provided the stated
159 | conditions are met. This License explicitly affirms your unlimited
160 | permission to run the unmodified Program. The output from running a
161 | covered work is covered by this License only if the output, given its
162 | content, constitutes a covered work. This License acknowledges your
163 | rights of fair use or other equivalent, as provided by copyright law.
164 |
165 | You may make, run and propagate covered works that you do not
166 | convey, without conditions so long as your license otherwise remains
167 | in force. You may convey covered works to others for the sole purpose
168 | of having them make modifications exclusively for you, or provide you
169 | with facilities for running those works, provided that you comply with
170 | the terms of this License in conveying all material for which you do
171 | not control copyright. Those thus making or running the covered works
172 | for you must do so exclusively on your behalf, under your direction
173 | and control, on terms that prohibit them from making any copies of
174 | your copyrighted material outside their relationship with you.
175 |
176 | Conveying under any other circumstances is permitted solely under
177 | the conditions stated below. Sublicensing is not allowed; section 10
178 | makes it unnecessary.
179 |
180 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
181 |
182 | No covered work shall be deemed part of an effective technological
183 | measure under any applicable law fulfilling obligations under article
184 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
185 | similar laws prohibiting or restricting circumvention of such
186 | measures.
187 |
188 | When you convey a covered work, you waive any legal power to forbid
189 | circumvention of technological measures to the extent such circumvention
190 | is effected by exercising rights under this License with respect to
191 | the covered work, and you disclaim any intention to limit operation or
192 | modification of the work as a means of enforcing, against the work's
193 | users, your or third parties' legal rights to forbid circumvention of
194 | technological measures.
195 |
196 | 4. Conveying Verbatim Copies.
197 |
198 | You may convey verbatim copies of the Program's source code as you
199 | receive it, in any medium, provided that you conspicuously and
200 | appropriately publish on each copy an appropriate copyright notice;
201 | keep intact all notices stating that this License and any
202 | non-permissive terms added in accord with section 7 apply to the code;
203 | keep intact all notices of the absence of any warranty; and give all
204 | recipients a copy of this License along with the Program.
205 |
206 | You may charge any price or no price for each copy that you convey,
207 | and you may offer support or warranty protection for a fee.
208 |
209 | 5. Conveying Modified Source Versions.
210 |
211 | You may convey a work based on the Program, or the modifications to
212 | produce it from the Program, in the form of source code under the
213 | terms of section 4, provided that you also meet all of these conditions:
214 |
215 | a) The work must carry prominent notices stating that you modified
216 | it, and giving a relevant date.
217 |
218 | b) The work must carry prominent notices stating that it is
219 | released under this License and any conditions added under section
220 | 7. This requirement modifies the requirement in section 4 to
221 | "keep intact all notices".
222 |
223 | c) You must license the entire work, as a whole, under this
224 | License to anyone who comes into possession of a copy. This
225 | License will therefore apply, along with any applicable section 7
226 | additional terms, to the whole of the work, and all its parts,
227 | regardless of how they are packaged. This License gives no
228 | permission to license the work in any other way, but it does not
229 | invalidate such permission if you have separately received it.
230 |
231 | d) If the work has interactive user interfaces, each must display
232 | Appropriate Legal Notices; however, if the Program has interactive
233 | interfaces that do not display Appropriate Legal Notices, your
234 | work need not make them do so.
235 |
236 | A compilation of a covered work with other separate and independent
237 | works, which are not by their nature extensions of the covered work,
238 | and which are not combined with it such as to form a larger program,
239 | in or on a volume of a storage or distribution medium, is called an
240 | "aggregate" if the compilation and its resulting copyright are not
241 | used to limit the access or legal rights of the compilation's users
242 | beyond what the individual works permit. Inclusion of a covered work
243 | in an aggregate does not cause this License to apply to the other
244 | parts of the aggregate.
245 |
246 | 6. Conveying Non-Source Forms.
247 |
248 | You may convey a covered work in object code form under the terms
249 | of sections 4 and 5, provided that you also convey the
250 | machine-readable Corresponding Source under the terms of this License,
251 | in one of these ways:
252 |
253 | a) Convey the object code in, or embodied in, a physical product
254 | (including a physical distribution medium), accompanied by the
255 | Corresponding Source fixed on a durable physical medium
256 | customarily used for software interchange.
257 |
258 | b) Convey the object code in, or embodied in, a physical product
259 | (including a physical distribution medium), accompanied by a
260 | written offer, valid for at least three years and valid for as
261 | long as you offer spare parts or customer support for that product
262 | model, to give anyone who possesses the object code either (1) a
263 | copy of the Corresponding Source for all the software in the
264 | product that is covered by this License, on a durable physical
265 | medium customarily used for software interchange, for a price no
266 | more than your reasonable cost of physically performing this
267 | conveying of source, or (2) access to copy the
268 | Corresponding Source from a network server at no charge.
269 |
270 | c) Convey individual copies of the object code with a copy of the
271 | written offer to provide the Corresponding Source. This
272 | alternative is allowed only occasionally and noncommercially, and
273 | only if you received the object code with such an offer, in accord
274 | with subsection 6b.
275 |
276 | d) Convey the object code by offering access from a designated
277 | place (gratis or for a charge), and offer equivalent access to the
278 | Corresponding Source in the same way through the same place at no
279 | further charge. You need not require recipients to copy the
280 | Corresponding Source along with the object code. If the place to
281 | copy the object code is a network server, the Corresponding Source
282 | may be on a different server (operated by you or a third party)
283 | that supports equivalent copying facilities, provided you maintain
284 | clear directions next to the object code saying where to find the
285 | Corresponding Source. Regardless of what server hosts the
286 | Corresponding Source, you remain obligated to ensure that it is
287 | available for as long as needed to satisfy these requirements.
288 |
289 | e) Convey the object code using peer-to-peer transmission, provided
290 | you inform other peers where the object code and Corresponding
291 | Source of the work are being offered to the general public at no
292 | charge under subsection 6d.
293 |
294 | A separable portion of the object code, whose source code is excluded
295 | from the Corresponding Source as a System Library, need not be
296 | included in conveying the object code work.
297 |
298 | A "User Product" is either (1) a "consumer product", which means any
299 | tangible personal property which is normally used for personal, family,
300 | or household purposes, or (2) anything designed or sold for incorporation
301 | into a dwelling. In determining whether a product is a consumer product,
302 | doubtful cases shall be resolved in favor of coverage. For a particular
303 | product received by a particular user, "normally used" refers to a
304 | typical or common use of that class of product, regardless of the status
305 | of the particular user or of the way in which the particular user
306 | actually uses, or expects or is expected to use, the product. A product
307 | is a consumer product regardless of whether the product has substantial
308 | commercial, industrial or non-consumer uses, unless such uses represent
309 | the only significant mode of use of the product.
310 |
311 | "Installation Information" for a User Product means any methods,
312 | procedures, authorization keys, or other information required to install
313 | and execute modified versions of a covered work in that User Product from
314 | a modified version of its Corresponding Source. The information must
315 | suffice to ensure that the continued functioning of the modified object
316 | code is in no case prevented or interfered with solely because
317 | modification has been made.
318 |
319 | If you convey an object code work under this section in, or with, or
320 | specifically for use in, a User Product, and the conveying occurs as
321 | part of a transaction in which the right of possession and use of the
322 | User Product is transferred to the recipient in perpetuity or for a
323 | fixed term (regardless of how the transaction is characterized), the
324 | Corresponding Source conveyed under this section must be accompanied
325 | by the Installation Information. But this requirement does not apply
326 | if neither you nor any third party retains the ability to install
327 | modified object code on the User Product (for example, the work has
328 | been installed in ROM).
329 |
330 | The requirement to provide Installation Information does not include a
331 | requirement to continue to provide support service, warranty, or updates
332 | for a work that has been modified or installed by the recipient, or for
333 | the User Product in which it has been modified or installed. Access to a
334 | network may be denied when the modification itself materially and
335 | adversely affects the operation of the network or violates the rules and
336 | protocols for communication across the network.
337 |
338 | Corresponding Source conveyed, and Installation Information provided,
339 | in accord with this section must be in a format that is publicly
340 | documented (and with an implementation available to the public in
341 | source code form), and must require no special password or key for
342 | unpacking, reading or copying.
343 |
344 | 7. Additional Terms.
345 |
346 | "Additional permissions" are terms that supplement the terms of this
347 | License by making exceptions from one or more of its conditions.
348 | Additional permissions that are applicable to the entire Program shall
349 | be treated as though they were included in this License, to the extent
350 | that they are valid under applicable law. If additional permissions
351 | apply only to part of the Program, that part may be used separately
352 | under those permissions, but the entire Program remains governed by
353 | this License without regard to the additional permissions.
354 |
355 | When you convey a copy of a covered work, you may at your option
356 | remove any additional permissions from that copy, or from any part of
357 | it. (Additional permissions may be written to require their own
358 | removal in certain cases when you modify the work.) You may place
359 | additional permissions on material, added by you to a covered work,
360 | for which you have or can give appropriate copyright permission.
361 |
362 | Notwithstanding any other provision of this License, for material you
363 | add to a covered work, you may (if authorized by the copyright holders of
364 | that material) supplement the terms of this License with terms:
365 |
366 | a) Disclaiming warranty or limiting liability differently from the
367 | terms of sections 15 and 16 of this License; or
368 |
369 | b) Requiring preservation of specified reasonable legal notices or
370 | author attributions in that material or in the Appropriate Legal
371 | Notices displayed by works containing it; or
372 |
373 | c) Prohibiting misrepresentation of the origin of that material, or
374 | requiring that modified versions of such material be marked in
375 | reasonable ways as different from the original version; or
376 |
377 | d) Limiting the use for publicity purposes of names of licensors or
378 | authors of the material; or
379 |
380 | e) Declining to grant rights under trademark law for use of some
381 | trade names, trademarks, or service marks; or
382 |
383 | f) Requiring indemnification of licensors and authors of that
384 | material by anyone who conveys the material (or modified versions of
385 | it) with contractual assumptions of liability to the recipient, for
386 | any liability that these contractual assumptions directly impose on
387 | those licensors and authors.
388 |
389 | All other non-permissive additional terms are considered "further
390 | restrictions" within the meaning of section 10. If the Program as you
391 | received it, or any part of it, contains a notice stating that it is
392 | governed by this License along with a term that is a further
393 | restriction, you may remove that term. If a license document contains
394 | a further restriction but permits relicensing or conveying under this
395 | License, you may add to a covered work material governed by the terms
396 | of that license document, provided that the further restriction does
397 | not survive such relicensing or conveying.
398 |
399 | If you add terms to a covered work in accord with this section, you
400 | must place, in the relevant source files, a statement of the
401 | additional terms that apply to those files, or a notice indicating
402 | where to find the applicable terms.
403 |
404 | Additional terms, permissive or non-permissive, may be stated in the
405 | form of a separately written license, or stated as exceptions;
406 | the above requirements apply either way.
407 |
408 | 8. Termination.
409 |
410 | You may not propagate or modify a covered work except as expressly
411 | provided under this License. Any attempt otherwise to propagate or
412 | modify it is void, and will automatically terminate your rights under
413 | this License (including any patent licenses granted under the third
414 | paragraph of section 11).
415 |
416 | However, if you cease all violation of this License, then your
417 | license from a particular copyright holder is reinstated (a)
418 | provisionally, unless and until the copyright holder explicitly and
419 | finally terminates your license, and (b) permanently, if the copyright
420 | holder fails to notify you of the violation by some reasonable means
421 | prior to 60 days after the cessation.
422 |
423 | Moreover, your license from a particular copyright holder is
424 | reinstated permanently if the copyright holder notifies you of the
425 | violation by some reasonable means, this is the first time you have
426 | received notice of violation of this License (for any work) from that
427 | copyright holder, and you cure the violation prior to 30 days after
428 | your receipt of the notice.
429 |
430 | Termination of your rights under this section does not terminate the
431 | licenses of parties who have received copies or rights from you under
432 | this License. If your rights have been terminated and not permanently
433 | reinstated, you do not qualify to receive new licenses for the same
434 | material under section 10.
435 |
436 | 9. Acceptance Not Required for Having Copies.
437 |
438 | You are not required to accept this License in order to receive or
439 | run a copy of the Program. Ancillary propagation of a covered work
440 | occurring solely as a consequence of using peer-to-peer transmission
441 | to receive a copy likewise does not require acceptance. However,
442 | nothing other than this License grants you permission to propagate or
443 | modify any covered work. These actions infringe copyright if you do
444 | not accept this License. Therefore, by modifying or propagating a
445 | covered work, you indicate your acceptance of this License to do so.
446 |
447 | 10. Automatic Licensing of Downstream Recipients.
448 |
449 | Each time you convey a covered work, the recipient automatically
450 | receives a license from the original licensors, to run, modify and
451 | propagate that work, subject to this License. You are not responsible
452 | for enforcing compliance by third parties with this License.
453 |
454 | An "entity transaction" is a transaction transferring control of an
455 | organization, or substantially all assets of one, or subdividing an
456 | organization, or merging organizations. If propagation of a covered
457 | work results from an entity transaction, each party to that
458 | transaction who receives a copy of the work also receives whatever
459 | licenses to the work the party's predecessor in interest had or could
460 | give under the previous paragraph, plus a right to possession of the
461 | Corresponding Source of the work from the predecessor in interest, if
462 | the predecessor has it or can get it with reasonable efforts.
463 |
464 | You may not impose any further restrictions on the exercise of the
465 | rights granted or affirmed under this License. For example, you may
466 | not impose a license fee, royalty, or other charge for exercise of
467 | rights granted under this License, and you may not initiate litigation
468 | (including a cross-claim or counterclaim in a lawsuit) alleging that
469 | any patent claim is infringed by making, using, selling, offering for
470 | sale, or importing the Program or any portion of it.
471 |
472 | 11. Patents.
473 |
474 | A "contributor" is a copyright holder who authorizes use under this
475 | License of the Program or a work on which the Program is based. The
476 | work thus licensed is called the contributor's "contributor version".
477 |
478 | A contributor's "essential patent claims" are all patent claims
479 | owned or controlled by the contributor, whether already acquired or
480 | hereafter acquired, that would be infringed by some manner, permitted
481 | by this License, of making, using, or selling its contributor version,
482 | but do not include claims that would be infringed only as a
483 | consequence of further modification of the contributor version. For
484 | purposes of this definition, "control" includes the right to grant
485 | patent sublicenses in a manner consistent with the requirements of
486 | this License.
487 |
488 | Each contributor grants you a non-exclusive, worldwide, royalty-free
489 | patent license under the contributor's essential patent claims, to
490 | make, use, sell, offer for sale, import and otherwise run, modify and
491 | propagate the contents of its contributor version.
492 |
493 | In the following three paragraphs, a "patent license" is any express
494 | agreement or commitment, however denominated, not to enforce a patent
495 | (such as an express permission to practice a patent or covenant not to
496 | sue for patent infringement). To "grant" such a patent license to a
497 | party means to make such an agreement or commitment not to enforce a
498 | patent against the party.
499 |
500 | If you convey a covered work, knowingly relying on a patent license,
501 | and the Corresponding Source of the work is not available for anyone
502 | to copy, free of charge and under the terms of this License, through a
503 | publicly available network server or other readily accessible means,
504 | then you must either (1) cause the Corresponding Source to be so
505 | available, or (2) arrange to deprive yourself of the benefit of the
506 | patent license for this particular work, or (3) arrange, in a manner
507 | consistent with the requirements of this License, to extend the patent
508 | license to downstream recipients. "Knowingly relying" means you have
509 | actual knowledge that, but for the patent license, your conveying the
510 | covered work in a country, or your recipient's use of the covered work
511 | in a country, would infringe one or more identifiable patents in that
512 | country that you have reason to believe are valid.
513 |
514 | If, pursuant to or in connection with a single transaction or
515 | arrangement, you convey, or propagate by procuring conveyance of, a
516 | covered work, and grant a patent license to some of the parties
517 | receiving the covered work authorizing them to use, propagate, modify
518 | or convey a specific copy of the covered work, then the patent license
519 | you grant is automatically extended to all recipients of the covered
520 | work and works based on it.
521 |
522 | A patent license is "discriminatory" if it does not include within
523 | the scope of its coverage, prohibits the exercise of, or is
524 | conditioned on the non-exercise of one or more of the rights that are
525 | specifically granted under this License. You may not convey a covered
526 | work if you are a party to an arrangement with a third party that is
527 | in the business of distributing software, under which you make payment
528 | to the third party based on the extent of your activity of conveying
529 | the work, and under which the third party grants, to any of the
530 | parties who would receive the covered work from you, a discriminatory
531 | patent license (a) in connection with copies of the covered work
532 | conveyed by you (or copies made from those copies), or (b) primarily
533 | for and in connection with specific products or compilations that
534 | contain the covered work, unless you entered into that arrangement,
535 | or that patent license was granted, prior to 28 March 2007.
536 |
537 | Nothing in this License shall be construed as excluding or limiting
538 | any implied license or other defenses to infringement that may
539 | otherwise be available to you under applicable patent law.
540 |
541 | 12. No Surrender of Others' Freedom.
542 |
543 | If conditions are imposed on you (whether by court order, agreement or
544 | otherwise) that contradict the conditions of this License, they do not
545 | excuse you from the conditions of this License. If you cannot convey a
546 | covered work so as to satisfy simultaneously your obligations under this
547 | License and any other pertinent obligations, then as a consequence you may
548 | not convey it at all. For example, if you agree to terms that obligate you
549 | to collect a royalty for further conveying from those to whom you convey
550 | the Program, the only way you could satisfy both those terms and this
551 | License would be to refrain entirely from conveying the Program.
552 |
553 | 13. Use with the GNU Affero General Public License.
554 |
555 | Notwithstanding any other provision of this License, you have
556 | permission to link or combine any covered work with a work licensed
557 | under version 3 of the GNU Affero General Public License into a single
558 | combined work, and to convey the resulting work. The terms of this
559 | License will continue to apply to the part which is the covered work,
560 | but the special requirements of the GNU Affero General Public License,
561 | section 13, concerning interaction through a network will apply to the
562 | combination as such.
563 |
564 | 14. Revised Versions of this License.
565 |
566 | The Free Software Foundation may publish revised and/or new versions of
567 | the GNU General Public License from time to time. Such new versions will
568 | be similar in spirit to the present version, but may differ in detail to
569 | address new problems or concerns.
570 |
571 | Each version is given a distinguishing version number. If the
572 | Program specifies that a certain numbered version of the GNU General
573 | Public License "or any later version" applies to it, you have the
574 | option of following the terms and conditions either of that numbered
575 | version or of any later version published by the Free Software
576 | Foundation. If the Program does not specify a version number of the
577 | GNU General Public License, you may choose any version ever published
578 | by the Free Software Foundation.
579 |
580 | If the Program specifies that a proxy can decide which future
581 | versions of the GNU General Public License can be used, that proxy's
582 | public statement of acceptance of a version permanently authorizes you
583 | to choose that version for the Program.
584 |
585 | Later license versions may give you additional or different
586 | permissions. However, no additional obligations are imposed on any
587 | author or copyright holder as a result of your choosing to follow a
588 | later version.
589 |
590 | 15. Disclaimer of Warranty.
591 |
592 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
593 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
594 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
595 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
596 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
597 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
598 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
599 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
600 |
601 | 16. Limitation of Liability.
602 |
603 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
604 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
605 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
606 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
607 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
608 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
609 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
610 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
611 | SUCH DAMAGES.
612 |
613 | 17. Interpretation of Sections 15 and 16.
614 |
615 | If the disclaimer of warranty and limitation of liability provided
616 | above cannot be given local legal effect according to their terms,
617 | reviewing courts shall apply local law that most closely approximates
618 | an absolute waiver of all civil liability in connection with the
619 | Program, unless a warranty or assumption of liability accompanies a
620 | copy of the Program in return for a fee.
621 |
622 | END OF TERMS AND CONDITIONS
623 |
624 | How to Apply These Terms to Your New Programs
625 |
626 | If you develop a new program, and you want it to be of the greatest
627 | possible use to the public, the best way to achieve this is to make it
628 | free software which everyone can redistribute and change under these terms.
629 |
630 | To do so, attach the following notices to the program. It is safest
631 | to attach them to the start of each source file to most effectively
632 | state the exclusion of warranty; and each file should have at least
633 | the "copyright" line and a pointer to where the full notice is found.
634 |
635 |
636 | Copyright (C)
637 |
638 | This program is free software: you can redistribute it and/or modify
639 | it under the terms of the GNU General Public License as published by
640 | the Free Software Foundation, either version 3 of the License, or
641 | (at your option) any later version.
642 |
643 | This program is distributed in the hope that it will be useful,
644 | but WITHOUT ANY WARRANTY; without even the implied warranty of
645 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
646 | GNU General Public License for more details.
647 |
648 | You should have received a copy of the GNU General Public License
649 | along with this program. If not, see .
650 |
651 | Also add information on how to contact you by electronic and paper mail.
652 |
653 | If the program does terminal interaction, make it output a short
654 | notice like this when it starts in an interactive mode:
655 |
656 | Copyright (C)
657 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
658 | This is free software, and you are welcome to redistribute it
659 | under certain conditions; type `show c' for details.
660 |
661 | The hypothetical commands `show w' and `show c' should show the appropriate
662 | parts of the General Public License. Of course, your program's commands
663 | might be different; for a GUI interface, you would use an "about box".
664 |
665 | You should also get your employer (if you work as a programmer) or school,
666 | if any, to sign a "copyright disclaimer" for the program, if necessary.
667 | For more information on this, and how to apply and follow the GNU GPL, see
668 | .
669 |
670 | The GNU General Public License does not permit incorporating your program
671 | into proprietary programs. If your program is a subroutine library, you
672 | may consider it more useful to permit linking proprietary applications with
673 | the library. If this is what you want to do, use the GNU Lesser General
674 | Public License instead of this License. But first, please read
675 | .
676 |
677 |
--------------------------------------------------------------------------------
/pidgin/Makefile:
--------------------------------------------------------------------------------
1 | # Makefile for Kontalk Pidgin plugin
2 |
3 | PACKAGE_NAME = "kontalk"
4 | PACKAGE_VERSION = "0.2"
5 |
6 | PREFIX = "/usr"
7 | LOCALEDIR = "$(PREFIX)/locale"
8 |
9 | BIN = kontalk.so
10 | OBJS = gpg.o cpim.o tunnel.o kontalk.o
11 | PKGCONFIG_DEPS = pidgin
12 | CC = gcc -c
13 | LD = gcc
14 | CFLAGS = -Wall -fPIC \
15 | -DPACKAGE_NAME=\"$(PACKAGE_NAME)\" \
16 | -DPACKAGE_VERSION=\"$(PACKAGE_VERSION)\" \
17 | -DENABLE_NLS \
18 | -DGETTEXT_PACKAGE=\"$(PACKAGE_NAME)\" \
19 | -DLOCALEDIR=\"$(LOCALEDIR)\" \
20 | -D_FILE_OFFSET_BITS=64 \
21 | -D_GNU_SOURCE \
22 | `pkg-config --cflags $(PKGCONFIG_DEPS)` \
23 | `gpgme-config --cflags`
24 | LDFLAGS = -shared -fPIC `pkg-config --libs $(PKGCONFIG_DEPS)` `gpgme-config --libs`
25 |
26 |
27 | all: $(BIN)
28 |
29 | install: all
30 | strip $(BIN)
31 | install $(BIN) ~/.purple/plugins
32 |
33 | clean:
34 | rm -f $(BIN) $(OBJS)
35 |
36 | $(BIN): $(OBJS)
37 | $(LD) -o $(BIN) $(OBJS) $(LDFLAGS)
38 |
39 | .c.o:
40 | $(CC) $(CFLAGS) -o $@ $<
41 |
--------------------------------------------------------------------------------
/pidgin/README.md:
--------------------------------------------------------------------------------
1 | Pidgin plugin
2 | =============
3 |
4 | This is a plugin for Pidgin. It provides support for:
5 |
6 | - [x] Authentication (provided by tunnel script)
7 | - [x] Encryption
8 | - [x] Key retrieval
9 | - [x] Rename users using name from key
10 | - [x] Media support (receive only)
11 | - [ ] Picture thumbnail
12 | - [ ] Registration
13 |
14 | ## Build
15 |
16 | The command below will install the plugin in your home directory under
17 | `~/.purple/plugins`. You'll need Pidgin and GPGME development headers to compile it.
18 |
19 | ```
20 | make install
21 | ```
22 |
23 |
24 | ## Usage
25 |
26 | By activating this plugin you will have automatic encryption/decryption for
27 | both incoming and outgoing messages plus automatic public key retrieval and
28 | import into your GnuPG keyring. Buddies with unset name will be renamed with
29 | the name found in the public key.
30 |
31 | To enable encryption for outgoing messages, you need to import your personal
32 | key pair (`kontalk-public.asc` and `kontalk-private.asc` from your personal key
33 | archive exported from the app) into GnuPG.
34 | Take note of the main key ID and paste it into the plugin preferences window.
35 | From that point on, Pidgin will start to encrypt outgoing messages, where
36 | possible, and decrypt incoming messages.
37 |
38 |
39 | ## Authentication support
40 |
41 | Kontalk authentication is not supported by Pidgin. We provide two ways to support it.
42 |
43 | The first and most compatible way is the Python script hosted in this repository
44 | in the `tunnel` directory. Please refer to its README file for instructions.
45 |
46 | The second and more easy way relies on the sole plugin. The plugin itself is able
47 | to create a tunnel internally much like the Python script does, but without starting
48 | any other program. Unfortunately, our dependency GLib is linked against GnuTLS which
49 | has issues with our login certificate. Therefore, you'll need to compile
50 | [another GLib module with OpenSSL support](https://github.com/GNOME/glib-openssl).
51 | After that, it's just a matter of telling the plugin where to take the certificate
52 | and the private key, host and port to connect to and the port to listen for
53 | connections from Pidgin (pretty much what the Python script asks).
54 |
55 | You can disable the internal tunnel of the plugin by setting the listening port
56 | to 0 (default).
57 |
58 |
59 | ## Additional features
60 |
61 | You can have support for delivery receipts using another plugin also forked here: [pidgin-xmpp-receipts](//github.com/kontalk/pidgin-xmpp-receipts)
62 |
--------------------------------------------------------------------------------
/pidgin/cpim.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #include "cpim.h"
20 | #include "kontalk.h"
21 |
22 | #include
23 | #include
24 | #include
25 |
26 | #define HEADER_CONTENT_TYPE "Content-type"
27 | #define MESSAGE_CONTENT_TYPE "Message/CPIM"
28 | #define TEXT_CONTENT_TYPE "text/plain"
29 |
30 | #define CPIM_MESSAGE_TEMPLATE \
31 | HEADER_CONTENT_TYPE ": " MESSAGE_CONTENT_TYPE "\n" \
32 | "\n" \
33 | "From: %s\n" \
34 | "To: %s\n" \
35 | "DateTime: %s\n" \
36 | "\n" \
37 | HEADER_CONTENT_TYPE ": %s%s\n" \
38 | "\n" \
39 | "%s"
40 |
41 |
42 | static char *
43 | readline(char *text, char **next)
44 | {
45 | char *newline = strchr(text, '\n');
46 | if (newline != NULL) {
47 | *newline = '\0';
48 | *next = newline + 1;
49 | }
50 | return text;
51 | }
52 |
53 | static char *
54 | parse_header(const char *header, char **name)
55 | {
56 | char *sep = strchr(header, ':');
57 | if (sep != NULL) {
58 | *name = g_strndup(header, sep-header);
59 | return g_strstrip(g_strdup(sep+1));
60 | }
61 |
62 | return NULL;
63 | }
64 |
65 | void
66 | cpim_message_free(cpim_message *msg)
67 | {
68 | if (msg != NULL) {
69 | g_free(msg->from);
70 | g_free(msg->to);
71 | g_free(msg->timestamp);
72 | g_free(msg->type);
73 | g_free(msg->body);
74 | g_free(msg);
75 | }
76 | }
77 |
78 | cpim_message *
79 | cpim_parse_message(const char *text, size_t len)
80 | {
81 | cpim_message *msg = g_malloc0(sizeof(cpim_message));
82 |
83 | char *buf = g_strndup(text, len);
84 | char *ptr = buf;
85 | char *key = NULL;
86 | char *value;
87 | char *line;
88 | int parse_ok;
89 |
90 | line = readline(ptr, &ptr);
91 | value = parse_header(line, &key);
92 |
93 | parse_ok = (value != NULL && !strcasecmp(key, HEADER_CONTENT_TYPE) && !strcasecmp(value, MESSAGE_CONTENT_TYPE));
94 | g_free((gpointer) value);
95 | g_free((gpointer) key);
96 |
97 | if (parse_ok) {
98 | line = readline(ptr, &ptr);
99 | if (!strlen(g_strstrip(line))) {
100 | do {
101 | value = parse_header(line, &key);
102 | if (value != NULL) {
103 | if (!strcasecmp(key, "from")) {
104 | msg->from = value;
105 | }
106 | else if (!strcasecmp(key, "to")) {
107 | msg->to = value;
108 | }
109 | else if (!strcasecmp(key, "datetime")) {
110 | msg->timestamp = value;
111 | }
112 | else {
113 | // useless header
114 | g_free(value);
115 | }
116 |
117 | g_free(key);
118 | }
119 | line = readline(ptr, &ptr);
120 |
121 | } while (strlen(g_strstrip(line)));
122 |
123 | // content type here must be plain text
124 | line = readline(ptr, &ptr);
125 | key = NULL;
126 | value = parse_header(line, &key);
127 | msg->type = value;
128 |
129 | parse_ok = (value != NULL && !strcasecmp(key, HEADER_CONTENT_TYPE));
130 | g_free((gpointer) key);
131 |
132 | if (parse_ok) {
133 | // read the empty line
134 | line = readline(ptr, &ptr);
135 | if (!strlen(g_strstrip(line))) {
136 | msg->body = g_strdup(ptr);
137 | }
138 |
139 | return msg;
140 | }
141 | }
142 | }
143 |
144 | //fail:
145 | g_free(buf);
146 | cpim_message_free(msg);
147 | return NULL;
148 | }
149 |
150 | char *
151 | cpim_message_create_text(const char *body, const char *from, const char *to, const char *timestamp)
152 | {
153 | return g_strdup_printf(CPIM_MESSAGE_TEMPLATE,
154 | from,
155 | to,
156 | timestamp,
157 | TEXT_CONTENT_TYPE,
158 | "; charset=utf-8",
159 | body);
160 | }
161 |
--------------------------------------------------------------------------------
/pidgin/cpim.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #ifndef _CPIM_H_
20 | #define _CPIM_H_
21 |
22 | #include
23 |
24 | typedef struct {
25 | char *from;
26 | char *to;
27 | char *timestamp;
28 | char *type;
29 |
30 | char *body;
31 | } cpim_message;
32 |
33 | void cpim_message_free(cpim_message *msg);
34 |
35 | cpim_message *cpim_parse_message(const char *text, size_t len);
36 |
37 | char *cpim_message_create_text(const char *body, const char *from, const char *to, const char *timestamp);
38 |
39 | #endif /* _CPIM_H_ */
40 |
--------------------------------------------------------------------------------
/pidgin/gpg.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #include
20 |
21 | #include
22 |
23 | #include "gpg.h"
24 | #include "kontalk.h"
25 |
26 |
27 | static gpgme_ctx_t ctx;
28 |
29 |
30 | gboolean
31 | gpg_init()
32 | {
33 | gboolean check = gpgme_check_version(GPGME_REQUIRED_VERSION) != NULL;
34 | if (check) {
35 | gpgme_error_t err = gpgme_new(&ctx);
36 | if (err) {
37 | purple_debug_fatal(PACKAGE_NAME, "cannot allocate GPGME context!\n");
38 | return FALSE;
39 | }
40 | }
41 |
42 | return check;
43 | }
44 |
45 | void
46 | gpg_free()
47 | {
48 | gpgme_release(ctx);
49 | }
50 |
51 | static gpgme_error_t
52 | gpg_get_key(const char *pattern, gpgme_key_t *key, int secret_only)
53 | {
54 | gpgme_error_t err;
55 |
56 | err = gpgme_op_keylist_start(ctx, pattern, secret_only);
57 | if (err)
58 | goto end;
59 |
60 | err = gpgme_op_keylist_next(ctx, key);
61 | if (err)
62 | goto end;
63 |
64 | end:
65 | gpgme_op_keylist_end(ctx);
66 | return err;
67 | }
68 |
69 | char *
70 | gpg_get_userid(const char *pattern, int secret_only)
71 | {
72 | gpgme_error_t err;
73 | gpgme_key_t key = NULL;
74 | char *uid = NULL;
75 |
76 | err = gpg_get_key(pattern, &key, secret_only);
77 | if (err)
78 | goto end;
79 |
80 | if (key->uids->uid != NULL)
81 | uid = strdup(key->uids->uid);
82 |
83 | end:
84 | if (key != NULL)
85 | gpgme_key_unref(key);
86 | return uid;
87 | }
88 |
89 | // TODO this should return an error like gpg_encrypt
90 | char *
91 | gpg_decrypt(void *data, size_t size, size_t *out_size)
92 | {
93 | gpgme_data_t cipher, plain;
94 | gpgme_data_new_from_mem(&cipher, data, size, 0);
95 | gpgme_data_new(&plain);
96 |
97 | gpgme_error_t err = gpgme_op_decrypt_verify(ctx, cipher, plain);
98 | gpgme_data_release(cipher);
99 | if (err) {
100 | purple_debug_error(PACKAGE_NAME, "cannot decrypt data!\n");
101 | gpgme_data_release(plain);
102 | return NULL;
103 | }
104 |
105 | return gpgme_data_release_and_get_mem(plain, out_size);
106 | }
107 |
108 | void
109 | gpg_data_free(void *data)
110 | {
111 | if (data != NULL)
112 | gpgme_free(data);
113 | }
114 |
115 | gpgme_error_t
116 | gpg_encrypt(const char *rcpt, const char *signer, char *data, size_t size, char **out, size_t *out_size)
117 | {
118 | gpgme_key_t key_sign = NULL, key_encrypt = NULL;
119 | gpgme_data_t cipher = NULL, plain = NULL;
120 | gpgme_error_t err;
121 |
122 | // clear any previous signers
123 | gpgme_signers_clear(ctx);
124 |
125 | // find our signing key
126 | err = gpg_get_key(signer, &key_sign, 1);
127 | if (err) {
128 | purple_debug_warning(PACKAGE_NAME, "cannot find secret key!\n");
129 | goto end;
130 | }
131 |
132 | gpgme_signers_add(ctx, key_sign);
133 |
134 | // find encryption key
135 | err = gpg_get_key(rcpt, &key_encrypt, 0);
136 | if (err) {
137 | purple_debug_warning(PACKAGE_NAME, "cannot find encryption key!\n");
138 | goto end;
139 | }
140 |
141 | // prepare buffers
142 | gpgme_data_new_from_mem(&plain, data, size, 0);
143 | gpgme_data_new(&cipher);
144 |
145 | gpgme_key_t recp[] = { key_encrypt, NULL };
146 | // WARNING working around trust
147 | err = gpgme_op_encrypt_sign(ctx, recp, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher);
148 | if (err) {
149 | gpgme_data_release(cipher);
150 | purple_debug_warning(PACKAGE_NAME, "encryption error!\n");
151 | goto end;
152 | }
153 |
154 | *out = gpgme_data_release_and_get_mem(cipher, out_size);
155 | return GPG_ERR_NO_ERROR;
156 |
157 | end:
158 | if (plain != NULL)
159 | gpgme_data_release(plain);
160 | if (key_sign != NULL)
161 | gpgme_key_unref(key_sign);
162 | if (key_encrypt != NULL)
163 | gpgme_key_unref(key_encrypt);
164 | return err;
165 | }
166 |
167 | const char *
168 | gpg_import_key(void *keydata, size_t size)
169 | {
170 | gpgme_data_t dh;
171 | gpgme_data_new_from_mem(&dh, keydata, size, 0);
172 |
173 | gpgme_error_t err = gpgme_op_import(ctx, dh);
174 | gpgme_data_release(dh);
175 | if (err) {
176 | purple_debug_error(PACKAGE_NAME, "cannot import key!\n");
177 | return NULL;
178 | }
179 |
180 | gpgme_import_result_t result = gpgme_op_import_result(ctx);
181 | if (result != NULL && (result->imported > 0 || result->unchanged > 0)) {
182 | return result->imports->fpr;
183 | }
184 |
185 | return NULL;
186 | }
187 |
--------------------------------------------------------------------------------
/pidgin/gpg.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #ifndef _GPG_H_
20 | #define _GPG_H_
21 |
22 | #include
23 | #include
24 |
25 | #define GPGME_REQUIRED_VERSION "1.4.3"
26 |
27 | gboolean gpg_init();
28 | void gpg_free();
29 |
30 | char *gpg_get_userid(const char *pattern, int secret_only);
31 |
32 | char *gpg_decrypt(void *data, size_t size, size_t *out_size);
33 |
34 | gpgme_error_t gpg_encrypt(const char *rcpt, const char *signer, char *data, size_t size, char **out, size_t *out_size);
35 |
36 | void gpg_data_free(void *data);
37 |
38 | const char *gpg_import_key(void *keydata, size_t size);
39 |
40 | #endif /* _GPG_H_ */
41 |
--------------------------------------------------------------------------------
/pidgin/kontalk.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #define PURPLE_PLUGINS
20 |
21 | #include
22 |
23 | #include
24 | #include
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 |
32 | #include "kontalk.h"
33 | #include "gpg.h"
34 | #include "cpim.h"
35 | #include "tunnel.h"
36 |
37 |
38 | #define SECRET_KEY_PREF "/plugins/gtk/" PACKAGE_NAME "/secret_key"
39 | #define TUN_PORT_PREF "/plugins/gtk/" PACKAGE_NAME "/tun_port"
40 | #define TUN_RELAY_HOST_PREF "/plugins/gtk/" PACKAGE_NAME "/tun_relay_host"
41 | #define TUN_RELAY_PORT_PREF "/plugins/gtk/" PACKAGE_NAME "/tun_relay_port"
42 | #define TUN_CERTIFICATE_PREF "/plugins/gtk/" PACKAGE_NAME "/tun_certificate"
43 | #define TUN_PRIVATE_KEY_PREF "/plugins/gtk/" PACKAGE_NAME "/tun_private_key"
44 |
45 | // this is completely safe since Pidgin is not multithread
46 | #define TIMESTAMP_LEN 25
47 | static char timestamp_buffer[TIMESTAMP_LEN+1] = {0, };
48 | #define TIMESTAMP_FORMAT "%Y-%m-%dT%H:%M:%S%z"
49 |
50 | static char *
51 | extract_oob_url(xmlnode *message)
52 | {
53 | xmlnode *oob = xmlnode_get_child_with_namespace(message, "x", "jabber:x:oob");
54 | if (oob != NULL) {
55 | xmlnode *url = xmlnode_get_child(oob, "url");
56 | if (url != NULL) {
57 | return xmlnode_get_data(url);
58 | }
59 | }
60 |
61 | return NULL;
62 | }
63 |
64 | static xmlnode *
65 | parse_xmpp_stanza(const char *text, gssize size)
66 | {
67 | xmlnode *child = NULL;
68 | xmlnode *doc = xmlnode_from_str(text, size);
69 | if (doc != NULL &&
70 | !strcmp(doc->name, XMPP_ELEMENT) &&
71 | !strcmp(xmlnode_get_namespace(doc), XMPP_NAMESPACE)) {
72 |
73 | child = doc->child;
74 | while (child != NULL) {
75 | if (child->name != NULL) {
76 | child = xmlnode_copy(child);
77 | break;
78 | }
79 | child = child->next;
80 | }
81 | }
82 |
83 | if (doc != NULL) {
84 | xmlnode_free(doc);
85 | }
86 |
87 | return child;
88 | }
89 |
90 | static char *
91 | jabber_get_bare_jid(const char *in)
92 | {
93 | if (in != NULL) {
94 | char *out = g_strdup(in);
95 | char *sep = strstr(out, "/");
96 | if (sep != NULL) {
97 | *sep = '\0';
98 | }
99 | return out;
100 | }
101 |
102 | return NULL;
103 | }
104 |
105 | static char *
106 | jabber_get_domain(const char *in)
107 | {
108 | if (in != NULL) {
109 | char *sep = strstr(in, "@");
110 | if (sep != NULL) {
111 | sep = g_strdup(sep+1);
112 | char *end = strstr(sep, "/");
113 | if (end != NULL) {
114 | *end = '\0';
115 | }
116 | return sep;
117 | }
118 | }
119 |
120 | return NULL;
121 | }
122 |
123 |
124 | static char*
125 | generate_next_id()
126 | {
127 | static guint32 index = 0;
128 |
129 | if (index == 0) {
130 | do {
131 | index = g_random_int();
132 | } while (index == 0);
133 | }
134 |
135 | return g_strdup_printf("purple%x", index++);
136 | }
137 |
138 | static void
139 | request_public_key(PurpleConnection *pc, const char *jid)
140 | {
141 | char *id = generate_next_id();
142 |
143 | xmlnode *iq = xmlnode_new("iq");
144 | xmlnode_set_attrib(iq, "type", "get");
145 | xmlnode_set_attrib(iq, "to", jid);
146 | xmlnode_set_attrib(iq, "id", id);
147 | g_free(id);
148 |
149 | xmlnode *pubkey = xmlnode_new_child(iq, PUBKEY_ELEMENT);
150 | xmlnode_set_namespace(pubkey, PUBKEY_NAMESPACE);
151 |
152 | purple_signal_emit(purple_connection_get_prpl(pc), "jabber-sending-xmlnode",
153 | pc, &iq);
154 |
155 | xmlnode_free(iq);
156 | }
157 |
158 | static void
159 | append_to_tooltip(PurpleBlistNode *node, GString *text, gboolean full)
160 | {
161 | if (full) {
162 | const gchar *note = purple_blist_node_get_string(node, "fingerprint");
163 |
164 | if ((note != NULL) && (*note != '\0')) {
165 | char *tmp, *esc;
166 | purple_markup_html_to_xhtml(note, NULL, &tmp);
167 | esc = g_markup_escape_text(tmp, -1);
168 | g_free(tmp);
169 | g_string_append_printf(text, _("\nFingerprint: %s"), esc);
170 | g_free(esc);
171 | }
172 | }
173 | }
174 |
175 | static void
176 | request_key_cb(PurpleBlistNode *node, gpointer data)
177 | {
178 | if (PURPLE_BLIST_NODE_IS_BUDDY(node)) {
179 | PurpleBuddy *b = (PurpleBuddy *) node;
180 | request_public_key(purple_account_get_connection(purple_buddy_get_account(b)),
181 | purple_buddy_get_name(b));
182 | }
183 | }
184 |
185 | static void
186 | extended_menu_cb(PurpleBlistNode *node, GList **m)
187 | {
188 | PurpleMenuAction *bna = NULL;
189 |
190 | if (purple_blist_node_get_flags(node) & PURPLE_BLIST_NODE_FLAG_NO_SAVE)
191 | return;
192 |
193 | if (PURPLE_BLIST_NODE_IS_BUDDY(node)) {
194 | *m = g_list_append(*m, bna);
195 | bna = purple_menu_action_new(_("Request key"), PURPLE_CALLBACK(request_key_cb), NULL, NULL);
196 | *m = g_list_append(*m, bna);
197 | }
198 | }
199 |
200 | static void
201 | xmlnode_replace_data(xmlnode *node, const char *text, size_t len)
202 | {
203 | xmlnode *c;
204 |
205 | for(c = node->child; c; c = c->next) {
206 | if(c->type == XMLNODE_TYPE_DATA) {
207 | g_free(c->data);
208 | size_t text_len = (len == -1) ? strlen(text) : len;
209 | c->data = g_memdup(text, text_len);
210 | c->data_sz = text_len;
211 | break;
212 | }
213 | }
214 | }
215 |
216 | static gboolean
217 | jid_localpart_equal(const char* jid, const char* localpart)
218 | {
219 | gboolean ret = FALSE;
220 | char* jid2 = NULL;
221 | char* domain = jabber_get_domain(jid);
222 | if (domain != NULL) {
223 | jid2 = g_strdup_printf("%s@%s", localpart, domain);
224 | g_assert(jid2 != NULL);
225 |
226 | ret = (strcasecmp(jid, jid2) == 0);
227 | }
228 |
229 | g_free(domain);
230 | g_free(jid2);
231 | return ret;
232 | }
233 |
234 | static const char *
235 | gpg_userid_get_name(const char* uid)
236 | {
237 | char *sep = strstr(uid, " <");
238 | if (sep != NULL) {
239 | *sep = '\0';
240 | return uid;
241 | }
242 |
243 | return NULL;
244 | }
245 |
246 | static gboolean
247 | rename_user_from_key(PurpleAccount *account, const char *jid, const char *fingerprint)
248 | {
249 | gboolean ret = FALSE;
250 |
251 | // lookup contact in blist
252 | PurpleBuddy *buddy = purple_find_buddy(account, jid);
253 | if (buddy != NULL) {
254 | const char* alias = purple_buddy_get_alias(buddy);
255 |
256 | // server could automatically set the name to the JID or to its localpart
257 | if (alias == NULL || !strcasecmp(jid, alias) || jid_localpart_equal(jid, alias)) {
258 | // extract UID from key
259 | char *uid = gpg_get_userid(fingerprint, 0);
260 | if (uid != NULL) {
261 | // modifies the string in place
262 | const char *name = gpg_userid_get_name(uid);
263 |
264 | if (name != NULL) {
265 | // set local alias
266 | purple_blist_alias_buddy(buddy, name);
267 | serv_alias_buddy(buddy);
268 | purple_debug_misc(PACKAGE_NAME, "user %s renamed to \"%s\"\n",
269 | jid, name);
270 | ret = TRUE;
271 | }
272 |
273 | free(uid);
274 | }
275 | }
276 | }
277 |
278 | return ret;
279 | }
280 |
281 | static gboolean
282 | jabber_iq_received(PurpleConnection *pc, const char *type, const char *id,
283 | const char *from, xmlnode *iq)
284 | {
285 | xmlnode *pubkey;
286 | if (from != NULL && !g_strcmp0(type, "result") &&
287 | (pubkey = xmlnode_get_child_with_namespace(iq, PUBKEY_ELEMENT, PUBKEY_NAMESPACE)) != NULL) {
288 |
289 | // just import the key for now
290 | char* keydata = xmlnode_get_data(pubkey);
291 | if (keydata != NULL && *(g_strchomp(keydata)) != '\0') {
292 | size_t len;
293 | g_base64_decode_inplace(keydata, &len);
294 |
295 | if (len > 0) {
296 | const char* fingerprint = gpg_import_key((void*) keydata, len);
297 | if (fingerprint == NULL) {
298 | purple_debug_warning(PACKAGE_NAME, "error importing public key for %s\n",
299 | from);
300 | }
301 | else {
302 | // create a copy of the string since this appears to go somewhere
303 | fingerprint = g_strdup(fingerprint);
304 | purple_debug_misc(PACKAGE_NAME, "public key for %s imported (fingerprint %s)\n",
305 | from, fingerprint ? fingerprint : "(null)");
306 | // rename the user if necessary
307 | rename_user_from_key(purple_connection_get_account(pc), from, fingerprint);
308 | g_free((char *) fingerprint);
309 | }
310 | }
311 | }
312 |
313 | g_free((gpointer) keydata);
314 |
315 | // packet was processed
316 | return TRUE;
317 | }
318 |
319 | // continue processing
320 | return FALSE;
321 | }
322 |
323 | static gboolean
324 | jabber_message_received(PurpleConnection *pc, const char *type, const char *id,
325 | const char *from, const char *to, xmlnode *message)
326 | {
327 | xmlnode *e2e, *body;
328 | if (from != NULL && !g_strcmp0(type, "chat") &&
329 | (e2e = xmlnode_get_child_with_namespace(message, E2E_ELEMENT, E2E_NAMESPACE)) != NULL) {
330 |
331 | // extract and decode e2e content
332 | char *data = xmlnode_get_data(e2e);
333 | if (data != NULL && *(g_strchomp(data)) != '\0') {
334 | size_t len, out_len;
335 | g_base64_decode_inplace(data, &len);
336 |
337 | if (len > 0) {
338 | // decrypt!
339 | char *text = gpg_decrypt((void *) data, len, &out_len);
340 | if (text != NULL && (body = xmlnode_get_child(message, "body")) != NULL) {
341 | char *body_text, *url = NULL;
342 | size_t body_len;
343 |
344 | // parse Message/CPIM
345 | cpim_message *msg = cpim_parse_message(text, out_len);
346 | if (msg != NULL) {
347 | // TODO check security status
348 | body_text = msg->body;
349 | body_len = strlen(msg->body);
350 |
351 | if (msg->type != NULL && g_str_has_prefix(msg->type, XMPP_CONTENT_TYPE)) {
352 | xmlnode *stanza = parse_xmpp_stanza(body_text, body_len);
353 | if (stanza != NULL && !strcmp(stanza->name, "message")) {
354 | // look for out-of-band
355 | url = extract_oob_url(stanza);
356 | if (url != NULL) {
357 | body_text = url;
358 | body_len = strlen(url);
359 | }
360 | }
361 | }
362 | }
363 | else {
364 | body_text = text;
365 | body_len = out_len;
366 | }
367 |
368 | // inject into body
369 | // WARNING accessing xmlnode internals
370 | xmlnode_replace_data(body, body_text, body_len);
371 |
372 | // free cpim data
373 | cpim_message_free(msg);
374 | // free url
375 | free(url);
376 | }
377 |
378 | gpg_data_free(text);
379 | }
380 | }
381 |
382 | g_free(data);
383 | }
384 |
385 | // continue processing
386 | return FALSE;
387 | }
388 |
389 | static gboolean
390 | jabber_presence_received(PurpleConnection *pc, const char *type,
391 | const char *from, xmlnode *presence)
392 | {
393 | if (from != NULL) {
394 | // store fingerprint regardless of presence type
395 | char* fingerprint = NULL;
396 | xmlnode *pubkey = xmlnode_get_child_with_namespace(presence, PUBKEY_ELEMENT, PUBKEY_NAMESPACE);
397 | if (pubkey != NULL) {
398 | xmlnode *fpr_node = xmlnode_get_child(pubkey, "print");
399 | if (fpr_node != NULL) {
400 | fingerprint = xmlnode_get_data(fpr_node);
401 | if (fingerprint != NULL && *(g_strchomp(fingerprint)) != '\0') {
402 | purple_debug_misc(PACKAGE_NAME, "public key fingerprint for %s: %s\n",
403 | from, fingerprint);
404 |
405 | // retrieve buddy from name
406 | PurpleBuddy *buddy = purple_find_buddy(purple_connection_get_account(pc), from);
407 | if (buddy != NULL) {
408 | char* uid = NULL;
409 |
410 | // is the fingerprint changed?
411 | const char* old_fingerprint = purple_blist_node_get_string
412 | (&buddy->node, "fingerprint");
413 | char* jid = jabber_get_bare_jid(from);
414 |
415 | // ...or maybe the key doesn't exist?
416 | if (g_strcmp0(old_fingerprint, fingerprint) || !(uid = gpg_get_userid(fingerprint, 0))) {
417 | // fingerprint changed or key not found, request key
418 | request_public_key(pc, jid);
419 | }
420 | else {
421 | // do we need to rename the user?
422 | rename_user_from_key(purple_connection_get_account(pc), jid, fingerprint);
423 | }
424 |
425 | g_free(jid);
426 | free(uid);
427 |
428 | // store fingerprint
429 | purple_blist_node_set_string(&buddy->node, "fingerprint", fingerprint);
430 | }
431 | else {
432 | purple_debug_warning(PACKAGE_NAME, "buddy %s not found!\n", from);
433 | }
434 |
435 | }
436 |
437 | g_free((gpointer) fingerprint);
438 | }
439 | }
440 | }
441 |
442 | // continue processing
443 | return FALSE;
444 | }
445 |
446 | static xmlnode *
447 | create_encryption_node(const char *cipher, size_t cipher_len)
448 | {
449 | xmlnode *node = xmlnode_new(E2E_ELEMENT);
450 | xmlnode_set_namespace(node, E2E_NAMESPACE);
451 |
452 | // encode data
453 | char *encoded = g_base64_encode((unsigned char *) cipher, cipher_len);
454 | if (encoded) {
455 | xmlnode_insert_data(node, encoded, -1);
456 | return node;
457 | }
458 |
459 | xmlnode_free(node);
460 | return NULL;
461 | }
462 |
463 | static const char *
464 | get_current_timestamp()
465 | {
466 | time_t ts = time(NULL);
467 | strftime(timestamp_buffer, TIMESTAMP_LEN, TIMESTAMP_FORMAT, gmtime(&ts));
468 | return timestamp_buffer;
469 | }
470 |
471 | static gboolean
472 | jabber_xmlnode_sending(PurpleConnection *pc, xmlnode **packet)
473 | {
474 | xmlnode *body;
475 | char *cpim, *text;
476 | const char *to;
477 | char *cipher;
478 | const char *fingerprint = NULL;
479 | size_t cipher_len;
480 |
481 | // create CPIM message from body node
482 | if (!strcmp((*packet)->name, "message") && (body = xmlnode_get_child(*packet, "body")) != NULL) {
483 | to = xmlnode_get_attrib(*packet, "to");
484 |
485 | // retrieve fingerprint for user
486 | PurpleBuddy *buddy = purple_find_buddy(pc->account, to);
487 | if (buddy != NULL) {
488 | // is the fingerprint changed?
489 | fingerprint = purple_blist_node_get_string
490 | (&buddy->node, "fingerprint");
491 |
492 | if (fingerprint == NULL) {
493 | purple_debug_warning(PACKAGE_NAME, "buddy %s has no fingerprint!\n", to);
494 | return FALSE;
495 | }
496 | }
497 | else {
498 | purple_debug_warning(PACKAGE_NAME, "buddy %s not found!\n", to);
499 | return FALSE;
500 | }
501 |
502 | if (fingerprint != NULL) {
503 | text = xmlnode_get_data(body);
504 |
505 | const char *secret_key = purple_prefs_get_string(SECRET_KEY_PREF);
506 |
507 | char *sender = gpg_get_userid(secret_key, 1);
508 | char *recipient = gpg_get_userid(fingerprint, 0);
509 | if (sender == NULL) {
510 | purple_debug_error(PACKAGE_NAME, "unable to find secret key!\n");
511 | free(text);
512 | return FALSE;
513 | }
514 | if (recipient == NULL) {
515 | purple_debug_warning(PACKAGE_NAME, "unable to find public key for %s!\n", to);
516 | free(text);
517 | return FALSE;
518 | }
519 |
520 | cpim = cpim_message_create_text(text, sender, recipient, get_current_timestamp());
521 | free(text);
522 | free(sender);
523 | free(recipient);
524 |
525 | if (!gpg_encrypt(fingerprint, secret_key, cpim, strlen(cpim), &cipher, &cipher_len)) {
526 | xmlnode *child = create_encryption_node(cipher, cipher_len);
527 | gpg_data_free(cipher);
528 |
529 | if (child != NULL) {
530 | // replace body with a dummy
531 | // WARNING accessing xmlnode internals
532 | xmlnode_replace_data(body, _("(encrypted)"), -1);
533 | // add the encrypted part
534 | xmlnode_insert_child(*packet, child);
535 | }
536 | }
537 |
538 | g_free(cpim);
539 | }
540 | }
541 |
542 | // continue processing
543 | return FALSE;
544 | }
545 |
546 | static PurplePluginPrefFrame *
547 | pref_frame(PurplePlugin *plugin)
548 | {
549 | PurplePluginPref *pref;
550 | PurplePluginPrefFrame *frame = purple_plugin_pref_frame_new();
551 |
552 | pref = purple_plugin_pref_new_with_name_and_label(SECRET_KEY_PREF,
553 | _("Secret key"));
554 | purple_plugin_pref_frame_add(frame, pref);
555 |
556 | pref = purple_plugin_pref_new_with_name_and_label(TUN_CERTIFICATE_PREF,
557 | _("Certificate file"));
558 | purple_plugin_pref_frame_add(frame, pref);
559 |
560 | pref = purple_plugin_pref_new_with_name_and_label(TUN_PRIVATE_KEY_PREF,
561 | _("Private key file"));
562 | purple_plugin_pref_frame_add(frame, pref);
563 |
564 | pref = purple_plugin_pref_new_with_name_and_label(TUN_PORT_PREF,
565 | _("Tunnel listen port"));
566 | purple_plugin_pref_set_bounds(pref, 0, 65536);
567 | purple_plugin_pref_frame_add(frame, pref);
568 |
569 | pref = purple_plugin_pref_new_with_name_and_label(TUN_RELAY_HOST_PREF,
570 | _("Server host"));
571 | purple_plugin_pref_frame_add(frame, pref);
572 |
573 | pref = purple_plugin_pref_new_with_name_and_label(TUN_RELAY_PORT_PREF,
574 | _("Server port"));
575 | purple_plugin_pref_set_bounds(pref, 0, 65536);
576 | purple_plugin_pref_frame_add(frame, pref);
577 |
578 | return frame;
579 | }
580 |
581 | static gboolean
582 | plugin_load(PurplePlugin *plugin)
583 | {
584 | // init gpgme
585 | if (!gpg_init()) {
586 | purple_notify_message(plugin, PURPLE_NOTIFY_MSG_ERROR, PACKAGE_TITLE,
587 | // TODO i18n
588 | "GPGME >= " GPGME_REQUIRED_VERSION " is required.", NULL, NULL, NULL);
589 | return FALSE;
590 | }
591 |
592 | void *jabber_handle = purple_plugins_find_with_id("prpl-jabber");
593 |
594 | if (jabber_handle) {
595 | // init signals
596 | purple_signal_connect(jabber_handle, "jabber-receiving-presence",
597 | plugin, PURPLE_CALLBACK(jabber_presence_received), NULL);
598 | // TODO convert to jabber-register-namespace-watcher?
599 | purple_signal_connect(jabber_handle, "jabber-receiving-iq",
600 | plugin, PURPLE_CALLBACK(jabber_iq_received), NULL);
601 | purple_signal_connect(jabber_handle, "jabber-receiving-message",
602 | plugin, PURPLE_CALLBACK(jabber_message_received), NULL);
603 | purple_signal_connect(jabber_handle, "jabber-sending-xmlnode",
604 | plugin, PURPLE_CALLBACK(jabber_xmlnode_sending), NULL);
605 |
606 | purple_signal_connect(pidgin_blist_get_handle(), "drawing-tooltip",
607 | plugin, PURPLE_CALLBACK(append_to_tooltip), NULL);
608 | purple_signal_connect(purple_blist_get_handle(), "blist-node-extended-menu",
609 | plugin, PURPLE_CALLBACK(extended_menu_cb), NULL);
610 |
611 | const guint16 tun_port = purple_prefs_get_int(TUN_PORT_PREF);
612 | if (tun_port > 0 && tun_port <= 65536) {
613 | const gchar* relay_host = purple_prefs_get_string(TUN_RELAY_HOST_PREF);
614 | const guint16 relay_port = purple_prefs_get_int(TUN_RELAY_PORT_PREF);
615 | const gchar* certificate = purple_prefs_get_string(TUN_CERTIFICATE_PREF);
616 | const gchar* private_key = purple_prefs_get_string(TUN_PRIVATE_KEY_PREF);
617 | if ((certificate != NULL && strlen(certificate) == 0) ||
618 | (private_key != NULL && strlen(private_key) == 0)) {
619 | certificate = private_key = NULL;
620 | }
621 |
622 | // TODO we should use SRV discovery and host/port as fallback
623 | // TODO extract service name from account (host property)
624 | TunnelError tun_err = tunnel_start(tun_port, relay_host, relay_host, relay_port,
625 | certificate, private_key);
626 | if (tun_err == TUN_ERR_LISTEN) {
627 | purple_notify_message(plugin, PURPLE_NOTIFY_MSG_WARNING, PACKAGE_TITLE,
628 | _("Unable to create tunnel service. Is the configured port already used?"), NULL, NULL, NULL);
629 | }
630 | else if (tun_err == TUN_ERR_CERTIFICATE) {
631 | purple_notify_message(plugin, PURPLE_NOTIFY_MSG_WARNING, PACKAGE_TITLE,
632 | _("Unable to load certificate for Kontalk."), NULL, NULL, NULL);
633 | }
634 | }
635 |
636 | return TRUE;
637 | }
638 |
639 | return FALSE;
640 | }
641 |
642 | static gboolean
643 | plugin_unload(PurplePlugin *plugin)
644 | {
645 | purple_signals_disconnect_by_handle(plugin);
646 | gpg_free();
647 | tunnel_stop();
648 |
649 | return TRUE;
650 | }
651 |
652 | static PurplePluginUiInfo prefs_info = {
653 | pref_frame,
654 | 0,
655 | NULL,
656 | NULL,
657 | NULL,
658 | NULL,
659 | NULL
660 | };
661 |
662 | static PurplePluginInfo info = {
663 | PURPLE_PLUGIN_MAGIC,
664 | PURPLE_MAJOR_VERSION,
665 | PURPLE_MINOR_VERSION,
666 | PURPLE_PLUGIN_STANDARD,
667 | PIDGIN_PLUGIN_TYPE,
668 | 0,
669 | NULL,
670 | PURPLE_PRIORITY_DEFAULT,
671 |
672 | PLUGIN_ID,
673 | NULL,
674 | PACKAGE_VERSION,
675 |
676 | NULL,
677 | NULL,
678 | PLUGIN_AUTHOR,
679 | PLUGIN_WEBSITE,
680 |
681 | plugin_load,
682 | plugin_unload,
683 | NULL,
684 |
685 | NULL,
686 | NULL,
687 | &prefs_info,
688 | NULL,
689 | NULL,
690 | NULL,
691 | NULL,
692 | NULL
693 | };
694 |
695 | static void
696 | init_plugin(PurplePlugin *plugin)
697 | {
698 | #ifdef ENABLE_NLS
699 | bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
700 | bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
701 | #endif /* ENABLE_NLS */
702 |
703 | info.name = _("Kontalk integration");
704 | info.summary = _("Provides support for features used by a Kontalk server.");
705 | info.description = _("Provides support for encryption, key management, media exchange,"
706 | " registration and authentication for a Kontalk server");
707 |
708 | purple_prefs_add_none("/plugins");
709 | purple_prefs_add_none("/plugins/gtk");
710 | purple_prefs_add_none("/plugins/gtk/" PACKAGE_NAME);
711 |
712 | purple_prefs_add_string(SECRET_KEY_PREF, "");
713 | // TODO until a checkbox is implemented, make this zero to disable internal tunnel
714 | purple_prefs_add_int(TUN_PORT_PREF, 0 /*DEFAULT_TUNNEL_PORT*/);
715 | purple_prefs_add_string(TUN_RELAY_HOST_PREF, DEFAULT_RELAY_HOST);
716 | purple_prefs_add_int(TUN_RELAY_PORT_PREF, DEFAULT_RELAY_PORT);
717 | purple_prefs_add_string(TUN_CERTIFICATE_PREF, "");
718 | purple_prefs_add_string(TUN_PRIVATE_KEY_PREF, "");
719 | }
720 |
721 | PURPLE_INIT_PLUGIN(kontalk, init_plugin, info)
722 |
--------------------------------------------------------------------------------
/pidgin/kontalk.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #ifndef _KONTALK_H_
20 | #define _KONTALK_H_
21 |
22 | // this is not translatable
23 | #define PACKAGE_TITLE "Kontalk"
24 |
25 | #define PLUGIN_ID "gtk-" PACKAGE_NAME
26 | #define PLUGIN_AUTHOR "Kontalk devteam "
27 | #define PLUGIN_WEBSITE "http://www.kontalk.org/"
28 |
29 | #define PUBKEY_ELEMENT "pubkey"
30 | #define PUBKEY_NAMESPACE "urn:xmpp:pubkey:2"
31 |
32 | #define DEFAULT_TUNNEL_PORT 5224
33 | #define DEFAULT_RELAY_HOST "beta.kontalk.net"
34 | #define DEFAULT_RELAY_PORT 5222
35 |
36 | #define E2E_ELEMENT "e2e"
37 | #define E2E_NAMESPACE "urn:ietf:params:xml:ns:xmpp-e2e"
38 |
39 | #define XMPP_CONTENT_TYPE "application/xmpp+xml"
40 | #define XMPP_ELEMENT "xmpp"
41 | #define XMPP_NAMESPACE "jabber:client"
42 |
43 | #endif /* _KONTALK_H_ */
44 |
--------------------------------------------------------------------------------
/pidgin/tunnel.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #include "tunnel.h"
20 |
21 | #include
22 | #include
23 | #include
24 |
25 | // data key in client connection for getting the connection to the server
26 | #define DATA_RELAY "connection_relay"
27 | // data key in server (relay) connection for getting the connection to the client
28 | #define DATA_CLIENT "connection_client"
29 | // data key in server (relay) connection for the TLS connection
30 | #define DATA_TLS "connection_tls"
31 | // data key for input buffer
32 | #define DATA_BUFFER_IN "buffer_in"
33 | // data key for output buffer (temporary - will be destroyed when operation is finished)
34 | #define DATA_BUFFER_OUT "buffer_out"
35 |
36 | #define BUF_LEN 8192
37 |
38 | #define TEXT_INIT ""
39 | #define TEXT_STARTTLS ""
40 | #define TEXT_SSL_REPLY1 ""
41 | #define TEXT_SSL_REPLY2 ""
42 | #define TEXT_MECH_EXT "EXTERNAL"
43 | #define TEXT_MECH_PLAIN "PLAIN"
44 | #define TEXT_PLAIN "PLAIN"
45 | #define TEXT_EXTERNAL "EXTERNAL"
46 | #define REGEX_AUTH "(.*)$"
47 |
48 | static GSocketService *main_service = NULL;
49 | static gchar *relay_service_name = NULL;
50 | static gchar *relay_server_host = NULL;
51 | static guint16 relay_server_port;
52 | static GTlsCertificate *user_certificate = NULL;
53 | static GSocketClient *relay_client = NULL;
54 | // compiled at first usage
55 | static GRegex *auth_regex = NULL;
56 |
57 | // https://ubuntuforums.org/showthread.php?t=1309881&p=8216359#post8216359
58 | // adapted for GLib
59 | static gchar *
60 | str_replace(const gchar *orig_str, const gchar *old_token, const gchar *new_token)
61 | {
62 | gchar *new_str = NULL;
63 | const gchar* pos = strstr(orig_str, old_token);
64 |
65 | if (pos) {
66 | new_str = g_malloc0(strlen(orig_str) - strlen(old_token) + strlen(new_token) + 1);
67 |
68 | strncpy(new_str, orig_str, pos - orig_str);
69 | strcat(new_str, new_token);
70 | strcat(new_str, pos + strlen(old_token));
71 | }
72 |
73 | return new_str;
74 | }
75 |
76 | static gboolean
77 | regex_match(const gchar *buf, gsize len, const gchar *regex)
78 | {
79 | if (auth_regex == NULL) {
80 | GError *err = NULL;
81 | auth_regex = g_regex_new(REGEX_AUTH, 0, 0, &err);
82 | if (err != NULL) {
83 | purple_debug_warning(PACKAGE_NAME, "error compiling regex: %s\n",
84 | err->message);
85 | g_error_free(err);
86 | return FALSE;
87 | }
88 | }
89 |
90 | GError *err = NULL;
91 | GMatchInfo *match = NULL;
92 | gboolean ret = g_regex_match_full(auth_regex, buf, len, 0, 0, &match, &err);
93 |
94 | if (err != NULL) {
95 | purple_debug_warning(PACKAGE_NAME, "error matching regex: %s\n",
96 | err->message);
97 | g_error_free(err);
98 | return FALSE;
99 | }
100 | if (match != NULL) {
101 | g_match_info_free(match);
102 | }
103 |
104 | return ret;
105 | }
106 |
107 | static void
108 | relay_read_cb(GInputStream *relay_in, GAsyncResult *res, GSocketConnection *relay_conn);
109 |
110 | static void
111 | close_stream(void *stream)
112 | {
113 | g_io_stream_close(G_IO_STREAM(stream), NULL, NULL);
114 | g_object_unref(G_OBJECT(stream));
115 | }
116 |
117 | static void
118 | stream_write_async(GIOStream *conn, gchar *buf, gsize len, GAsyncReadyCallback callback, gpointer user_data)
119 | {
120 | GOutputStream *out = g_io_stream_get_output_stream(conn);
121 | g_assert(out != NULL);
122 |
123 | g_object_set_data(G_OBJECT(out), DATA_BUFFER_OUT, buf);
124 | g_output_stream_write_all_async(out, buf, len,
125 | G_PRIORITY_DEFAULT, NULL, callback,
126 | user_data != NULL ? user_data : conn);
127 | }
128 |
129 | static void
130 | client_event(GSocketClient *client, GSocketClientEvent event, GSocketConnectable *connectable,
131 | GIOStream *connection, gpointer user_data)
132 | {
133 | // TODO
134 | purple_debug_misc(PACKAGE_NAME, "relay connection event: %d\n", event);
135 | }
136 |
137 | static void
138 | relay_write_cb(GOutputStream *relay_out, GAsyncResult *res, GSocketConnection *relay_conn)
139 | {
140 | // sent data can be destroyed now
141 | gchar *buf = g_object_get_data(G_OBJECT(relay_out), DATA_BUFFER_OUT);
142 | g_free(buf);
143 |
144 | GError *err = NULL;
145 | if (!g_output_stream_write_all_finish(relay_out, res, NULL, &err)) {
146 | purple_debug_warning(PACKAGE_NAME, "error writing to server: %s\n",
147 | err->message);
148 | g_error_free(err);
149 |
150 | // disconnect client
151 | GSocketConnection *client_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_CLIENT);
152 | if (client_conn != NULL)
153 | close_stream(client_conn);
154 |
155 | // disconnect server
156 | close_stream(relay_conn);
157 | }
158 | }
159 |
160 | static void
161 | client_read_cb(GInputStream *client_in, GAsyncResult *res, GSocketConnection *client_conn)
162 | {
163 | GSocketConnection *relay_conn = g_object_get_data(G_OBJECT(client_conn), DATA_RELAY);
164 | if (relay_conn == NULL) {
165 | // object has been destroyed
166 | return;
167 | }
168 |
169 | GError *err = NULL;
170 | gssize buf_len = g_input_stream_read_finish(client_in, res, &err);
171 |
172 | // error
173 | if (buf_len < 0) {
174 | purple_debug_warning(PACKAGE_NAME, "error reading from client: %s\n",
175 | err->message);
176 | g_error_free(err);
177 | // disconnect both sides
178 | close_stream(relay_conn);
179 | close_stream(client_conn);
180 | }
181 | // connection closed from client
182 | else if (buf_len == 0) {
183 | purple_debug_info(PACKAGE_NAME, "connection from client closed.\n");
184 | // disconnect server
185 | close_stream(relay_conn);
186 | }
187 | else {
188 | gchar *buf = g_object_get_data(G_OBJECT(client_conn), DATA_BUFFER_IN);
189 | if (buf != NULL) {
190 | #ifdef DEBUG
191 | purple_debug_misc(PACKAGE_NAME, "client: \"%.*s\"\n", (int) buf_len, buf);
192 | #endif
193 |
194 | // forward data to server
195 | GIOStream *tls_relay_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_TLS);
196 | g_assert(tls_relay_conn != NULL);
197 |
198 | gchar *out;
199 |
200 | // check for PLAIN auth and replace it with EXTERNAL to fool the server
201 | if (regex_match(buf, buf_len, REGEX_AUTH)) {
202 | // replace PLAIN with EXTERNAL
203 | // we are doing string operation so it's better to stringify the buffer
204 | // TODO this could be avoided by modifying str_replace to accept length
205 | out = g_memdup(buf, buf_len);
206 | gchar *buf_str = g_strndup(buf, buf_len);
207 | gchar *buf_fixed = str_replace(buf_str, TEXT_PLAIN, TEXT_EXTERNAL);
208 | g_free(buf_str);
209 |
210 | out = buf_fixed;
211 | buf_len = strlen(buf_fixed);
212 | }
213 | else {
214 | out = g_memdup(buf, buf_len);
215 | }
216 |
217 | stream_write_async(G_IO_STREAM(tls_relay_conn), out, buf_len, (GAsyncReadyCallback) relay_write_cb, relay_conn);
218 |
219 | // read again
220 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(client_conn)),
221 | buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) client_read_cb, client_conn);
222 | }
223 | }
224 | }
225 |
226 | static void
227 | relay_tls_handshake_cb(GTlsClientConnection *tls_relay_conn, GAsyncResult *res, GSocketConnection *relay_conn)
228 | {
229 | GSocketConnection *client_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_CLIENT);
230 |
231 | GError *err = NULL;
232 | if (!g_tls_connection_handshake_finish(G_TLS_CONNECTION(tls_relay_conn), res, &err)) {
233 | purple_debug_warning(PACKAGE_NAME, "error handshaking TLS with server: %s\n",
234 | err->message);
235 | g_error_free(err);
236 |
237 | // disconnect client
238 | if (client_conn != NULL)
239 | close_stream(client_conn);
240 |
241 | // disconnect server
242 | close_stream(relay_conn);
243 |
244 | return;
245 | }
246 |
247 | purple_debug_misc(PACKAGE_NAME, "TLS handshake with server completed.\n");
248 |
249 | // we got TLS from the relay server
250 | // now we need to flush out data from the client
251 | // so start reading from the client socket (i.e. from Pidgin)
252 | void *client_buf = g_malloc0(BUF_LEN);
253 | // this will release the buffer on object destruction
254 | g_object_set_data_full(G_OBJECT(client_conn), DATA_BUFFER_IN, client_buf, g_free);
255 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(client_conn)),
256 | client_buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) client_read_cb, client_conn);
257 |
258 | // also resume reading from the server
259 | void *relay_buf = g_object_get_data(G_OBJECT(relay_conn), DATA_BUFFER_IN);
260 | g_assert(relay_buf != NULL);
261 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(tls_relay_conn)),
262 | relay_buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) relay_read_cb, relay_conn);
263 | }
264 |
265 | static void
266 | start_tls(GSocketConnection *relay_conn)
267 | {
268 | // TODO server identity?
269 | GTlsClientConnection *tls_relay_conn = G_TLS_CLIENT_CONNECTION
270 | (g_tls_client_connection_new(G_IO_STREAM(relay_conn), NULL, NULL));
271 | g_assert(tls_relay_conn != NULL);
272 |
273 | // load client certificate
274 | g_tls_connection_set_certificate(G_TLS_CONNECTION(tls_relay_conn),
275 | user_certificate);
276 |
277 | // store our TLS connection object
278 | g_object_set_data(G_OBJECT(relay_conn), DATA_TLS, tls_relay_conn);
279 |
280 | // start the handshake
281 | g_tls_client_connection_set_validation_flags(tls_relay_conn,
282 | G_TLS_CERTIFICATE_UNKNOWN_CA);
283 | g_tls_connection_handshake_async(G_TLS_CONNECTION(tls_relay_conn),
284 | G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) relay_tls_handshake_cb, relay_conn);
285 | }
286 |
287 | static void
288 | client_write_cb(GOutputStream *client_out, GAsyncResult *res, GSocketConnection *client_conn)
289 | {
290 | // sent data can be destroyed now
291 | gchar *buf = g_object_get_data(G_OBJECT(client_out), DATA_BUFFER_OUT);
292 | g_free(buf);
293 |
294 | GError *err = NULL;
295 | if (!g_output_stream_write_all_finish(client_out, res, NULL, &err)) {
296 | purple_debug_warning(PACKAGE_NAME, "error writing to client: %s\n",
297 | err->message);
298 | g_error_free(err);
299 |
300 | // disconnect server
301 | GSocketConnection *relay_conn = g_object_get_data(G_OBJECT(client_conn), DATA_RELAY);
302 | if (relay_conn != NULL)
303 | close_stream(relay_conn);
304 |
305 | // disconnect client
306 | close_stream(client_conn);
307 | }
308 | }
309 |
310 | static void
311 | relay_read_cb(GInputStream *relay_in, GAsyncResult *res, GSocketConnection *relay_conn)
312 | {
313 | GSocketConnection *client_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_CLIENT);
314 | if (client_conn == NULL) {
315 | // object has been destroyed
316 | return;
317 | }
318 |
319 | GError *err = NULL;
320 | gssize buf_len = g_input_stream_read_finish(relay_in, res, &err);
321 |
322 | // error
323 | if (buf_len < 0) {
324 | purple_debug_warning(PACKAGE_NAME, "error reading from relay server: %s\n",
325 | err->message);
326 | g_error_free(err);
327 | // disconnect both sides
328 | close_stream(relay_conn);
329 | close_stream(client_conn);
330 | }
331 | // connection closed from server
332 | else if (buf_len == 0) {
333 | purple_debug_info(PACKAGE_NAME, "connection from server closed.\n");
334 | // disconnect client
335 | close_stream(client_conn);
336 | }
337 | else {
338 | gchar *buf = g_object_get_data(G_OBJECT(relay_conn), DATA_BUFFER_IN);
339 | if (buf != NULL) {
340 | #ifdef DEBUG
341 | purple_debug_misc(PACKAGE_NAME, "server: \"%.*s\"\n", (int) buf_len, buf);
342 | #endif
343 |
344 | GTlsClientConnection *tls_relay_conn = g_object_get_data
345 | (G_OBJECT(relay_conn), DATA_TLS);
346 |
347 | // in relay mode
348 | if (tls_relay_conn != NULL) {
349 | gchar *out;
350 |
351 | // check for EXTERNAL mechanism and replace it with PLAIN to fool Pidgin
352 | if (memmem(buf, buf_len, TEXT_MECH_EXT, strlen(TEXT_MECH_EXT))) {
353 | // replace EXTERNAL with PLAIN
354 | // we are doing string operation so it's better to stringify the buffer
355 | // TODO this could be avoided by modifying str_replace to accept length
356 | gchar *buf_str = g_strndup(buf, buf_len);
357 | gchar *buf_fixed = str_replace(buf_str, TEXT_MECH_EXT, TEXT_MECH_PLAIN);
358 | g_free(buf_str);
359 |
360 | out = buf_fixed;
361 | buf_len = strlen(buf_fixed);
362 | }
363 | else {
364 | out = g_memdup(buf, buf_len);
365 | }
366 |
367 | // forward data to client
368 | stream_write_async(G_IO_STREAM(client_conn), out, buf_len,
369 | (GAsyncReadyCallback) client_write_cb, NULL);
370 |
371 | // read again
372 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(tls_relay_conn)),
373 | buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) relay_read_cb, relay_conn);
374 |
375 | }
376 | // in autonomous mode
377 | else {
378 | // check for starttls proceed command and start TLS against the relay server
379 | if (memmem(buf, buf_len, TEXT_SSL_REPLY1, strlen(TEXT_SSL_REPLY1)) ||
380 | memmem(buf, buf_len, TEXT_SSL_REPLY1, strlen(TEXT_SSL_REPLY2))) {
381 | start_tls(relay_conn);
382 | }
383 | else {
384 | // read again
385 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(relay_conn)),
386 | buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) relay_read_cb, relay_conn);
387 | }
388 | }
389 | }
390 | }
391 | }
392 |
393 | static void
394 | relay_write_starttls_cb(GOutputStream *relay_out, GAsyncResult *res, GSocketConnection *relay_conn)
395 | {
396 | // data is static, no need to free
397 |
398 | GError *err = NULL;
399 | if (!g_output_stream_write_all_finish(relay_out, res, NULL, &err)) {
400 | purple_debug_warning(PACKAGE_NAME, "error writing to server: %s\n",
401 | err->message);
402 | g_error_free(err);
403 |
404 | // disconnect client
405 | GSocketConnection *client_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_CLIENT);
406 | if (client_conn != NULL)
407 | close_stream(client_conn);
408 |
409 | // disconnect server
410 | close_stream(relay_conn);
411 | }
412 |
413 | // reading part will catch the "proceed" and start TLS handshake
414 | }
415 |
416 | static void
417 | relay_write_init_cb(GOutputStream *relay_out, GAsyncResult *res, GSocketConnection *relay_conn)
418 | {
419 | // sent data can be destroyed now
420 | gchar *buf = g_object_get_data(G_OBJECT(relay_out), DATA_BUFFER_OUT);
421 | g_free(buf);
422 |
423 | GError *err = NULL;
424 | if (!g_output_stream_write_all_finish(relay_out, res, NULL, &err)) {
425 | purple_debug_warning(PACKAGE_NAME, "error writing to server: %s\n",
426 | err->message);
427 | g_error_free(err);
428 |
429 | // disconnect client
430 | GSocketConnection *client_conn = g_object_get_data(G_OBJECT(relay_conn), DATA_CLIENT);
431 | if (client_conn != NULL)
432 | close_stream(client_conn);
433 |
434 | // disconnect server
435 | close_stream(relay_conn);
436 | return;
437 | }
438 |
439 | // request starttls
440 | stream_write_async(G_IO_STREAM(relay_conn), TEXT_STARTTLS, strlen(TEXT_STARTTLS),
441 | (GAsyncReadyCallback) relay_write_starttls_cb, NULL);
442 | }
443 |
444 | /**
445 | * Callback for relay server connection event.
446 | * @param connection the connection from Pidgin
447 | */
448 | static void
449 | relay_connected_cb(GSocketClient *client, GAsyncResult *res, GSocketConnection *client_conn)
450 | {
451 | GError *err = NULL;
452 | GSocketConnection *relay_conn = g_socket_client_connect_to_host_finish(client, res, &err);
453 | if (relay_conn == NULL) {
454 | purple_debug_warning(PACKAGE_NAME, "unable to connect to relay server: %s\n",
455 | err->message);
456 | g_error_free(err);
457 |
458 | // close connection from Pidgin
459 | close_stream(client_conn);
460 | return;
461 | }
462 |
463 | // store a cross-reference to the each other
464 | g_object_set_data(G_OBJECT(client_conn), DATA_RELAY, relay_conn);
465 | g_object_set_data(G_OBJECT(relay_conn), DATA_CLIENT, client_conn);
466 |
467 | // start reading from relay connection (connection to server)
468 | void *relay_buf = g_malloc0(BUF_LEN);
469 | // this will release the buffer on object destruction
470 | g_object_set_data_full(G_OBJECT(relay_conn), DATA_BUFFER_IN, relay_buf, g_free);
471 | g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(relay_conn)),
472 | relay_buf, BUF_LEN, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) relay_read_cb, relay_conn);
473 |
474 | // begin communication with server
475 | gchar *init_out = g_strdup_printf(TEXT_INIT, relay_service_name);
476 | stream_write_async(G_IO_STREAM(relay_conn), init_out, strlen(init_out),
477 | (GAsyncReadyCallback) relay_write_init_cb, NULL);
478 | }
479 |
480 | static gboolean
481 | connect_to_relay_server(GSocketConnection *connection)
482 | {
483 | g_socket_client_connect_to_host_async(relay_client, relay_server_host, relay_server_port,
484 | NULL, (GAsyncReadyCallback) relay_connected_cb, connection);
485 | return TRUE;
486 | }
487 |
488 | static gboolean
489 | socket_incoming(GSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data)
490 | {
491 | GSocketAddress *addr = g_socket_connection_get_remote_address(connection, NULL);
492 | g_assert(addr != NULL);
493 |
494 | gchar *addr_str = g_socket_connectable_to_string(G_SOCKET_CONNECTABLE(addr));
495 | purple_debug_info(PACKAGE_NAME, "incoming connection from %s\n", addr_str);
496 | g_free(addr_str);
497 |
498 | // we intend to use our connection object
499 | g_object_ref(connection);
500 |
501 | // connect to the relay server and go on
502 | connect_to_relay_server(connection);
503 | return TRUE;
504 | }
505 |
506 | static gboolean
507 | load_client_certificate(const gchar *cert_file, const gchar *key_file, GError **error)
508 | {
509 | user_certificate = g_tls_certificate_new_from_files(cert_file, key_file, error);
510 | return user_certificate != NULL;
511 | }
512 |
513 | TunnelError
514 | tunnel_start(guint16 listen_port, const gchar *service_name,
515 | const gchar *server_host, guint16 server_port,
516 | const gchar* cert_file, const gchar *key_file)
517 | {
518 | if (cert_file == NULL || key_file == NULL) {
519 | // configuration error
520 | return TUN_ERR_CONFIG;
521 | }
522 |
523 | GError *err = NULL;
524 |
525 | // load certificate
526 | if (!load_client_certificate(cert_file, key_file, &err)) {
527 | purple_debug_warning(PACKAGE_NAME, "unable to load client certificate: %s\n",
528 | err->message);
529 | g_error_free(err);
530 | return TUN_ERR_CERTIFICATE;
531 | }
532 |
533 | main_service = g_socket_service_new();
534 | g_assert(main_service != NULL);
535 | relay_client = g_socket_client_new();
536 | g_assert(relay_client != NULL);
537 | // client event handler is global so connect it now
538 | g_signal_connect(G_OBJECT(relay_client), "event", G_CALLBACK(client_event), NULL);
539 |
540 | if (!g_socket_listener_add_inet_port(G_SOCKET_LISTENER(main_service), listen_port, NULL, &err)) {
541 | purple_debug_warning(PACKAGE_NAME, "unable to listen on port %d: %s\n",
542 | listen_port, err->message);
543 | g_error_free(err);
544 | return TUN_ERR_LISTEN;
545 | }
546 |
547 | g_signal_connect(G_OBJECT(main_service), "incoming", G_CALLBACK(socket_incoming), NULL);
548 | relay_service_name = g_strdup(service_name);
549 | relay_server_host = g_strdup(server_host);
550 | relay_server_port = server_port;
551 | return TUN_ERR_OK;
552 | }
553 |
554 | void
555 | tunnel_stop()
556 | {
557 | if (main_service != NULL) {
558 | g_socket_service_stop(main_service);
559 | g_socket_listener_close(G_SOCKET_LISTENER(main_service));
560 | g_object_unref(main_service);
561 | g_object_unref(relay_client);
562 | g_free(relay_server_host);
563 | relay_server_host = NULL;
564 | g_free(relay_service_name);
565 | relay_service_name = NULL;
566 | main_service = NULL;
567 | }
568 | }
569 |
--------------------------------------------------------------------------------
/pidgin/tunnel.h:
--------------------------------------------------------------------------------
1 | /*
2 | * Kontalk Pidgin plugin
3 | * Copyright (C) 2017 Kontalk Devteam
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 |
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program. If not, see .
17 | */
18 |
19 | #ifndef _TUNNEL_H_
20 | #define _TUNNEL_H_
21 |
22 | #include
23 |
24 | typedef enum {
25 | TUN_ERR_OK = 0,
26 | TUN_ERR_LISTEN,
27 | TUN_ERR_CERTIFICATE,
28 | TUN_ERR_CONFIG,
29 | } TunnelError;
30 |
31 | TunnelError
32 | tunnel_start(guint16 listen_port, const gchar *service_name,
33 | const gchar *server_host, guint16 server_port,
34 | const gchar* cert_file, const gchar *key_file);
35 |
36 |
37 | void tunnel_stop();
38 |
39 | #endif /* _TUNNEL_H_ */
40 |
--------------------------------------------------------------------------------
/tunnel/README.md:
--------------------------------------------------------------------------------
1 | Generic SSL tunnel bridge
2 | =========================
3 |
4 | This script can be used with any standard XMPP client.
5 | It creates a listening socket (defaults on port 5224) which clients can
6 | connect to.
7 |
8 | To use this script, you need to export your personal key from the Android
9 | app. You will find a zip file called `kontalk-keys.zip` in your external
10 | storage. You need these two files:
11 |
12 | * `kontalk-login.crt`
13 | * `kontalk-login.key`
14 |
15 |
16 | ## Installation
17 |
18 | You can install the script in any directory you like.
19 | You will need:
20 |
21 | * Python >= 2.7
22 | * [Twisted](http://twistedmatrix.com/) >= 13.x
23 |
24 |
25 | ## Usage
26 |
27 | This scripts will listen on a port that your preferred XMPP client will connect
28 | to. Here is an example command line:
29 |
30 | ```
31 | ./ssl_bridge.py -d \
32 | --domain beta.kontalk.net \
33 | -c kontalk-login.crt \
34 | -k kontalk-login.key \
35 | beta.kontalk.net:5222
36 | ```
37 |
38 | The script will listen on the default port 5224 for connections and create a
39 | bridge to `beta.kontalk.net` on port 5222, doing the SSL handshake for you.
40 | Setup your XMPP client to make an **unencrypted connection and to use plain
41 | authentication** (credentials don't matter, they will be discarded by the script,
42 | however you will need to use the domain you passed to `--domain`).
43 | The `-p` parameter is optional and indicates the port it will listen for
44 | incoming connections.
45 | The `-d` parameter is also optional and instructs the script to print debugging
46 | information along with a complete dump of the XMPP traffic between client and
47 | server.
48 |
49 | For more information, please refer to the built-in guide by running:
50 |
51 | ```
52 | ./ssl_bridge.py -h
53 | ```
54 |
--------------------------------------------------------------------------------
/tunnel/ssl_bridge.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | # SSL bridge for XMPP STARTTLS
3 | # This code is in the public domain
4 |
5 | import argparse
6 | import re
7 |
8 | from twisted.internet.protocol import Protocol, Factory
9 | from twisted.internet.endpoints import TCP4ClientEndpoint, connectProtocol
10 | from twisted.internet import ssl, reactor, error
11 |
12 |
13 | PORT = 5222
14 | LISTEN_PORT = 5224
15 |
16 |
17 | class XMPPClient(Protocol):
18 | debug = False
19 | INIT = ''
20 | SSL_INIT = ''
21 | SSL_REPLY = re.compile("$")
22 | SASL_MECHS = re.compile("EXTERNALKONTALK-TOKEN")
23 | SASL_EXTERNAL = re.compile("EXTERNAL")
24 |
25 | def __init__(self, client, domain, cert_file, pkey_file):
26 | self.client = client
27 | self.domain = domain
28 | self.cert_file = cert_file
29 | self.pkey_file = pkey_file
30 | self._buf = ''
31 |
32 | def write(self, data):
33 | """Writes data to the XMPP server."""
34 | if self.transport and self.transport.TLS:
35 | if self.debug:
36 | print 'SEND: %s' % (data, )
37 | self.transport.write(data)
38 | else:
39 | self._buf += data
40 |
41 | def connectionMade(self):
42 | """Connected to XMPP server."""
43 | init = self.INIT % self.domain
44 | if self.debug:
45 | print 'SEND: %s' % (init, )
46 | self.transport.write(init)
47 | reactor.callLater(0.5, self._init2)
48 |
49 | def _init2(self):
50 | self.transport.write(self.SSL_INIT)
51 |
52 | def dataReceived(self, data):
53 | if self.debug:
54 | print 'RECV: %s' % (data, )
55 | if self.SASL_MECHS.search(data):
56 | if self.debug:
57 | print 'filtering SASL mechanisms'
58 | data = self.SASL_MECHS.sub('', data)
59 | elif self.SASL_EXTERNAL.search(data):
60 | if self.debug:
61 | print 'filtering SASL mechanisms'
62 | data = self.SASL_EXTERNAL.sub('PLAIN', data)
63 |
64 | if self.transport.TLS:
65 | self.client.transport.write(data)
66 | elif self.SSL_REPLY.search(data):
67 | if self.debug:
68 | print 'starting TLS'
69 |
70 | with open(self.pkey_file) as keyFile:
71 | with open(self.cert_file) as certFile:
72 | clientCert = ssl.PrivateCertificate.loadPEM(
73 | keyFile.read() + certFile.read())
74 |
75 | ctx = clientCert.options()
76 | self.transport.startTLS(ctx)
77 | if self._buf:
78 | if self.debug:
79 | print 'SEND: %s' % (self._buf, )
80 | self.transport.write(self._buf)
81 | self._buf = None
82 |
83 |
84 | class BridgeProtocol(Protocol):
85 | debug = False
86 | AUTH_INIT = re.compile("(.*)$")
87 |
88 | def __init__(self, addr, domain, host, port, cert_file, pkey_file):
89 | self.addr = addr
90 | self.domain = domain
91 | self.host = host
92 | self.port = port
93 | self.cert_file = cert_file
94 | self.pkey_file = pkey_file
95 | self._endpoint = None
96 | self._buf = ''
97 |
98 | def _connected(self, p):
99 | self._endpoint = None
100 |
101 | def _error(self, fail=None):
102 | self.connectionLost(fail)
103 |
104 | def dataReceived(self, data):
105 | if self.AUTH_INIT.search(data):
106 | data = data.replace('PLAIN', 'EXTERNAL')
107 |
108 | self.client.write(data)
109 | if '' in data:
110 | if self.client.transport:
111 | self.client.transport.loseConnection()
112 | self.transport.loseConnection()
113 |
114 | def connectionMade(self):
115 | if self.debug:
116 | print 'got connection from %s' % (self.addr, )
117 | print 'connecting to %s:%d' % (self.host, self.port)
118 | point = TCP4ClientEndpoint(reactor, self.host, self.port)
119 | self.client = XMPPClient(self, self.domain, self.cert_file, self.pkey_file)
120 | d = connectProtocol(point, self.client)
121 | d.addCallback(self._connected)
122 | d.addErrback(self._error)
123 | self._endpoint = d
124 |
125 | def connectionLost(self, reason=error.ConnectionDone):
126 | if not isinstance(reason, error.ConnectionDone):
127 | print 'lost connection from %s (%s)' % (self.addr, reason)
128 | if self.client.transport:
129 | self.client.transport.loseConnection()
130 | if self._endpoint:
131 | self._endpoint.cancel()
132 | self._endpoint = None
133 |
134 |
135 | class BridgeFactory(Factory):
136 | debug = False
137 |
138 | def __init__(self, domain, host, port, cert_file, pkey_file):
139 | self.domain = domain
140 | self.host = host
141 | self.port = port
142 | self.cert_file = cert_file
143 | self.pkey_file = pkey_file
144 |
145 | def buildProtocol(self, addr):
146 | return BridgeProtocol(addr, self.domain, self.host, self.port,
147 | self.cert_file, self.pkey_file)
148 |
149 |
150 | if __name__ == '__main__':
151 |
152 | parser = argparse.ArgumentParser(description='An XMPP bridge for clients not supporting SSL client certificate authentication.')
153 | parser.add_argument('-d', '--debug', help='enable debug output', action='store_true')
154 | parser.add_argument('-p', '--port', help='listen for local connections on this port (default: %d)' % LISTEN_PORT, default=LISTEN_PORT)
155 | parser.add_argument('--domain', help='use this domain for stream initialization', required=True)
156 | parser.add_argument('-c', '--certificate', help='X.509 certificate file', required=True)
157 | parser.add_argument('-k', '--privatekey', help='X.509 private key file', required=True)
158 | parser.add_argument('address', help='forward connections to this host (host:port)')
159 |
160 | args = parser.parse_args()
161 |
162 | if ':' in args.address:
163 | host, port = args.address.split(':')
164 | port = int(port)
165 | else:
166 | host, port = args.address, PORT
167 |
168 | BridgeFactory.debug = BridgeProtocol.debug = XMPPClient.debug = args.debug
169 |
170 | print 'listening on port %d, forwarding to %s:%d' % (int(args.port), host, port)
171 | reactor.listenTCP(int(args.port), BridgeFactory(args.domain, host, port, args.certificate, args.privatekey))
172 | reactor.run()
173 |
--------------------------------------------------------------------------------