summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/transport/third_party/nICEr/src/stun/stun_server_ctx.c
blob: b92b6b5ab693ee3401dff88935740553e65ce50c (plain)
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
/*
Copyright (c) 2007, Adobe Systems, Incorporated
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of Adobe Systems, Network Resonance nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <string.h>
#include <assert.h>

#include "nr_api.h"
#include "stun.h"

static int nr_stun_server_destroy_client(nr_stun_server_client *clnt);
static int nr_stun_server_send_response(nr_stun_server_ctx *ctx, nr_socket *sock, nr_transport_addr *peer_addr, nr_stun_message *res, nr_stun_server_client *clnt);
static int nr_stun_server_process_request_auth_checks(nr_stun_server_ctx *ctx, nr_stun_message *req, int auth_rule, nr_stun_message *res);


int nr_stun_server_ctx_create(char *label, nr_stun_server_ctx **ctxp)
  {
    int r,_status;
    nr_stun_server_ctx *ctx=0;

    if ((r=nr_stun_startup()))
      ABORT(r);

    if(!(ctx=RCALLOC(sizeof(nr_stun_server_ctx))))
      ABORT(R_NO_MEMORY);

    if(!(ctx->label=r_strdup(label)))
      ABORT(R_NO_MEMORY);

    STAILQ_INIT(&ctx->clients);

    *ctxp=ctx;

    _status=0;
  abort:
    return(_status);
  }

int nr_stun_server_ctx_destroy(nr_stun_server_ctx **ctxp)
  {
    nr_stun_server_ctx *ctx;
    nr_stun_server_client *clnt1,*clnt2;

    if(!ctxp || !*ctxp)
      return(0);

    ctx=*ctxp;

    STAILQ_FOREACH_SAFE(clnt1, &ctx->clients, entry, clnt2) {
        nr_stun_server_destroy_client(clnt1);
    }

    nr_stun_server_destroy_client(ctx->default_client);

    RFREE(ctx->label);
    RFREE(ctx);

    return(0);
  }

static int nr_stun_server_client_create(nr_stun_server_ctx *ctx, char *client_label, char *user, Data *pass, nr_stun_server_cb cb, void *cb_arg, nr_stun_server_client **clntp)
  {
    nr_stun_server_client *clnt=0;
    int r,_status;

    if(!(clnt=RCALLOC(sizeof(nr_stun_server_client))))
      ABORT(R_NO_MEMORY);

    if(!(clnt->label=r_strdup(client_label)))
      ABORT(R_NO_MEMORY);

    if(!(clnt->username=r_strdup(user)))
      ABORT(R_NO_MEMORY);

    if(r=r_data_copy(&clnt->password,pass))
      ABORT(r);

    r_log(NR_LOG_STUN,LOG_DEBUG,"STUN-SERVER(%s)/CLIENT(%s): Adding client for %s",ctx->label, client_label, user);
    clnt->stun_server_cb=cb;
    clnt->cb_arg=cb_arg;

    *clntp = clnt;
    _status=0;
 abort:
    if(_status){
      nr_stun_server_destroy_client(clnt);
    }
    return(_status);
  }

int nr_stun_server_add_client(nr_stun_server_ctx *ctx, char *client_label, char *user, Data *pass, nr_stun_server_cb cb, void *cb_arg)
  {
   int r,_status;
   nr_stun_server_client *clnt;

    if (r=nr_stun_server_client_create(ctx, client_label, user, pass, cb, cb_arg, &clnt))
      ABORT(r);

    STAILQ_INSERT_TAIL(&ctx->clients,clnt,entry);

    _status=0;
  abort:
    return(_status);
  }

int nr_stun_server_add_default_client(nr_stun_server_ctx *ctx, char *ufrag, Data *pass, nr_stun_server_cb cb, void *cb_arg)
  {
    int r,_status;
    nr_stun_server_client *clnt;

    assert(!ctx->default_client);
    if (ctx->default_client)
      ABORT(R_INTERNAL);

    if (r=nr_stun_server_client_create(ctx, "default_client", ufrag, pass, cb, cb_arg, &clnt))
      ABORT(r);

    ctx->default_client = clnt;

    _status=0;
  abort:
    return(_status);
  }

int nr_stun_server_remove_client(nr_stun_server_ctx *ctx, void *cb_arg)
  {
    nr_stun_server_client *clnt1,*clnt2;
    int found = 0;

    STAILQ_FOREACH_SAFE(clnt1, &ctx->clients, entry, clnt2) {
      if(clnt1->cb_arg == cb_arg) {
        STAILQ_REMOVE(&ctx->clients, clnt1, nr_stun_server_client_, entry);
        nr_stun_server_destroy_client(clnt1);
        found++;
      }
    }

    if (!found)
      ERETURN(R_NOT_FOUND);

    return 0;
  }

static int nr_stun_server_get_password(void *arg, nr_stun_message *msg, Data **password)
  {
    int _status;
    nr_stun_server_ctx *ctx = (nr_stun_server_ctx*)arg;
    nr_stun_server_client *clnt = 0;
    nr_stun_message_attribute *username_attribute;

    if ((nr_stun_get_message_client(ctx, msg, &clnt))) {
        if (! nr_stun_message_has_attribute(msg, NR_STUN_ATTR_USERNAME, &username_attribute)) {
           r_log(NR_LOG_STUN,LOG_WARNING,"STUN-SERVER(%s): Missing Username",ctx->label);
           ABORT(R_NOT_FOUND);
        }

        /* Although this is an exceptional condition, we'll already have seen a
         * NOTICE-level log message about the unknown user, so additional log
         * messages at any level higher than DEBUG are unnecessary. */

        r_log(NR_LOG_STUN,LOG_DEBUG,"STUN-SERVER(%s): Unable to find password for unknown user: %s",ctx->label,username_attribute->u.username);
        ABORT(R_NOT_FOUND);
    }

    *password = &clnt->password;

    _status=0;
  abort:
    return(_status);
  }

int nr_stun_server_process_request_auth_checks(nr_stun_server_ctx *ctx, nr_stun_message *req, int auth_rule, nr_stun_message *res)
  {
    int r,_status;

    if (nr_stun_message_has_attribute(req, NR_STUN_ATTR_MESSAGE_INTEGRITY, 0)
     || !(auth_rule & NR_STUN_AUTH_RULE_OPTIONAL)) {
        /* favor long term credentials over short term, if both are supported */

        if (auth_rule & NR_STUN_AUTH_RULE_LONG_TERM) {
            if ((r=nr_stun_receive_request_long_term_auth(req, ctx, res)))
                ABORT(r);
        }
        else if (auth_rule & NR_STUN_AUTH_RULE_SHORT_TERM) {
            if ((r=nr_stun_receive_request_or_indication_short_term_auth(req, res)))
                ABORT(r);
        }
    }

    _status=0;
  abort:
    return(_status);
  }

int nr_stun_server_process_request(nr_stun_server_ctx *ctx, nr_socket *sock, char *msg, int len, nr_transport_addr *peer_addr, int auth_rule)
  {
    int r,_status;
    char string[256];
    nr_stun_message *req = 0;
    nr_stun_message *res = 0;
    nr_stun_server_client *clnt = 0;
    nr_stun_server_request info;
    int error;
    int dont_free = 0;
    nr_transport_addr my_addr;

    if ((r=nr_socket_getaddr(sock, &my_addr))) {
      ABORT(r);
    }

    r_log(NR_LOG_STUN,LOG_DEBUG,"STUN-SERVER(%s): Received(my_addr=%s,peer_addr=%s)",ctx->label,my_addr.as_string,peer_addr->as_string);

    snprintf(string, sizeof(string)-1, "STUN-SERVER(%s): Received ", ctx->label);
    r_dump(NR_LOG_STUN, LOG_DEBUG, string, (char*)msg, len);

    memset(&info,0,sizeof(info));

    if ((r=nr_stun_message_create2(&req, (UCHAR*)msg, len)))
        ABORT(r);

    if ((r=nr_stun_message_create(&res)))
        ABORT(r);

    if ((r=nr_stun_decode_message(req, nr_stun_server_get_password, ctx))) {
        /* RFC5389 S 7.3 says "If any errors are detected, the message is
         * silently discarded."  */
#ifndef USE_STUN_PEDANTIC
        /* ... but that seems like a bad idea, at least return a 400 so
         * that the server isn't a black hole to the client */
        nr_stun_form_error_response(req, res, 400, "Bad Request - Failed to decode request");
        ABORT(R_ALREADY);
#endif /* USE_STUN_PEDANTIC */
        ABORT(R_REJECTED);
    }

    if ((r=nr_stun_receive_message(0, req))) {
        /* RFC5389 S 7.3 says "If any errors are detected, the message is
         * silently discarded."  */
#ifndef USE_STUN_PEDANTIC
        /* ... but that seems like a bad idea, at least return a 400 so
         * that the server isn't a black hole to the client */
        nr_stun_form_error_response(req, res, 400, "Bad Request - Section 7.3 check failed");
        ABORT(R_ALREADY);
#endif /* USE_STUN_PEDANTIC */
        ABORT(R_REJECTED);
    }

    if (NR_STUN_GET_TYPE_CLASS(req->header.type) != NR_CLASS_REQUEST
     && NR_STUN_GET_TYPE_CLASS(req->header.type) != NR_CLASS_INDICATION) {
         r_log(NR_LOG_STUN,LOG_WARNING,"STUN-SERVER(%s): Illegal message type: %04x",ctx->label,req->header.type);
        /* RFC5389 S 7.3 says "If any errors are detected, the message is
         * silently discarded."  */
#ifndef USE_STUN_PEDANTIC
        /* ... but that seems like a bad idea, at least return a 400 so
         * that the server isn't a black hole to the client */
        nr_stun_form_error_response(req, res, 400, "Bad Request - Unsupported message type");
        ABORT(R_ALREADY);
#endif /* USE_STUN_PEDANTIC */
        ABORT(R_REJECTED);
    }

    /* "The STUN agent then does any checks that are required by a
     * authentication mechanism that the usage has specified" */
    if ((r=nr_stun_server_process_request_auth_checks(ctx, req, auth_rule, res)))
        ABORT(r);

    if (NR_STUN_GET_TYPE_CLASS(req->header.type) == NR_CLASS_INDICATION) {
        if ((r=nr_stun_process_indication(req)))
            ABORT(r);
    }
    else {
        if ((r=nr_stun_process_request(req, res)))
            ABORT(r);
    }

    assert(res->header.type == 0);

    clnt = 0;
    if (NR_STUN_GET_TYPE_CLASS(req->header.type) == NR_CLASS_REQUEST) {
        if ((nr_stun_get_message_client(ctx, req, &clnt))) {
            if ((r=nr_stun_form_success_response(req, peer_addr, 0, res)))
                ABORT(r);
        }
        else {
            if ((r=nr_stun_form_success_response(req, peer_addr, &clnt->password, res)))
                ABORT(r);
        }
    }

    if(clnt && clnt->stun_server_cb){
        r_log(NR_LOG_STUN,LOG_DEBUG,"Entering STUN server callback");

        /* Set up the info */
        if(r=nr_transport_addr_copy(&info.src_addr,peer_addr))
            ABORT(r);

        info.request = req;
        info.response = res;

        error = 0;
        dont_free = 0;
        if (clnt->stun_server_cb(clnt->cb_arg,ctx,sock,&info,&dont_free,&error)) {
            if (error == 0)
                error = 500;

            nr_stun_form_error_response(req, res, error, "ICE Failure");
            ABORT(R_ALREADY);
        }
    }

    _status=0;
  abort:
    if (!res)
        goto skip_response;

    if (NR_STUN_GET_TYPE_CLASS(req->header.type) == NR_CLASS_INDICATION)
        goto skip_response;

    /* Now respond */

    if (_status != 0 && ! nr_stun_message_has_attribute(res, NR_STUN_ATTR_ERROR_CODE, 0))
         nr_stun_form_error_response(req, res, 500, "Failed to specify error");

    if ((r=nr_stun_server_send_response(ctx, sock, peer_addr, res, clnt))) {
        r_log(NR_LOG_STUN,LOG_ERR,"STUN-SERVER(label=%s): Failed sending response (my_addr=%s,peer_addr=%s)",ctx->label,my_addr.as_string,peer_addr->as_string);
        _status = R_FAILED;
    }

#if 0
    /* EKR: suppressed these checks because if you have an error when
       you are sending an error, things go wonky */
#ifdef SANITY_CHECKS
    if (_status == R_ALREADY) {
        assert(NR_STUN_GET_TYPE_CLASS(res->header.type) == NR_CLASS_ERROR_RESPONSE);
        assert(nr_stun_message_has_attribute(res, NR_STUN_ATTR_ERROR_CODE, 0));
    }
    else {
        assert(NR_STUN_GET_TYPE_CLASS(res->header.type) == NR_CLASS_RESPONSE);
        assert(!nr_stun_message_has_attribute(res, NR_STUN_ATTR_ERROR_CODE, 0));
    }
#endif /* SANITY_CHECKS */
#endif

    if (0) {
  skip_response:
        _status = 0;
    }

    if (!dont_free) {
      nr_stun_message_destroy(&res);
      nr_stun_message_destroy(&req);
    }

    return(_status);
  }

static int nr_stun_server_send_response(nr_stun_server_ctx *ctx, nr_socket *sock, nr_transport_addr *peer_addr, nr_stun_message *res, nr_stun_server_client *clnt)
  {
    int r,_status;
    char string[256];
    nr_transport_addr my_addr;

    if ((r=nr_socket_getaddr(sock, &my_addr))) {
      ABORT(r);
    }

    r_log(NR_LOG_STUN,LOG_DEBUG,"STUN-SERVER(label=%s): Sending(my_addr=%s,peer_addr=%s)",ctx->label,my_addr.as_string,peer_addr->as_string);

    if ((r=nr_stun_encode_message(res))) {
        /* should never happen */
        r_log(NR_LOG_STUN,LOG_ERR,"STUN-SERVER(label=%s): Unable to encode message", ctx->label);
    }
    else {
        snprintf(string, sizeof(string)-1, "STUN(%s): Sending to %s ", ctx->label, peer_addr->as_string);
        r_dump(NR_LOG_STUN, LOG_DEBUG, string, (char*)res->buffer, res->length);

        if(r=nr_socket_sendto(sock,res->buffer,res->length,0,peer_addr))
          ABORT(r);
    }

    _status=0;
  abort:
    return(_status);
  }

static int nr_stun_server_destroy_client(nr_stun_server_client *clnt)
  {
    if (!clnt)
      return 0;

    RFREE(clnt->label);
    RFREE(clnt->username);
    r_data_zfree(&clnt->password);

    RFREE(clnt);
    return(0);
  }

int nr_stun_get_message_client(nr_stun_server_ctx *ctx, nr_stun_message *req, nr_stun_server_client **out)
  {
    int _status;
    nr_stun_message_attribute *attr;
    nr_stun_server_client *clnt=0;

    if (! nr_stun_message_has_attribute(req, NR_STUN_ATTR_USERNAME, &attr)) {
       r_log(NR_LOG_STUN,LOG_WARNING,"STUN-SERVER(%s): Missing Username",ctx->label);
       ABORT(R_NOT_FOUND);
    }

    STAILQ_FOREACH(clnt, &ctx->clients, entry) {
        if (!strncmp(clnt->username, attr->u.username,
                     sizeof(attr->u.username)))
            break;
    }

    if (!clnt && ctx->default_client) {
      /* If we can't find a specific client see if this matches the default,
         which means that the username starts with our ufrag.
       */
      char *colon = strchr(attr->u.username, ':');
      if (colon && !strncmp(ctx->default_client->username,
                            attr->u.username,
                            colon - attr->u.username)) {
        clnt = ctx->default_client;
        r_log(NR_LOG_STUN,LOG_NOTICE,"STUN-SERVER(%s): Falling back to default client, username=: %s",ctx->label,attr->u.username);
      }
    }

    if (!clnt) {
        r_log(NR_LOG_STUN,LOG_WARNING,"STUN-SERVER(%s): Request from unknown user: %s",ctx->label,attr->u.username);
        ABORT(R_NOT_FOUND);
    }

    *out = clnt;

    _status=0;
  abort:
    return(_status);
  }