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

« back to all changes in this revision

Viewing changes to util/jid.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:
19
19
 */
20
20
 
21
21
#include "util.h"
22
 
 
23
 
#ifdef HAVE_IDN
24
22
#include <stringprep.h>
25
 
#endif
26
23
 
27
24
/** Forward declaration **/
28
25
static jid_t jid_reset_components_internal(jid_t jid, const unsigned char *node, const unsigned char *domain, const unsigned char *resource, int prepare);
29
26
 
30
 
/** preparation cache */
31
 
prep_cache_t prep_cache_new(void) {
32
 
#ifdef HAVE_IDN
33
 
    prep_cache_t pc;
34
 
 
35
 
    pc = (prep_cache_t) malloc(sizeof(struct prep_cache_st));
36
 
    memset(pc, 0, sizeof(struct prep_cache_st));
37
 
 
38
 
    pc->node = xhash_new(301);
39
 
    pc->domain = xhash_new(301);
40
 
    pc->resource = xhash_new(301);
41
 
 
42
 
    return pc;
43
 
#else
44
 
    return NULL;
45
 
#endif
46
 
}
47
 
 
48
 
void prep_cache_free(prep_cache_t pc) {
49
 
#ifdef HAVE_IDN
50
 
    xhash_free(pc->node);
51
 
    xhash_free(pc->domain);
52
 
    xhash_free(pc->resource);
53
 
    free(pc);
54
 
#endif
55
 
}
56
 
 
57
 
char *prep_cache_node_get(prep_cache_t pc, char *from) {
58
 
    return (char *) xhash_get(pc->node, from);
59
 
}
60
 
 
61
 
void prep_cache_node_set(prep_cache_t pc, char *from, char *to) {
62
 
    xhash_put(pc->node, pstrdup(xhash_pool(pc->node), from), (void *) pstrdup(xhash_pool(pc->node), to));
63
 
}
64
 
 
65
 
char *prep_cache_domain_get(prep_cache_t pc, char *from) {
66
 
    return (char *) xhash_get(pc->domain, from);
67
 
}
68
 
 
69
 
void prep_cache_domain_set(prep_cache_t pc, char *from, char *to) {
70
 
    xhash_put(pc->domain, pstrdup(xhash_pool(pc->domain), from), (void *) pstrdup(xhash_pool(pc->domain), to));
71
 
}
72
 
 
73
 
char *prep_cache_resource_get(prep_cache_t pc, char *from) {
74
 
    return (char *) xhash_get(pc->resource, from);
75
 
}
76
 
 
77
 
void prep_cache_resource_set(prep_cache_t pc, char *from, char *to) {
78
 
    xhash_put(pc->resource, pstrdup(xhash_pool(pc->resource), from), (void *) pstrdup(xhash_pool(pc->resource), to));
79
 
}
80
 
 
81
27
/** do stringprep on the pieces */
82
 
int jid_prep_pieces(prep_cache_t pc, char *node, char *domain, char *resource) {
83
 
#ifdef HAVE_IDN
 
28
static int jid_prep_pieces(char *node, char *domain, char *resource) {
84
29
    char str[1024], *prep;
85
30
 
86
 
    /* no cache, so do a real prep */
87
 
    if(pc == NULL) {
88
 
        if(node[0] != '\0')
89
 
            if(stringprep_xmpp_nodeprep(node, 1024) != 0)
90
 
                return 1;
91
 
 
92
 
        if(stringprep_nameprep(domain, 1024) != 0)
93
 
            return 1;
94
 
 
95
 
        if(resource[0] != '\0')
96
 
            if(stringprep_xmpp_resourceprep(node, 1024) != 0)
97
 
                return 1;
98
 
 
99
 
        return 0;
100
 
    }
101
 
 
102
 
    /* cache version */
103
 
    if(node[0] != '\0') {
104
 
        strcpy(str, node);
105
 
        prep = prep_cache_node_get(pc, str);
106
 
        if(prep != NULL)
107
 
            strcpy(node, prep);
108
 
        else {
109
 
            if(stringprep_xmpp_nodeprep(str, 1024) != 0)
110
 
                return 1;
111
 
            prep_cache_node_set(pc, node, str);
112
 
            strcpy(node, str);
113
 
        }
114
 
    }
115
 
 
116
 
    strcpy(str, domain);
117
 
    prep = prep_cache_domain_get(pc, str);
118
 
    if(prep != NULL)
119
 
        strcpy(domain, prep);
120
 
    else {
121
 
        if(stringprep_nameprep(str, 1024) != 0)
122
 
            return 1;
123
 
        prep_cache_domain_set(pc, domain, str);
124
 
        strcpy(domain, str);
125
 
    }
126
 
 
127
 
    if(resource[0] != '\0') {
128
 
        strcpy(str, resource);
129
 
        prep = prep_cache_resource_get(pc, str);
130
 
        if(prep != NULL)
131
 
            strcpy(resource, prep);
132
 
        else {
133
 
            if(stringprep_xmpp_resourceprep(str, 1024) != 0)
134
 
                return 1;
135
 
            prep_cache_resource_set(pc, resource, str);
136
 
            strcpy(resource, str);
137
 
        }
138
 
    }
139
 
 
140
 
#endif
 
31
    if(node[0] != '\0')
 
32
        if(stringprep_xmpp_nodeprep(node, 1024) != 0)
 
33
            return 1;
 
34
 
 
35
    if(stringprep_nameprep(domain, 1024) != 0)
 
36
        return 1;
 
37
 
 
38
    if(resource[0] != '\0')
 
39
        if(stringprep_xmpp_resourceprep(resource, 1024) != 0)
 
40
            return 1;
 
41
 
141
42
    return 0;
142
43
}
143
44
 
169
70
    else
170
71
        resource[0] = '\0';
171
72
 
172
 
    if(jid_prep_pieces(jid->pc, node, domain, resource) != 0)
 
73
    if(jid_prep_pieces(node, domain, resource) != 0)
173
74
        return 1;
174
75
 
175
76
    /* put prepared components into jid */
179
80
}
180
81
 
181
82
/** make a new jid */
182
 
jid_t jid_new(prep_cache_t pc, const unsigned char *id, int len) {
 
83
jid_t jid_new(const unsigned char *id, int len) {
183
84
    jid_t jid, ret;
184
85
 
185
86
    jid = malloc(sizeof(struct jid_st));
186
 
    jid->pc = pc;
187
87
    jid->jid_data = NULL;
188
88
 
189
89
    ret = jid_reset(jid, id, len);
213
113
 
214
114
/** build a jid from an id */
215
115
jid_t jid_reset(jid_t jid, const unsigned char *id, int len) {
216
 
    prep_cache_t pc;
217
116
    unsigned char *myid, *cur, *olddata=NULL;
218
117
 
219
 
    assert((int) jid);
 
118
    assert((int) (jid != NULL));
220
119
 
221
 
    pc = jid->pc;
222
120
    if (jid->jid_data != NULL) {
223
121
        if(jid->jid_data_len != 0)
224
122
            free(jid->jid_data);
227
125
    }
228
126
    memset(jid, 0, sizeof(struct jid_st));
229
127
    jid->dirty = 1;
230
 
    jid->pc = pc;
231
128
    jid->node = "";
232
129
    jid->domain = "";
233
130
    jid->resource = "";
302
199
 
303
200
/** build a jid from components - internal version */
304
201
static jid_t jid_reset_components_internal(jid_t jid, const unsigned char *node, const unsigned char *domain, const unsigned char *resource, int prepare) {
305
 
    prep_cache_t pc;
306
202
    unsigned char *olddata=NULL;
307
203
    int node_l,domain_l,resource_l;
308
204
    int dataStatic;
309
 
 
310
 
    assert((int) jid);
311
 
 
312
 
    pc = jid->pc;
 
205
    jid_static_buf staticTmpBuf;
 
206
 
 
207
    assert((int) (jid != NULL));
 
208
 
313
209
    if(jid->jid_data != NULL)
314
210
        olddata = jid->jid_data; /* Store old data before clearing JID */
315
211
 
319
215
    free(jid->_full);
320
216
 
321
217
    memset(jid, 0, sizeof(struct jid_st));
322
 
    jid->pc = pc;
323
218
 
324
219
    /* get lengths */
325
220
    node_l = strlen(node);
336
231
        resource_l = MAXLEN_JID_COMP;
337
232
    
338
233
    if(dataStatic) {
339
 
        /* use old static buffer */
340
 
        jid->jid_data = olddata;
 
234
        /* use static buffer */
 
235
        jid->jid_data = staticTmpBuf;
341
236
    }
342
237
    else {
343
238
        /* allocate new data buffer */
344
239
        jid->jid_data_len = node_l+domain_l+resource_l+3;
345
 
        jid->jid_data = malloc(jid->jid_data_len);
 
240
        jid->jid_data = realloc(jid->jid_data, jid->jid_data_len);
346
241
    }
347
242
 
348
243
    /* copy to buffer */
369
264
 
370
265
    jid->dirty = 1;
371
266
 
 
267
    if (dataStatic) {
 
268
        jid->jid_data = olddata; /* Return pointer to the original static buffer */
 
269
        memcpy(jid->jid_data,staticTmpBuf,node_l+domain_l+resource_l+3); /* Copy data from tmp buf to original buffer */
 
270
 
 
271
        /* Relocate pointers */
 
272
        jid->node = olddata+(jid->node-(unsigned char *)staticTmpBuf);
 
273
        jid->domain = olddata+(jid->domain-(unsigned char *)staticTmpBuf);
 
274
        jid->resource = olddata+(jid->resource-(unsigned char *)staticTmpBuf);
 
275
    }
 
276
 
372
277
    return jid;
373
278
}
374
279
 
382
287
{
383
288
    if((jid->jid_data != NULL) && (jid->jid_data_len != 0))
384
289
        free(jid->jid_data);
385
 
    free(jid->_user);
386
 
    free(jid->_full);
387
 
    free(jid);
 
290
    if (jid->_user != NULL )
 
291
        free(jid->_user);
 
292
    if (jid->_full != NULL )
 
293
        free(jid->_full);
 
294
    if (jid != NULL )
 
295
        free(jid);
388
296
}
389
297
 
390
298
/** build user and full if they're out of date */