| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
|
| 58 |
|
| 59 |
PJ_DEF(void) pj_turn_auth_dinit(void) |
| 60 |
{ |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 85 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 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 |
|