capa.c
4.19 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2001, 2003, 2007, 2009-2012, 2014-2017 Free
Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include "pop3d.h"
/*
The CAPA Command
The POP3 CAPA command returns a list of capabilities supported by the
POP3 server. It is available in both the AUTHORIZATION and
TRANSACTION states.
Capabilities available in the AUTHORIZATION state MUST be announced
in both states. */
static int
print_capa (void *item, void *data)
{
struct pop3d_capa *cp = item;
struct pop3d_session *session = data;
if (cp->type == capa_func)
{
cp->value.func (cp->name, session);
}
else
{
pop3d_outf ("%s", cp->name);
if (cp->value.string)
pop3d_outf ("%s", cp->value.string);
pop3d_outf ("\n");
}
return 0;
}
int
pop3d_capa (char *arg, struct pop3d_session *sess)
{
if (strlen (arg) != 0)
return ERR_BAD_ARGS;
pop3d_outf ("+OK Capability list follows\n");
mu_list_foreach (sess->capa, print_capa, sess);
pop3d_outf (".\n");
return OK;
}
static void
pop3d_append_capa_string (struct pop3d_session *sess, const char *name,
const char *value)
{
struct pop3d_capa *cp;
cp = mu_alloc (sizeof (*cp));
cp->type = capa_string;
cp->name = name;
cp->value.string = value ? mu_strdup (value) : NULL;
if (mu_list_append (sess->capa, cp))
mu_alloc_die ();
}
static void
pop3d_append_capa_func (struct pop3d_session *sess, const char *name,
void (*func) (const char *, struct pop3d_session *))
{
struct pop3d_capa *cp;
if (!func)
return;
cp = mu_alloc (sizeof (*cp));
cp->type = capa_func;
cp->name = name;
cp->value.func = func;
if (mu_list_append (sess->capa, cp))
mu_alloc_die ();
}
static void
capa_free (void *p)
{
struct pop3d_capa *cp = p;
if (cp->type == capa_string && cp->value.string)
free (cp->value.string);
free (cp);
}
static void
capa_implementation (const char *name, struct pop3d_session *session)
{
if (state == TRANSACTION) /* let's not advertise to just anyone */
pop3d_outf ("%s %s\n", name, PACKAGE_STRING);
}
static void
capa_stls (const char *name, struct pop3d_session *session)
{
switch (session->tls_mode)
{
case tls_ondemand:
case tls_required:
pop3d_outf ("%s\n", name);
break;
default:
break;
}
}
static void
capa_user (const char *name, struct pop3d_session *session)
{
if (state == INITIAL)
pop3d_outf ("XTLSREQUIRED\n");
else
pop3d_outf ("USER\n");
}
void
pop3d_session_init (struct pop3d_session *session)
{
if (mu_list_create (&session->capa))
mu_alloc_die ();
mu_list_set_destroy_item (session->capa, capa_free);
/* Initialize the capability list */
pop3d_append_capa_string (session, "TOP", NULL);
pop3d_append_capa_string (session, "UIDL", NULL);
pop3d_append_capa_string (session, "RESP-CODES", NULL);
pop3d_append_capa_string (session, "PIPELINING", NULL);
pop3d_append_capa_string (session, "AUTH-RESP-CODE", NULL);
if (pop3d_xlines)
pop3d_append_capa_string (session, "XLINES", NULL);
pop3d_append_capa_func (session, "LOGIN-DELAY", login_delay_capa);
/* This can be implemented by setting an header field on the message. */
pop3d_append_capa_string (session, "EXPIRE",
(expire == EXPIRE_NEVER) ?
"NEVER" : mu_umaxtostr (0, expire));
pop3d_append_capa_func (session, NULL, capa_user);
pop3d_append_capa_func (session, "STLS", capa_stls);
pop3d_append_capa_func (session, "IMPLEMENTATION", capa_implementation);
}
void
pop3d_session_free (struct pop3d_session *session)
{
mu_list_destroy (&session->capa);
}