File: jid.c

package info (click to toggle)
jabberd2 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 4,016 kB
  • sloc: ansic: 37,099; sh: 1,108; perl: 656; xml: 561; makefile: 511; python: 238; ruby: 145; sql: 55
file content (525 lines) | stat: -rw-r--r-- 13,191 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
 * jabberd - Jabber Open Source Server
 * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
 *                    Ryan Eatmon, Robert Norris
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
 */

#include "util.h"
#include <stringprep.h>

/** Forward declaration **/
static jid_t jid_reset_components_internal(jid_t jid, const char *node, const char *domain, const char *resource, int prepare);

/** do stringprep on the pieces */
static int jid_prep_pieces(char *node, char *domain, char *resource) {
    if(node[0] != '\0')
        if(stringprep_xmpp_nodeprep(node, 1024) != 0)
            return 1;

    if(stringprep_nameprep(domain, 1024) != 0)
        return 1;

    if(resource[0] != '\0')
        if(stringprep_xmpp_resourceprep(resource, 1024) != 0)
            return 1;

    return 0;
}

/** do stringprep on the piece **/
int jid_prep(jid_t jid)
{
    char node[MAXLEN_JID_COMP+1];
    char domain[MAXLEN_JID_COMP+1];
    char resource[MAXLEN_JID_COMP+1];

    if(jid->node != NULL) {
        strncpy(node, jid->node, MAXLEN_JID_COMP);
        node[MAXLEN_JID_COMP]='\0';
    }
    else
        node[0] = '\0';

    if(jid->domain != NULL) {
        strncpy(domain, jid->domain, MAXLEN_JID_COMP);
        domain[MAXLEN_JID_COMP]='\0';
    }
    else
        domain[0] = '\0';

    if(jid->resource != NULL) {
        strncpy(resource, jid->resource, MAXLEN_JID_COMP);
        resource[MAXLEN_JID_COMP]='\0';
    }
    else
        resource[0] = '\0';

    if(jid_prep_pieces(node, domain, resource) != 0)
        return 1;

    /* put prepared components into jid */
    jid_reset_components_internal(jid, node, domain, resource, 0);

    return 0;
}

/** make a new jid */
jid_t jid_new(const char *id, int len) {
    jid_t jid, ret;

    jid = malloc(sizeof(struct jid_st));
    jid->jid_data = NULL;

    ret = jid_reset(jid, id, len);
    if(ret == NULL) {
        if(len < 0) {
           log_debug(ZONE, "invalid jid: %s", id);
        } else {
           log_debug(ZONE, "invalid jid: %.*s", len, id);
        }
        free(jid);
    }

    return ret;
}

/** Make jid to use static buffer (jid data won't be allocated dynamically, but
 * given buffer will be always used. JID may not be previously used! */
void jid_static(jid_t jid, jid_static_buf *buf)
{
    /* clear jid */
    memset(jid, 0, sizeof(*jid));

    /* set buffer */
    jid->jid_data = (char *)buf;
}


/** build a jid from an id */
jid_t jid_reset(jid_t jid, const char *id, int len) {
    char *myid, *cur, *olddata=NULL;

    assert((int) (jid != NULL));

    if (jid->jid_data != NULL) {
        if(jid->jid_data_len != 0)
            free(jid->jid_data);
        else
            olddata = jid->jid_data; /* store pointer to old data */
    }
    memset(jid, 0, sizeof(struct jid_st));
    jid->dirty = 1;
    jid->node = "";
    jid->domain = "";
    jid->resource = "";

    /* nice empty jid */
    if(id == NULL)
        return jid;

    if(len < 0)
        len = strlen(id);

    if((len == 0) || (len > MAXLEN_JID))
        return NULL;

    if(olddata != NULL)
        myid = olddata; /* use static buffer */
    else {
        jid->jid_data_len = sizeof(char) * (len + 1);
        myid = (char *) malloc(jid->jid_data_len);
    }
    sprintf(myid, "%.*s", len, id);

    /* fail - only a resource or leading @ */
    if(myid[0] == '/' || myid[0] == '@') {
        if(olddata == NULL) free(myid);
        return NULL;
    }

    /* get the resource first */
    cur = strstr(myid, "/");

    if(cur != NULL)
    {
        *cur = '\0';
        cur++;
        if(strlen(cur) > 0) {
            jid->resource = cur;
        } else {
            /* fail - a resource separator but nothing after it */
            if(olddata == NULL) free(myid);
            return NULL;
        }
    }

    /* find the domain */
    cur = strstr(myid, "@");
    if(cur != NULL) {
        *cur = '\0';
        cur++;
        if(strlen(cur) == 0) {
            /* no domain part, bail out */
            if(olddata == NULL) free(myid);
            return NULL;
        }
        jid->domain = cur;
        jid->node = myid;
    } else {
        /* no @, so it's a domain only */
        jid->domain = myid;
    }

    jid->jid_data = myid;

    if(jid_prep(jid) != 0) {
        if(olddata == NULL) free(myid);
        jid->jid_data = NULL;
        return NULL;
    }

    return jid;
}

/** build a jid from components - internal version */
static jid_t jid_reset_components_internal(jid_t jid, const char *node, const char *domain, const char *resource, int prepare) {
    char *olddata=NULL;
    int node_l,domain_l,resource_l;
    int dataStatic;
    jid_static_buf staticTmpBuf;

    assert((int) (jid != NULL));

    if(jid->jid_data != NULL)
        olddata = jid->jid_data; /* Store old data before clearing JID */

    dataStatic = ((jid->jid_data != NULL) && (jid->jid_data_len == 0));

    if (jid->_user != NULL )
        free(jid->_user);
    if (jid->_full != NULL )
        free(jid->_full);

    memset(jid, 0, sizeof(struct jid_st));

    /* get lengths */
    node_l = strlen(node);
    domain_l = strlen(domain);
    resource_l = strlen(resource);

    if(node_l > MAXLEN_JID_COMP)
        node_l = MAXLEN_JID_COMP;

    if(domain_l > MAXLEN_JID_COMP)
        domain_l = MAXLEN_JID_COMP;

    if(resource_l > MAXLEN_JID_COMP)
        resource_l = MAXLEN_JID_COMP;

    if(dataStatic) {
        /* use static buffer */
        jid->jid_data = staticTmpBuf;
    }
    else {
        /* allocate new data buffer */
        jid->jid_data_len = node_l+domain_l+resource_l+3;
        jid->jid_data = realloc(jid->jid_data, jid->jid_data_len);
    }

    /* copy to buffer */
    jid->node = jid->jid_data;
    strncpy(jid->node, node, node_l);
    jid->node[node_l] = 0;

    jid->domain = jid->node + node_l + 1;
    strncpy(jid->domain, domain, domain_l);
    jid->domain[domain_l] = 0;

    jid->resource = jid->domain + domain_l + 1;
    strncpy(jid->resource, resource, resource_l);
    jid->resource[resource_l] = 0;

    /* Free old data buffer. Postponed to this point so that arguments may point (in)to old jid data. */
    if((!dataStatic) && (olddata != NULL))
        free(olddata);

    if(prepare) {
        if(jid_prep(jid) != 0)
            return NULL;
    }

    jid->dirty = 1;

    if (dataStatic) {
        jid->jid_data = olddata; /* Return pointer to the original static buffer */
        memcpy(jid->jid_data,staticTmpBuf,node_l+domain_l+resource_l+3); /* Copy data from tmp buf to original buffer */

        /* Relocate pointers */
        jid->node = olddata+(jid->node-(char *)staticTmpBuf);
        jid->domain = olddata+(jid->domain-(char *)staticTmpBuf);
        jid->resource = olddata+(jid->resource-(char *)staticTmpBuf);
    }

    return jid;
}

/** build a jid from components */
jid_t jid_reset_components(jid_t jid, const char *node, const char *domain, const char *resource) {
    return jid_reset_components_internal(jid, node, domain, resource, 1);
}

/** free a jid */
void jid_free(jid_t jid)
{
    if((jid->jid_data != NULL) && (jid->jid_data_len != 0))
        free(jid->jid_data);
    if (jid->_user != NULL )
        free(jid->_user);
    if (jid->_full != NULL )
        free(jid->_full);
    free(jid);
}

/** build user and full if they're out of date */
void jid_expand(jid_t jid)
{
    int nlen, dlen, rlen, ulen;

    if((!jid->dirty) && (jid->_full))
        return; /* Not dirty & already expanded */

    if(*jid->domain == '\0') {
      /* empty */
      jid->_full = (char*) realloc(jid->_full, 1);
      jid->_full[0] = 0;
      return;
    }

    nlen = strlen(jid->node);
    dlen = strlen(jid->domain);
    rlen = strlen(jid->resource);

    if(nlen == 0) {
        ulen = dlen+1;
        jid->_user = (char*) realloc(jid->_user, ulen);
        strcpy(jid->_user, jid->domain);
    } else {
        ulen = nlen+1+dlen+1;
        jid->_user = (char*) realloc(jid->_user, ulen);
        snprintf(jid->_user, ulen, "%s@%s", jid->node, jid->domain);
    }

    if(rlen == 0) {
        jid->_full = (char*) realloc(jid->_full, ulen);
        strcpy(jid->_full, jid->_user);
    } else {
        jid->_full = (char*) realloc(jid->_full, ulen+1+rlen);
        snprintf(jid->_full, ulen+1+rlen, "%s/%s", jid->_user, jid->resource);
    }

    jid->dirty = 0;
}

/** expand and return the user */
const char *jid_user(jid_t jid)
{
    jid_expand(jid);

    return jid->_user;
}

/** expand and return the full */
const char *jid_full(jid_t jid)
{
    jid_expand(jid);

    return jid->_full;
}

/** compare the user portion of two jids */
int jid_compare_user(jid_t a, jid_t b)
{
    jid_expand(a);
    jid_expand(b);

    return strcmp(a->_user, b->_user);
}

/** compare two full jids */
int jid_compare_full(jid_t a, jid_t b)
{
    jid_expand(a);
    jid_expand(b);

    return strcmp(a->_full, b->_full);
}

/** duplicate a jid */
jid_t jid_dup(jid_t jid)
{
    jid_t new;

    new = (jid_t) malloc(sizeof(struct jid_st));
    memcpy(new, jid, sizeof(struct jid_st));
    if(jid->jid_data != NULL) {
      if(jid->jid_data_len == 0) {
        /* when original jid had static buffer, allocate new dynamic buffer
         * of the same size as has the static buffer */
        jid->jid_data_len = sizeof(jid_static_buf);
      }

      /* allocate & populate new dynamic buffer */
      new->jid_data = malloc(new->jid_data_len);
      memcpy(new->jid_data, jid->jid_data, new->jid_data_len);

      /* relocate pointers */
      if(jid->node[0] == '\0')
          new->node = "";
      else
          new->node = new->jid_data + (jid->node - jid->jid_data);
      if(jid->domain[0] == '\0')
          new->domain = "";
      else
          new->domain = new->jid_data + (jid->domain - jid->jid_data);
      if(jid->resource[0] == '\0')
          new->resource = "";
      else
          new->resource = new->jid_data + (jid->resource - jid->jid_data);
    }
    if(jid->_user)
      new->_user = strdup(jid->_user);
    if(jid->_full)
      new->_full = strdup(jid->_full);

    return new;
}

/** util to search through jids */
int jid_search(jid_t list, jid_t jid)
{
    jid_t cur;
    for(cur = list; cur != NULL; cur = cur->next)
        if(jid_compare_full(cur,jid) == 0)
            return 1;
    return 0;
}

/** remove a jid_t from a list, returning the new list */
jid_t jid_zap(jid_t list, jid_t jid)
{
    jid_t cur, dead;

    if(jid == NULL || list == NULL)
        return NULL;

    /* check first */
    if(jid_compare_full(jid,list) == 0) {
        cur = list->next;
        jid_free(list);
        return cur;
    }

    /* check through the list, stopping at the previous list entry to a matching one */
    cur = list;
    while(cur != NULL)
    {
        if(cur->next == NULL)
            /* none match, so we're done */
            return list;

        if(jid_compare_full(cur->next, jid) == 0)
        {
            /* match, kill it */
            dead = cur->next;
            cur->next = cur->next->next;
            jid_free(dead);

            return list;
        }

        /* loop */
        cur = cur->next;
    }

    /* shouldn't get here */
    return list;
}

/** make a copy of jid, link into list (avoiding dups) */
jid_t jid_append(jid_t list, jid_t jid)
{
    jid_t scan;

    if(list == NULL)
        return jid_dup(jid);

    scan = list;
    while(scan != NULL)
    {
        /* check for dups */
        if(jid_compare_full(scan, jid) == 0)
            return list;

        /* tack it on to the end of the list */
        if(scan->next == NULL)
        {
            scan->next = jid_dup(jid);
            return list;
        }

        scan = scan->next;
    }

    return list;
}

/** create random resource **/
void jid_random_part(jid_t jid, jid_part_t part)
{
    char hashBuf[41];
    char randomBuf[257];
    int i,r;

    /* create random string */
    for(i = 0; i < 256; i++) {
        r = (int) (36.0 * rand() / RAND_MAX);
        randomBuf[i] = (r >= 0 && r <= 0) ? (r + 48) : (r + 87);
    }
    randomBuf[256] = 0;

    /* hash it */
    shahash_r(randomBuf, hashBuf);

    /* change jid */
    switch(part) {
       case jid_NODE:
           jid_reset_components(jid, hashBuf, jid->domain, jid->resource);
           break;

       case jid_DOMAIN: /* unused */
           jid_reset_components(jid, jid->node, hashBuf, jid->resource);
           break;

       case jid_RESOURCE:
           jid_reset_components(jid, jid->node, jid->domain, hashBuf);
           break;
     }

    /* prepare */
    jid_prep(jid);
}