The user principal identity is now used as the JID in all XMPP protocol stanzas.
[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 "ctdl_module.h"
49 #include "serv_xmpp.h"
50
51
52 /*
53  * Output a single roster item, for roster queries or pushes
54  */
55 void xmpp_roster_item(struct CitContext *cptr) {
56         char xmlbuf1[256];
57         char xmlbuf2[256];
58
59         cprintf("<item jid=\"%s\" name=\"%s\" subscription=\"both\">",
60                 xmlesc(xmlbuf1, cptr->cs_principal_id, sizeof xmlbuf1),
61                 xmlesc(xmlbuf2, cptr->user.fullname, sizeof xmlbuf2)
62         );
63         cprintf("<group>%s</group>", xmlesc(xmlbuf1, CtdlGetConfigStr("c_humannode"), sizeof xmlbuf1));
64         cprintf("</item>");
65 }
66
67
68 /* 
69  * Return the results for a "jabber:iq:roster:query"
70  *
71  * Since we are not yet managing a roster, we simply return the entire wholist
72  * (minus any entries for this user -- don't tell me about myself)
73  *
74  */
75 void xmpp_iq_roster_query(void)
76 {
77         struct CitContext *cptr;
78         int nContexts, i;
79
80         syslog(LOG_DEBUG, "xmpp: roster push!");
81         cprintf("<query xmlns=\"jabber:iq:roster\">");
82         cptr = CtdlGetContextArray(&nContexts);
83         if (cptr) {
84                 for (i=0; i<nContexts; i++) {
85                         if (xmpp_is_visible(&cptr[i], CC)) {
86                                 syslog(LOG_DEBUG, "xmpp: rosterizing %s", cptr[i].user.fullname);
87                                 xmpp_roster_item(&cptr[i]);
88                         }
89                 }
90                 free (cptr);
91         }
92         cprintf("</query>");
93 }
94
95
96 /*
97  * Client is doing a namespace query.  These are all handled differently.
98  */
99 void xmpp_query_namespace(char *iq_id, char *iq_from, char *iq_to, char *query_xmlns)
100 {
101         int supported_namespace = 0;
102         int roster_query = 0;
103         char xmlbuf[256];
104         int reply_must_be_from_my_jid = 0;
105
106         /* We need to know before we begin the response whether this is a supported namespace, so
107          * unfortunately all supported namespaces need to be defined here *and* down below where
108          * they are handled.
109          */
110         if (
111                 (!strcasecmp(query_xmlns, "jabber:iq:roster:query"))
112                 || (!strcasecmp(query_xmlns, "jabber:iq:auth:query"))
113                 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query"))
114                 || (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query"))
115         ) {
116                 supported_namespace = 1;
117         }
118
119         syslog(LOG_DEBUG, "xmpp: xmpp_query_namespace(id=%s, from=%s, to=%s, xmlns=%s)", iq_id, iq_from, iq_to, query_xmlns);
120
121         /*
122          * Beginning of query result.
123          */
124
125         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
126                 reply_must_be_from_my_jid = 1;
127         }
128
129         char dom[1024];                                                         // client is expecting to see the reply
130         if (reply_must_be_from_my_jid) {                                        // coming "from" the user's jid
131                 safestrncpy(dom, XMPP->client_jid, sizeof(dom));
132                 char *slash = strchr(dom, '/');
133                 if (slash) {
134                         *slash = 0;
135                 }
136         }
137         else {
138                 safestrncpy(dom, XMPP->client_jid, sizeof(dom));                // client is expecting to see the reply
139                 if (IsEmptyStr(dom)) {                                          // coming "from" the domain of the user's jid
140                         safestrncpy(dom, XMPP->server_name, sizeof(dom));
141                 }
142                 char *at = strrchr(dom, '@');
143                 if (at) {
144                         strcpy(dom, ++at);
145                 }
146                 char *slash = strchr(dom, '/');
147                 if (slash) {
148                         *slash = 0;
149                 }
150         }
151
152         if (supported_namespace) {
153                 cprintf("<iq type=\"result\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
154         }
155         else {
156                 cprintf("<iq type=\"error\" from=\"%s\" ", xmlesc(xmlbuf, dom, sizeof xmlbuf) );
157         }
158         if (!IsEmptyStr(iq_from)) {
159                 cprintf("to=\"%s\" ", xmlesc(xmlbuf, iq_from, sizeof xmlbuf));
160         }
161         cprintf("id=\"%s\">", xmlesc(xmlbuf, iq_id, sizeof xmlbuf));
162
163         /*
164          * Is this a query we know how to handle?
165          */
166
167         if (!strcasecmp(query_xmlns, "jabber:iq:roster:query")) {
168                 roster_query = 1;
169                 xmpp_iq_roster_query();
170         }
171
172         else if (!strcasecmp(query_xmlns, "jabber:iq:auth:query")) {
173                 cprintf("<query xmlns=\"jabber:iq:auth\">"
174                         "<username/><password/><resource/>"
175                         "</query>"
176                 );
177         }
178
179         // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
180         else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#items:query")) {
181                 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
182         }
183
184         // Extension "xep-0030" (http://xmpp.org/extensions/xep-0030.html) (return an empty set of results)
185         else if (!strcasecmp(query_xmlns, "http://jabber.org/protocol/disco#info:query")) {
186                 cprintf("<query xmlns=\"%s\"/>", xmlesc(xmlbuf, query_xmlns, sizeof xmlbuf));
187         }
188
189         /*
190          * If we didn't hit any known query namespaces then we should deliver a
191          * "service unavailable" error (see RFC3921 section 2.4 and 11.1.5.4)
192          */
193
194         else {
195                 syslog(LOG_DEBUG, "xmpp: unknown query namespace '%s' - returning <service-unavailable/>", query_xmlns);
196                 cprintf("<error code=\"503\" type=\"cancel\">"
197                         "<service-unavailable xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/>"
198                         "</error>"
199                 );
200         }
201
202         cprintf("</iq>");
203
204         /* If we told the client who is on the roster, we also need to tell the client
205          * who is *not* on the roster.  (It's down here because we can't do it in the same
206          * stanza; this will be an unsolicited push.)
207          */
208         if (roster_query) {
209                 xmpp_delete_old_buddies_who_no_longer_exist_from_the_client_roster();
210         }
211 }