8482b4b81b2b5cecc037cddb7161e8e51cf4fea2
[citadel.git] / citadel / modules / xmpp / xmpp_query_namespace.c
1 /*
2  * Handle <iq> <get> <query> type situations (namespace queries)
3  *
4  * Copyright (c) 2007-2015 by Art Cancro and citadel.org
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <sys/wait.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <ctype.h>
40 #include <expat.h>
41 #include <libcitadel.h>
42 #include "citadel.h"
43 #include "server.h"
44 #include "citserver.h"
45 #include "support.h"
46 #include "config.h"
47 #include "internet_addressing.h"
48 #include "md5.h"
49 #include "ctdl_module.h"
50 #include "serv_xmpp.h"
51
52
53 /*
54  * Output a single roster item, for roster queries or pushes
55  */
56 void xmpp_roster_item(struct CitContext *cptr) {
57         char xmlbuf1[256];
58         char xmlbuf2[256];
59
60         cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
61                 xmlesc(xmlbuf1, cptr->cs_inet_email, sizeof xmlbuf1),
62                 xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
63         );
64         cprintf("<group>%s</group>", xmlesc(xmlbuf1, config.c_humannode, sizeof xmlbuf1));
65         cprintf("</item>");
66 }
67
68
69 /* 
70  * Return the results for a "jabber:iq:roster:query"
71  *
72  * Since we are not yet managing a roster, we simply return the entire wholist
73  * (minus any entries for this user -- don't tell me about myself)
74  *
75  */
76 void xmpp_iq_roster_query(void)
77 {
78         struct CitContext *cptr;
79         int nContexts, i;
80
81         syslog(LOG_DEBUG, "Roster push!");
82         cprintf("<query xmlns=\"jabber:iq:roster\" rt=\"ass\">");
83         cptr = CtdlGetContextArray(&nContexts);
84         if (cptr) {
85                 for (i=0; i<nContexts; i++) {
86                         if (xmpp_is_visible(&cptr[i], CC)) {
87                                 XMPP_syslog(LOG_DEBUG, "Rosterizing %s\n", cptr[i].user.fullname);
88                                 xmpp_roster_item(&cptr[i]);
89                         }
90                 }
91                 free (cptr);
92         }
93         cprintf("</query>");
94 }
95
96
97
98 /*
99  * Client is doing a namespace query.  These are all handled differently.
100  * A "rumplestiltskin lookup" is the most efficient way to handle this.  Please do not refactor this code.
101  */
102 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
103 {
104         int supported_namespace = 0;
105         int roster_query = 0;
106         char xmlbuf[256];
107
108         /* We need to know before we begin the response whether this is a supported namespace, so
109          * unfortunately all supported namespaces need to be defined here *and* down below where
110          * they are handled.
111          */
112         if (
113                 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
114                 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
115                 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query"))
116                 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query"))
117         ) {
118                 supported_namespace = 1;
119         }
120
121         XMPP_syslog(LOG_DEBUG, "xmpp_query_namespace(id=%s, from=%s, to=%s, xmlns=%s)\n", iq_id, iq_from, iq_to, query_xmlns);
122
123         /*
124          * Beginning of query result.
125          */
126
127         char dom[1024];                                                 // Pidgin is very picky about where this
128         safestrncpy(dom, XMPP->client_jid, sizeof(dom));                // result stanza is "from" - it wants the
129         if (IsEmptyStr(dom)) {                                          // same domain that the user's jid is in.
130                 safestrncpy(dom, XMPP->server_name, sizeof(dom));
131         }
132         char *at = strrchr(dom, '@');
133         if (at) {
134                 strcpy(dom, ++at);
135         }
136         char *slash = strchr(dom, '/');
137         if (slash) {
138                 *slash = 0;
139         }
140
141         if (supported_namespace) {
142                 cprintf("<iq type=\"result\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
143         }
144         else {
145                 cprintf("<iq type=\"error\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
146         }
147         if (!IsEmptyStr(iq_from)) {
148                 cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
149         }
150         cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
151
152         /*
153          * Is this a query we know how to handle?
154          */
155
156         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
157                 roster_query = 1;
158                 xmpp_iq_roster_query();
159         }
160
161         else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
162                 cprintf("<query xmlns=\"jabber:iq:auth\">"
163                         "<username/><password/><resource/>"
164                         "</query>"
165                 );
166         }
167
168         // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
169         else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query")) {
170                 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
171         }
172
173         // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
174         else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query")) {
175                 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
176         }
177
178         /*
179          * If we didn't hit any known query namespaces then we should deliver a
180          * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
181          */
182
183         else {
184                 XMPP_syslog(LOG_DEBUG, "Unknown query namespace '%s' - returning <service-unavailable/>\n", query_xmlns);
185                 cprintf("<error code=\"503\" type=\"cancel\">"
186                         "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
187                         "</error>"
188                 );
189         }
190
191         cprintf("</iq>");
192
193         /* If we told the client who is on the roster, we also need to tell the client
194          * who is *not* on the roster.  (It's down here because we can't do it in the same
195          * stanza; this will be an unsolicited push.)
196          */
197         if (roster_query) {
198                 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
199         }
200 }