comparison mcabber/mcabber/xmpp.c @ 2189:3b91f6a100f5

PGP: Decrypt our own encrypted messages when Carbons are enabled
author Mikael Berthe <mikael@lilotux.net>
date Sun, 04 Oct 2015 19:07:17 +0200
parents 84252c616919
children ee3a40ffcd8b
comparison
equal deleted inserted replaced
2188:84252c616919 2189:3b91f6a100f5
1 /* 1 /*
2 * xmpp.c -- Jabber protocol handling 2 * xmpp.c -- Jabber protocol handling
3 * 3 *
4 * Copyright (C) 2008-2014 Frank Zschockelt <mcabber@freakysoft.de> 4 * Copyright (C) 2008-2014 Frank Zschockelt <mcabber@freakysoft.de>
5 * Copyright (C) 2005-2014 Mikael Berthe <mikael@lilotux.net> 5 * Copyright (C) 2005-2015 Mikael Berthe <mikael@lilotux.net>
6 * Parts come from the centericq project: 6 * Parts come from the centericq project:
7 * Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua> 7 * Copyright (C) 2002-2005 by Konstantin Klyagin <konst@konst.org.ua>
8 * 8 *
9 * This program is free software; you can redistribute it and/or modify 9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 10 * it under the terms of the GNU General Public License as published by
917 917
918 rname = strchr(from, JID_RESOURCE_SEPARATOR); 918 rname = strchr(from, JID_RESOURCE_SEPARATOR);
919 if (rname) rname++; 919 if (rname) rname++;
920 920
921 #ifdef HAVE_GPGME 921 #ifdef HAVE_GPGME
922 if (enc && gpg_enabled()) { 922 if (gpg_enabled()) {
923 decrypted_pgp = gpg_decrypt(enc); 923 if (enc) {
924 if (decrypted_pgp) { 924 decrypted_pgp = gpg_decrypt(enc);
925 body = decrypted_pgp; 925 if (decrypted_pgp)
926 } 926 body = decrypted_pgp;
927 } 927 }
928 // Check signature of an unencrypted message 928 // Check signature of the unencrypted/decrypted message
929 if (node_signed && gpg_enabled()) 929 if (node_signed)
930 check_signature(bjid, rname, node_signed, decrypted_pgp); 930 check_signature(bjid, rname, node_signed, body);
931 }
931 #endif 932 #endif
932 933
933 // Check for unexpected groupchat messages 934 // Check for unexpected groupchat messages
934 // If we receive a groupchat message from a room we're not a member of, 935 // If we receive a groupchat message from a room we're not a member of,
935 // this is probably a server issue and the best we can do is to send 936 // this is probably a server issue and the best we can do is to send
1102 1103
1103 // Check for carbons! 1104 // Check for carbons!
1104 x = lm_message_node_find_xmlns(m->node, NS_CARBONS_2); 1105 x = lm_message_node_find_xmlns(m->node, NS_CARBONS_2);
1105 gboolean carbons = FALSE; 1106 gboolean carbons = FALSE;
1106 if (x) { 1107 if (x) {
1108 LmMessageNode *xenc;
1109 const char *carbon_name = x->name;
1107 carbons = TRUE; 1110 carbons = TRUE;
1111 // Go 1 level deeper to the forwarded message
1112 x = lm_message_node_find_xmlns(x, NS_FORWARD);
1113 x = lm_message_node_get_child(x, "message");
1114
1115 xenc = lm_message_node_find_xmlns(x, NS_ENCRYPTED);
1116 if (xenc && (p = lm_message_node_get_value(xenc)) != NULL)
1117 enc = p;
1118
1119 if (body && *body && !subject)
1120 ns_signed = lm_message_node_find_xmlns(x, NS_SIGNED);
1121 else
1122 skip_process = TRUE;
1123
1108 // Parse a message that is send to one of our other resources 1124 // Parse a message that is send to one of our other resources
1109 if (!g_strcmp0(x->name, "received")) { 1125 if (!g_strcmp0(carbon_name, "received")) {
1110 // Go 1 level deeper to the forwarded message
1111 x = lm_message_node_find_xmlns(x, NS_FORWARD);
1112 x = lm_message_node_get_child(x, "message");
1113
1114 from = lm_message_node_get_attribute(x, "from"); 1126 from = lm_message_node_get_attribute(x, "from");
1115 if (!from) { 1127 if (!from) {
1116 scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!"); 1128 scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!");
1117 goto handle_messages_return; 1129 goto handle_messages_return;
1118 } 1130 }
1119 g_free(bjid); 1131 g_free(bjid);
1120 bjid = g_strdup(from); 1132 bjid = g_strdup(from);
1121 res = strchr(bjid, JID_RESOURCE_SEPARATOR); 1133 res = strchr(bjid, JID_RESOURCE_SEPARATOR);
1122 if (res) *res++ = 0; 1134 if (res) *res++ = 0;
1123 1135
1124 if (body && *body && !subject && !enc)
1125 ns_signed = lm_message_node_find_xmlns(x, NS_SIGNED);
1126 else
1127 skip_process = TRUE;
1128
1129 // Try to handle forwarded chat state messages 1136 // Try to handle forwarded chat state messages
1130 handle_state_events(from, res, x); 1137 handle_state_events(from, res, x);
1131 1138
1132 scr_LogPrint(LPRINT_DEBUG, "Received incoming carbon from <%s>", from); 1139 scr_LogPrint(LPRINT_DEBUG, "Received incoming carbon from <%s>", from);
1133 1140
1134 } else if (!g_strcmp0(x->name, "sent")) { 1141 } else if (!g_strcmp0(carbon_name, "sent")) {
1135 x = lm_message_node_find_xmlns(x, NS_FORWARD); 1142 guint encrypted = 0;
1136 x = lm_message_node_get_child(x, "message"); 1143 char *decrypted_pgp = NULL;
1137
1138 const char *to= lm_message_node_get_attribute(x, "to"); 1144 const char *to= lm_message_node_get_attribute(x, "to");
1139 if (!to) { 1145 if (!to) {
1140 scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!"); 1146 scr_LogPrint(LPRINT_LOGNORM, "Malformed carbon copy!");
1141 goto handle_messages_return; 1147 goto handle_messages_return;
1142 } 1148 }
1143 g_free(bjid); 1149 g_free(bjid);
1144 bjid = jidtodisp(to); 1150 bjid = jidtodisp(to);
1145 1151
1152 #ifdef HAVE_GPGME
1153 if (gpg_enabled()) {
1154 if (enc) {
1155 decrypted_pgp = gpg_decrypt(enc);
1156 if (decrypted_pgp) {
1157 body = decrypted_pgp;
1158 encrypted = ENCRYPTED_PGP;
1159 }
1160 }
1161 /*
1162 // Check messsage signature
1163 // This won't work here, since check_signature wasn't intended
1164 // to be used to check our own messages.
1165 if (ns_signed)
1166 check_signature(ME, NULL, ns_signed, body);
1167 */
1168 }
1169 #endif
1170
1146 if (body && *body) 1171 if (body && *body)
1147 hk_message_out(bjid, NULL, timestamp, body, 0, NULL); 1172 hk_message_out(bjid, NULL, timestamp, body, encrypted, NULL);
1148 1173
1149 scr_LogPrint(LPRINT_DEBUG, "Received outgoing carbon for <%s>", to); 1174 scr_LogPrint(LPRINT_DEBUG, "Received outgoing carbon for <%s>", to);
1175 g_free(decrypted_pgp);
1150 goto handle_messages_return; 1176 goto handle_messages_return;
1151 } 1177 }
1152 } else { // Not a Carbon 1178 } else { // Not a Carbon
1153 ns_signed = lm_message_node_find_xmlns(m->node, NS_SIGNED); 1179 ns_signed = lm_message_node_find_xmlns(m->node, NS_SIGNED);
1154 } 1180 }