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
|
/*
** Copyright 1998 - 2008 Double Precision, Inc.
** See COPYING for distribution information.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "imaptoken.h"
#include "imapwrite.h"
#include "courierauth.h"
#include "courierauthsasl.h"
#include "courierauthdebug.h"
extern int main_argc;
extern char **main_argv;
extern int login_callback(struct authinfo *ainfo, void *dummy);
extern const char *imap_externalauth();
static char *send_auth_reply(const char *q, void *dummy)
{
struct imaptoken *tok;
char *p;
#if SMAP
const char *cp=getenv("PROTOCOL");
if (cp && strcmp(cp, "SMAP1") == 0)
writes("> ");
else
#endif
{
writes("+ ");
}
writes(q);
writes("\r\n");
writeflush();
read_timeout(SOCKET_TIMEOUT);
tok=nexttoken_nouc();
switch (tok->tokentype) {
case IT_ATOM:
case IT_NUMBER:
p=my_strdup(tok->tokenbuf);
break;
case IT_EOL:
p=my_strdup("");
break;
default:
return (0);
}
if (!p)
{
perror("malloc");
return (0);
}
if (nexttoken()->tokentype != IT_EOL)
{
free(p);
fprintf(stderr, "Invalid SASL response\n");
return (0);
}
read_eol();
return (p);
}
int authenticate(const char *tag, char *methodbuf, int methodbuflen)
{
struct imaptoken *tok=nexttoken();
char *authmethod;
char *initreply=0;
char *authtype, *authdata;
char authservice[40];
char *p ;
int rc;
switch (tok->tokentype) {
case IT_ATOM:
case IT_QUOTED_STRING:
break;
default:
return (0);
}
authmethod=my_strdup(tok->tokenbuf);
if (methodbuf)
snprintf(methodbuf, methodbuflen, "%s", authmethod);
tok=nexttoken_nouc();
if (tok->tokentype != IT_EOL)
{
switch (tok->tokentype) {
case IT_ATOM:
case IT_NUMBER:
case IT_QUOTED_STRING:
break;
default:
return (0);
}
initreply=my_strdup(tok->tokenbuf);
if (strcmp(initreply, "=") == 0)
*initreply=0;
tok=nexttoken_nouc();
}
if (tok->tokentype != IT_EOL) return (0);
read_eol();
if ((rc = auth_sasl_ex(authmethod, initreply, imap_externalauth(),
&send_auth_reply, NULL,
&authtype, &authdata)) != 0)
{
free(authmethod);
if (initreply)
free(initreply);
return (rc);
}
free(authmethod);
if (initreply)
free(initreply);
strcat(strcpy(authservice, "AUTHSERVICE"),
getenv("TCPLOCALPORT"));
p=getenv(authservice);
if (!p || !*p)
p="imap";
rc=auth_generic_meta(NULL, p, authtype, authdata,
login_callback, (void *)tag);
free(authtype);
free(authdata);
return (rc);
}
|