~ubuntu-branches/ubuntu/oneiric/jabberd2/oneiric-security

« back to all changes in this revision

Viewing changes to sx/ack.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolai Spohrer
  • Date: 2008-08-12 16:13:43 UTC
  • mfrom: (1.1.3 upstream) (0.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20080812161343-6trz3r97dtevxd17
Tags: 2.2.1-1ubuntu1
* Merge with Debian unstable (LP: #257130), remaining changes:
  - debian/control:
    + Modify Maintainer field as per spec
    + Depend on libdb4.6-dev instead of libdb4.4-dev
    + Added Conflicts and Replaces: ..., jabber for jabberd2
  - debian/rules: Added libtoolize call (jabberd2 ships with
     an older ltmain.sh version that conflicts with the
     current libtool version)
  - debian/init: create /var/run/jabber directory with correct
     permissions
* Dropped changes:
  - Debian already depends on libpq-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * jabberd - Jabber Open Source Server
 
3
 * Copyright (c) 2007 Tomasz Sterna
 
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; version 2 of the License
 
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
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
 
17
 */
 
18
 
 
19
/*
 
20
 * this sx plugin implements stanza acknowledgements
 
21
 * as described in XEP-0198: Stanza Acknowledgements
 
22
 */
 
23
 
 
24
#include "sx.h"
 
25
 
 
26
#define STREAM_ACK_NS_DECL      " xmlns:ack='" uri_ACK "'"
 
27
 
 
28
static void _sx_ack_header(sx_t s, sx_plugin_t p, sx_buf_t buf) {
 
29
 
 
30
    log_debug(ZONE, "hacking ack namespace decl onto stream header");
 
31
 
 
32
    /* get enough space */
 
33
    _sx_buffer_alloc_margin(buf, 0, strlen(STREAM_ACK_NS_DECL) + 2);
 
34
 
 
35
    /* overwrite the trailing ">" with a decl followed by a new ">" */
 
36
    memcpy(&buf->data[buf->len - 1], STREAM_ACK_NS_DECL ">", strlen(STREAM_ACK_NS_DECL)+1);
 
37
    buf->len += strlen(STREAM_ACK_NS_DECL);
 
38
}
 
39
 
 
40
/** sx features callback */
 
41
static void _sx_ack_features(sx_t s, sx_plugin_t p, nad_t nad) {
 
42
    /* offer feature only when authenticated and not enabled yet */
 
43
    if(s->state == state_OPEN && s->plugin_data[p->index] == NULL)
 
44
        nad_append_elem(nad, -1, "ack:ack", 1);
 
45
}
 
46
 
 
47
/** process handshake packets from the client */
 
48
static int _sx_ack_process(sx_t s, sx_plugin_t p, nad_t nad) {
 
49
    int attr;
 
50
 
 
51
    /* not interested if we're not a server */
 
52
    if(s->type != type_SERVER)
 
53
        return 1;
 
54
 
 
55
    /* only want ack packets */
 
56
    if((NAD_ENS(nad, 0) < 0 || NAD_NURI_L(nad, NAD_ENS(nad, 0)) != strlen(uri_ACK) || strncmp(NAD_NURI(nad, NAD_ENS(nad, 0)), uri_ACK, strlen(uri_COMPRESS)) != 0))
 
57
        return 1;
 
58
 
 
59
    /* pings */
 
60
    if(NAD_ENAME_L(nad, 0) == 4 && strncmp(NAD_ENAME(nad, 0), "ping", 4) == 0) {
 
61
        jqueue_push(s->wbufq, _sx_buffer_new("<ack:pong/>", 11, NULL, NULL), 0);
 
62
        s->want_write = 1;
 
63
 
 
64
        /* handled the packet */
 
65
        nad_free(nad);
 
66
        return 0;
 
67
    }
 
68
 
 
69
    /* enable only when authenticated */
 
70
    if(s->state == state_OPEN && NAD_ENAME_L(nad, 0) == 6 && strncmp(NAD_ENAME(nad, 0), "enable", 6) == 0) {
 
71
        jqueue_push(s->wbufq, _sx_buffer_new("<ack:enabled/>", 14, NULL, NULL), 254);
 
72
        s->want_write = 1;
 
73
 
 
74
        s->plugin_data[p->index] = (void *) 1;
 
75
 
 
76
        /* handled the packet */
 
77
        nad_free(nad);
 
78
        return 0;
 
79
    }
 
80
 
 
81
    /* 'r' or 'a' when enabled */
 
82
    if(s->plugin_data[p->index] != NULL && NAD_ENAME_L(nad, 0) == 1 && (strncmp(NAD_ENAME(nad, 0), "r", 1) == 0 || strncmp(NAD_ENAME(nad, 0), "a", 1) == 0) ) {
 
83
        attr = nad_find_attr(nad, 0, -1, "c", NULL);
 
84
        if(attr >= 0) {
 
85
            char *buf = (char *) malloc(sizeof(char) * (NAD_AVAL_L(nad, attr) + 13 + 1));
 
86
            snprintf(buf, NAD_AVAL_L(nad, attr) + 13 + 1, "<ack:a b='%.*s'/>", NAD_AVAL_L(nad, attr), NAD_AVAL(nad, attr));
 
87
            jqueue_push(s->wbufq, _sx_buffer_new(buf, NAD_AVAL_L(nad, attr) + 13, NULL, NULL), 255);
 
88
            free(buf);
 
89
            s->want_write = 1;
 
90
        }
 
91
        
 
92
        /* handled the packet */
 
93
        nad_free(nad);
 
94
        return 0;
 
95
    }
 
96
 
 
97
    _sx_debug(ZONE, "unhandled ack namespace element '%.*s', dropping packet", NAD_ENAME_L(nad, 0), NAD_ENAME(nad, 0));
 
98
    nad_free(nad);
 
99
    return 0;
 
100
}
 
101
 
 
102
/** args: none */
 
103
int sx_ack_init(sx_env_t env, sx_plugin_t p, va_list args) {
 
104
    log_debug(ZONE, "initialising stanza acknowledgements sx plugin");
 
105
 
 
106
    p->header = _sx_ack_header;
 
107
    p->features = _sx_ack_features;
 
108
    p->process = _sx_ack_process;
 
109
 
 
110
    return 0;
 
111
}