smtp_mech.c
4.06 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/cctype.h>
#include <mailutils/cstr.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/util.h>
#include <mailutils/smtp.h>
#include <mailutils/sys/smtp.h>
static int
_mech_comp (const void *item, const void *data)
{
return mu_c_strcasecmp ((const char*)item, (const char*)data);
}
int
mu_smtp_add_auth_mech (mu_smtp_t smtp, const char *mech)
{
char *p;
if (!smtp)
return EINVAL;
if (!smtp->authmech)
{
int rc = mu_list_create (&smtp->authmech);
if (rc)
return rc;
mu_list_set_destroy_item (smtp->authmech, mu_list_free_item);
mu_list_set_comparator (smtp->authmech, _mech_comp);
}
p = strdup (mech);
if (!p)
return ENOMEM;
mu_strupper (p);
return mu_list_append (smtp->authmech, p);
}
int
mu_smtp_clear_auth_mech (mu_smtp_t smtp)
{
if (!smtp)
return EINVAL;
mu_list_clear (smtp->authmech);
return 0;
}
static int
_mech_append (void *item, void *data)
{
mu_smtp_t smtp = data;
const char *mech = item;
return mu_smtp_add_auth_mech (smtp, mech);
}
int
mu_smtp_add_auth_mech_list (mu_smtp_t smtp, mu_list_t list)
{
if (!smtp)
return EINVAL;
return mu_list_foreach (list, _mech_append, smtp);
}
/* Set a list of implemented authentication mechanisms */
int
_mu_smtp_mech_impl (mu_smtp_t smtp, mu_list_t list)
{
mu_list_destroy (&smtp->authimpl);
smtp->authimpl = list;
mu_list_set_comparator (smtp->authimpl, _mech_comp);
return 0;
}
static int
_mech_copy (void *item, void *data)
{
const char *mech = item;
mu_list_t list = data;
return mu_list_append (list, (void *)mech);
}
/* Select authentication mechanism to use */
int
mu_smtp_mech_select (mu_smtp_t smtp, const char **pmech)
{
int rc;
const char *authstr;
mu_list_t alist;
mu_iterator_t itr;
if (!smtp)
return EINVAL;
/* Obtain the list of mechanisms supported by the server */
rc = mu_smtp_capa_test (smtp, "AUTH", &authstr);
if (rc)
return rc;
/* Create an intersection of implemented and allowed mechanisms */
if (!smtp->authimpl)
return MU_ERR_NOENT; /* obvious case */
if (!smtp->authmech)
{
rc = mu_list_create (&alist);
if (rc == 0)
rc = mu_list_foreach (smtp->authimpl, _mech_copy, alist);
}
else
{
rc = mu_list_intersect_dup (&alist, smtp->authmech, smtp->authimpl,
NULL, NULL);
}
if (rc)
return rc;
/* Select first element from the intersection that occurs also in the
list of methods supported by the server */
rc = mu_list_get_iterator (alist, &itr);
if (rc == 0)
{
const char *p;
int res = 1;
rc = MU_ERR_NOENT;
authstr += 5; /* "AUTH */
for (mu_iterator_first (itr); rc && !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *mech;
mu_iterator_current (itr, (void**) &mech);
for (p = authstr; *p; )
{
char *end;
p = mu_str_stripws ((char *)p);
end = mu_str_skip_class_comp (p, MU_CTYPE_SPACE);
res = mu_c_strncasecmp (mech, p, end - p);
if (res == 0)
{
*pmech = mech;
rc = 0;
break;
}
p = end;
}
}
}
/* cleanup and return */
mu_list_destroy (&alist);
return rc;
}