Debian Codegraph @

Xpjproject/pjnath/src/pjturn-srv/auth.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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
28 static char g_realm[MAX_REALM];
29
30 static struct cred_t
31 {
32 char username[MAX_USERNAME];
33 char passwd[MAX_PASSWORD];
34 } g_cred[] =
35 {
36 { "100", "100" },
37 { "700", "700" },
38 { "701", "701" }
39 };
40
41 #define THIS_FILE "auth.c"
42 #define THE_NONCE "pjnath"
43 #define LOG(expr) PJ_LOG(3,expr)
44
45
46 /*
47 * Initialize TURN authentication subsystem.
48 */
49 PJ_DEF(pj_status_t) pj_turn_auth_init(const char *realm)
50 {
51 PJ_ASSERT_RETURN(pj_ansi_strlen(realm) < MAX_REALM, PJ_ENAMETOOLONG);
52 pj_ansi_strxcpy(g_realm, realm, sizeof(g_realm));
53 return PJ_SUCCESS;
54 }
55
56 /*
57 * Shutdown TURN authentication subsystem.
58 */
59 PJ_DEF(void) pj_turn_auth_dinit(void)
60 {
61 /* Nothing to do */
62 }
63
64
65 /*
66 * This function is called by pj_stun_verify_credential() when
67 * server needs to challenge the request with 401 response.
68 */
69 PJ_DEF(pj_status_t) pj_turn_get_auth(void *user_data,
70 pj_pool_t *pool,
71 pj_str_t *realm,
72 pj_str_t *nonce)
73 {
74 PJ_UNUSED_ARG(user_data);
75 PJ_UNUSED_ARG(pool);
76
77 *realm = pj_str(g_realm);
78 *nonce = pj_str(THE_NONCE);
79
80 return PJ_SUCCESS;
81 }
82
83 /*
84 * This function is called to get the password for the specified username.
85 * This function is also used to check whether the username is valid.
86 */
87 PJ_DEF(pj_status_t) pj_turn_get_password(const pj_stun_msg *msg,
88 void *user_data,
89 const pj_str_t *realm,
90 const pj_str_t *username,
91 pj_pool_t *pool,
92 pj_stun_passwd_type *data_type,
93 pj_str_t *data)
94 {
95 unsigned i;
96
97 PJ_UNUSED_ARG(msg);
98 PJ_UNUSED_ARG(user_data);
99 PJ_UNUSED_ARG(pool);
100
101 if (pj_stricmp2(realm, g_realm)) {
102 LOG((THIS_FILE, "auth error: invalid realm '%.*s'",
103 (int)realm->slen, realm->ptr));
104 return PJ_EINVAL;
105 }
106
107 for (i=0; i<PJ_ARRAY_SIZE(g_cred); ++i) {
108 if (pj_stricmp2(username, g_cred[i].username) == 0) {
109 *data_type = PJ_STUN_PASSWD_PLAIN;
110 *data = pj_str(g_cred[i].passwd);
111 return PJ_SUCCESS;
112 }
113 }
114
115 LOG((THIS_FILE, "auth error: user '%.*s' not found",
116 (int)username->slen, username->ptr));
117 return PJ_ENOTFOUND;
118 }
119
120 /*
121 * This function will be called to verify that the NONCE given
122 * in the message can be accepted. If this callback returns
123 * PJ_FALSE, 438 (Stale Nonce) response will be created.
124 */
125 PJ_DEF(pj_bool_t) pj_turn_verify_nonce(const pj_stun_msg *msg,
126 void *user_data,
127 const pj_str_t *realm,
128 const pj_str_t *username,
129 const pj_str_t *nonce)
130 {
131 PJ_UNUSED_ARG(msg);
132 PJ_UNUSED_ARG(user_data);
133 PJ_UNUSED_ARG(realm);
134 PJ_UNUSED_ARG(username);
135
136 if (pj_stricmp2(nonce, THE_NONCE)) {
137 LOG((THIS_FILE, "auth error: invalid nonce '%.*s'",
138 (int)nonce->slen, nonce->ptr));
139 return PJ_FALSE;
140 }
141
142 return PJ_TRUE;
143 }
144