blob: d2527dfa66d0d7cc17f185644e6945f512c92cfe [file] [log] [blame]
Benny Prijono708725a2008-03-09 12:55:00 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
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; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "auth.h"
20#include <pjlib.h>
21
22
23#define MAX_REALM 80
24#define MAX_USERNAME 32
25#define MAX_PASSWORD 32
26#define MAX_NONCE 32
27
28static char g_realm[MAX_REALM];
29
30static struct cred_t
31{
32 char username[MAX_USERNAME];
33 char passwd[MAX_PASSWORD];
34} g_cred[] =
35{
Benny Prijonoe7226852008-04-13 21:48:44 +000036 { "100", "100" },
37 { "700", "700" },
38 { "701", "701" },
39 { "702", "702" }
Benny Prijono708725a2008-03-09 12:55:00 +000040};
41
42#define THE_NONCE "pjnath"
43
44
45/*
46 * Initialize TURN authentication subsystem.
47 */
48PJ_DEF(pj_status_t) pj_turn_auth_init(const char *realm)
49{
50 PJ_ASSERT_RETURN(pj_ansi_strlen(realm) < MAX_REALM, PJ_ENAMETOOLONG);
51 pj_ansi_strcpy(g_realm, realm);
52 return PJ_SUCCESS;
53}
54
55/*
56 * Shutdown TURN authentication subsystem.
57 */
58PJ_DEF(void) pj_turn_auth_dinit(void)
59{
60 /* Nothing to do */
61}
62
63
Benny Prijono708725a2008-03-09 12:55:00 +000064/*
65 * This function is called by pj_stun_verify_credential() when
66 * server needs to challenge the request with 401 response.
67 */
68PJ_DEF(pj_status_t) pj_turn_get_auth(void *user_data,
69 pj_pool_t *pool,
70 pj_str_t *realm,
71 pj_str_t *nonce)
72{
73 PJ_UNUSED_ARG(user_data);
74 PJ_UNUSED_ARG(pool);
75
76 *realm = pj_str(g_realm);
77 *nonce = pj_str(THE_NONCE);
78
79 return PJ_SUCCESS;
80}
81
82/*
83 * This function is called to get the password for the specified username.
84 * This function is also used to check whether the username is valid.
85 */
86PJ_DEF(pj_status_t) pj_turn_get_password(const pj_stun_msg *msg,
87 void *user_data,
88 const pj_str_t *realm,
89 const pj_str_t *username,
90 pj_pool_t *pool,
Benny Prijono68854002008-03-20 19:21:27 +000091 pj_stun_passwd_type *data_type,
Benny Prijono708725a2008-03-09 12:55:00 +000092 pj_str_t *data)
93{
94 unsigned i;
95
96 PJ_UNUSED_ARG(msg);
97 PJ_UNUSED_ARG(user_data);
98 PJ_UNUSED_ARG(pool);
99
100 if (pj_stricmp2(realm, g_realm))
Benny Prijono68854002008-03-20 19:21:27 +0000101 return PJ_EINVAL;
Benny Prijono708725a2008-03-09 12:55:00 +0000102
103 for (i=0; i<PJ_ARRAY_SIZE(g_cred); ++i) {
104 if (pj_stricmp2(username, g_cred[i].username) == 0) {
Benny Prijono24a21852008-04-14 04:04:30 +0000105 *data_type = PJ_STUN_PASSWD_PLAIN;
Benny Prijono708725a2008-03-09 12:55:00 +0000106 *data = pj_str(g_cred[i].passwd);
107 return PJ_SUCCESS;
108 }
109 }
110
111 return PJ_ENOTFOUND;
112}
113
114/*
115 * This function will be called to verify that the NONCE given
116 * in the message can be accepted. If this callback returns
117 * PJ_FALSE, 438 (Stale Nonce) response will be created.
118 */
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000119PJ_DEF(pj_bool_t) pj_turn_verify_nonce(const pj_stun_msg *msg,
120 void *user_data,
121 const pj_str_t *realm,
122 const pj_str_t *username,
123 const pj_str_t *nonce)
Benny Prijono708725a2008-03-09 12:55:00 +0000124{
125 PJ_UNUSED_ARG(msg);
126 PJ_UNUSED_ARG(user_data);
127 PJ_UNUSED_ARG(realm);
128 PJ_UNUSED_ARG(username);
129
130 if (pj_stricmp2(nonce, THE_NONCE))
131 return PJ_FALSE;
132
Benny Prijono9e6d60a2008-03-20 16:32:06 +0000133 return PJ_TRUE;
Benny Prijono708725a2008-03-09 12:55:00 +0000134}
135