summaryrefslogtreecommitdiff
path: root/test/sip/user.c
blob: 99fb47dc0d4e933ee626fa4a003aa131c2348278 (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
/**
 * @file sip/user.c Mock SIP server -- user handling
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <re.h>
#include "sipsrv.h"


static void destructor(void *arg)
{
	struct user *usr = arg;

	hash_unlink(&usr->he);
	mem_deref(usr->name);
}


int user_add(struct hash *ht, const char *username, const char *password,
	     const char *realm)
{
	struct user *usr;
	int err;

	usr = mem_zalloc(sizeof(*usr), destructor);
	if (!usr) {
		err = ENOMEM;
		goto out;
	}

	err = str_dup(&usr->name, username);
	if (err) {
		goto out;
	}

	err = md5_printf(usr->ha1, "%s:%s:%s", username, realm, password);
	if (err) {
		goto out;
	}

	hash_append(ht, hash_joaat_str(username), &usr->he, usr);

 out:
	if (err) {
		mem_deref(usr);
	}

	return err;
}


struct user *user_find(struct hash *ht, const struct pl *name)
{
	struct list *lst;
	struct le *le;

	if (!ht || !name)
		return NULL;

	lst = hash_list(ht, hash_joaat((uint8_t *)name->p, name->l));

	for (le=list_head(lst); le; le=le->next) {

		struct user *usr = le->data;

		if (pl_strcmp(name, usr->name))
			continue;

		return usr;
	}

	return NULL;
}