summaryrefslogtreecommitdiff
path: root/src/libpttls/sasl/sasl_mechanism.c
blob: 4e54de31449e090130e62c8857c28596cca32f78 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright (C) 2013 Martin Willi
 * Copyright (C) 2013 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "sasl_mechanism.h"

#include "sasl_plain/sasl_plain.h"

/**
 * Available SASL mechanisms.
 */
static struct {
	char *name;
	bool server;
	sasl_mechanism_constructor_t create;
} mechs[] = {
	{ "PLAIN",		TRUE,	(sasl_mechanism_constructor_t)sasl_plain_create	},
	{ "PLAIN",		FALSE,	(sasl_mechanism_constructor_t)sasl_plain_create	},
};

/**
 * See header.
 */
sasl_mechanism_t *sasl_mechanism_create(char *name, identification_t *client)
{
	int i;

	for (i = 0; i < countof(mechs); i++)
	{
		if (streq(mechs[i].name, name) && mechs[i].server == (client == NULL))
		{
			return mechs[i].create(name, client);
		}
	}
	return NULL;
}

/**
 * SASL mechanism enumerator
 */
typedef struct {
	/** implements enumerator_t */
	enumerator_t public;
	/** looking for client or server? */
	bool server;
	/** position in mechs[] */
	int i;
} mech_enumerator_t;

METHOD(enumerator_t, mech_enumerate, bool,
	mech_enumerator_t *this, va_list args)
{
	char **name;

	VA_ARGS_VGET(args, name);
	while (this->i < countof(mechs))
	{
		if (mechs[this->i].server == this->server)
		{
			*name = mechs[this->i].name;
			this->i++;
			return TRUE;
		}
		this->i++;
	}
	return FALSE;
}

/**
 * See header.
 */
enumerator_t* sasl_mechanism_create_enumerator(bool server)
{
	mech_enumerator_t *enumerator;

	INIT(enumerator,
		.public = {
			.enumerate = enumerator_enumerate_default,
			.venumerate = _mech_enumerate,
			.destroy = (void*)free,
		},
		.server = server,
	);
	return &enumerator->public;
}