File: crypto.c

package info (click to toggle)
atheme-services 7.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,992 kB
  • sloc: ansic: 93,230; sh: 7,440; php: 5,032; perl: 3,327; makefile: 1,261; sed: 16; ruby: 15; python: 3
file content (151 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (2)
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
/*
 * atheme-services: A collection of minimalist IRC services
 * crypto.c: Cryptographic module support.
 *
 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 "atheme.h"

static mowgli_list_t crypt_impl_list = { NULL, NULL, 0 };
bool crypto_module_loaded = false;

static const char *generic_crypt_string(const char *str, const char *salt)
{
	return str;
}

static const char *generic_gen_salt(void)
{
	static char buf[BUFSIZE];
	char *ht = random_string(6);

	mowgli_strlcpy(buf, "$1$", BUFSIZE);
	mowgli_strlcat(buf, ht, BUFSIZE);
	mowgli_strlcat(buf, "$", BUFSIZE);

	free(ht);

	return buf;
}

static bool generic_needs_param_upgrade(const char *user_pass_string)
{
	return 0;
}

static const crypt_impl_t fallback_crypt_impl = {
	.id = "plaintext",
	.crypt = &generic_crypt_string,
	.salt = &generic_gen_salt,
	.needs_param_upgrade = &generic_needs_param_upgrade,
};

const crypt_impl_t *crypt_get_default_provider(void)
{
	crypt_impl_t *ci;

	if (!MOWGLI_LIST_LENGTH(&crypt_impl_list))
		return &fallback_crypt_impl;

	/* top of stack should handle string crypting, should be populated by now */
	return_val_if_fail(crypt_impl_list.head != NULL, &fallback_crypt_impl);
	ci = crypt_impl_list.head->data;

	/* ensure the provider is populated */
	return_val_if_fail(ci->crypt != NULL, &fallback_crypt_impl);
	return_val_if_fail(ci->salt != NULL, &fallback_crypt_impl);

	return ci;
}

/*
 * crypt_string is just like crypt(3) under UNIX
 * systems. Modules provide this function, otherwise
 * it returns the string unencrypted.
 */
const char *crypt_string(const char *key, const char *salt)
{
	const crypt_impl_t *ci = crypt_get_default_provider();

	return ci->crypt(key, salt);
}

const char *gen_salt(void)
{
	const crypt_impl_t *ci = crypt_get_default_provider();

	return ci->salt();
}

void crypt_register(crypt_impl_t *impl)
{
	return_if_fail(impl != NULL);

	if (impl->crypt == NULL)
		impl->crypt = &generic_crypt_string;
	if (impl->salt == NULL)
		impl->salt = &generic_gen_salt;

	mowgli_node_add(impl, &impl->node, &crypt_impl_list);

	crypto_module_loaded = MOWGLI_LIST_LENGTH(&crypt_impl_list) > 0 ? true : false;
}

void crypt_unregister(crypt_impl_t *impl)
{
	return_if_fail(impl != NULL);

	mowgli_node_delete(&impl->node, &crypt_impl_list);

	crypto_module_loaded = MOWGLI_LIST_LENGTH(&crypt_impl_list) > 0 ? true : false;
}

/*
 * crypt_verify_password is a frontend to crypt_string().
 */
const crypt_impl_t *crypt_verify_password(const char *uinput, const char *pass)
{
	mowgli_node_t *n;
	const char *cstr;

	MOWGLI_ITER_FOREACH(n, crypt_impl_list.head)
	{
		crypt_impl_t *ci;

		ci = n->data;
		cstr = ci->crypt(uinput, pass);

		if (cstr != NULL && !strcmp(cstr, pass))
			return ci;
	}

	cstr = fallback_crypt_impl.crypt(uinput, pass);

	if (!strcmp(cstr, pass))
		return &fallback_crypt_impl;

	return NULL;
}

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
 * vim:ts=8
 * vim:sw=8
 * vim:noexpandtab
 */