forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutt_account.c
418 lines (375 loc) · 11.3 KB
/
mutt_account.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* @file
* ConnAccount object used by POP and IMAP
*
* @authors
* Copyright (C) 2000-2007 Brendan Cully <brendan@kublai.com>
*
* @copyright
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page mutt_account ConnAccount object used by POP and IMAP
*
* ConnAccount object used by POP and IMAP
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "mutt_account.h"
#include "curs_lib.h"
#include "filter.h"
#include "globals.h"
#include "options.h"
#include "pop/pop.h"
/* These Config Variables are only used in mutt_account.c */
char *C_ImapLogin; ///< Config: (imap) Login name for the IMAP server (defaults to #C_ImapUser)
char *C_ImapOauthRefreshCmd; ///< Config: (imap) External command to generate OAUTH refresh token
char *C_ImapPass; ///< Config: (imap) Password for the IMAP server
char *C_NntpPass; ///< Config: (nntp) Password for the news server
char *C_NntpUser; ///< Config: (nntp) Username for the news server
char *C_PopOauthRefreshCmd; ///< Config: (pop) External command to generate OAUTH refresh token
char *C_PopPass; ///< Config: (pop) Password of the POP server
char *C_PopUser; ///< Config: (pop) Username of the POP server
char *C_SmtpOauthRefreshCmd; ///< Config: (smtp) External command to generate OAUTH refresh token
char *C_SmtpPass; ///< Config: (smtp) Password for the SMTP server
/**
* mutt_account_match - Compare account info (host/port/user)
* @param a1 First ConnAccount
* @param a2 Second ConnAccount
* @retval true Accounts match
*/
bool mutt_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
{
if (a1->type != a2->type)
return false;
if (mutt_str_strcasecmp(a1->host, a2->host) != 0)
return false;
if (a1->port != a2->port)
return false;
if (a1->flags & a2->flags & MUTT_ACCT_USER)
return strcmp(a1->user, a2->user) == 0;
#ifdef USE_NNTP
if (a1->type == MUTT_ACCT_TYPE_NNTP)
return (a1->flags & MUTT_ACCT_USER) && a1->user[0] ? false : true;
#endif
const char *user = NONULL(Username);
#ifdef USE_IMAP
if ((a1->type == MUTT_ACCT_TYPE_IMAP) && C_ImapUser)
user = C_ImapUser;
#endif
#ifdef USE_POP
if ((a1->type == MUTT_ACCT_TYPE_POP) && C_PopUser)
user = C_PopUser;
#endif
#ifdef USE_NNTP
if ((a1->type == MUTT_ACCT_TYPE_NNTP) && C_NntpUser)
user = C_NntpUser;
#endif
if (a1->flags & MUTT_ACCT_USER)
return strcmp(a1->user, user) == 0;
if (a2->flags & MUTT_ACCT_USER)
return strcmp(a2->user, user) == 0;
return true;
}
/**
* mutt_account_fromurl - Fill ConnAccount with information from url
* @param account ConnAccount to fill
* @param url Url to parse
* @retval 0 Success
* @retval -1 Error
*/
int mutt_account_fromurl(struct ConnAccount *account, const struct Url *url)
{
/* must be present */
if (url->host)
mutt_str_strfcpy(account->host, url->host, sizeof(account->host));
else
return -1;
if (url->user)
{
mutt_str_strfcpy(account->user, url->user, sizeof(account->user));
account->flags |= MUTT_ACCT_USER;
}
if (url->pass)
{
mutt_str_strfcpy(account->pass, url->pass, sizeof(account->pass));
account->flags |= MUTT_ACCT_PASS;
}
if (url->port)
{
account->port = url->port;
account->flags |= MUTT_ACCT_PORT;
}
return 0;
}
/**
* mutt_account_tourl - Fill URL with info from account
* @param account Source ConnAccount
* @param url Url to fill
*
* The URL information is a set of pointers into account - don't free or edit
* account until you've finished with url (make a copy of account if you need
* it for a while).
*/
void mutt_account_tourl(struct ConnAccount *account, struct Url *url)
{
url->scheme = U_UNKNOWN;
url->user = NULL;
url->pass = NULL;
url->port = 0;
url->path = NULL;
#ifdef USE_IMAP
if (account->type == MUTT_ACCT_TYPE_IMAP)
{
if (account->flags & MUTT_ACCT_SSL)
url->scheme = U_IMAPS;
else
url->scheme = U_IMAP;
}
#endif
#ifdef USE_POP
if (account->type == MUTT_ACCT_TYPE_POP)
{
if (account->flags & MUTT_ACCT_SSL)
url->scheme = U_POPS;
else
url->scheme = U_POP;
}
#endif
#ifdef USE_SMTP
if (account->type == MUTT_ACCT_TYPE_SMTP)
{
if (account->flags & MUTT_ACCT_SSL)
url->scheme = U_SMTPS;
else
url->scheme = U_SMTP;
}
#endif
#ifdef USE_NNTP
if (account->type == MUTT_ACCT_TYPE_NNTP)
{
if (account->flags & MUTT_ACCT_SSL)
url->scheme = U_NNTPS;
else
url->scheme = U_NNTP;
}
#endif
url->host = account->host;
if (account->flags & MUTT_ACCT_PORT)
url->port = account->port;
if (account->flags & MUTT_ACCT_USER)
url->user = account->user;
if (account->flags & MUTT_ACCT_PASS)
url->pass = account->pass;
}
/**
* mutt_account_getuser - Retrieve username into ConnAccount, if necessary
* @param account ConnAccount to fill
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_account_getuser(struct ConnAccount *account)
{
char prompt[256];
/* already set */
if (account->flags & MUTT_ACCT_USER)
return 0;
#ifdef USE_IMAP
else if ((account->type == MUTT_ACCT_TYPE_IMAP) && C_ImapUser)
mutt_str_strfcpy(account->user, C_ImapUser, sizeof(account->user));
#endif
#ifdef USE_POP
else if ((account->type == MUTT_ACCT_TYPE_POP) && C_PopUser)
mutt_str_strfcpy(account->user, C_PopUser, sizeof(account->user));
#endif
#ifdef USE_NNTP
else if ((account->type == MUTT_ACCT_TYPE_NNTP) && C_NntpUser)
mutt_str_strfcpy(account->user, C_NntpUser, sizeof(account->user));
#endif
else if (OptNoCurses)
return -1;
/* prompt (defaults to unix username), copy into account->user */
else
{
/* L10N: Example: Username at myhost.com */
snprintf(prompt, sizeof(prompt), _("Username at %s: "), account->host);
mutt_str_strfcpy(account->user, Username, sizeof(account->user));
if (mutt_get_field_unbuffered(prompt, account->user, sizeof(account->user), 0))
return -1;
}
account->flags |= MUTT_ACCT_USER;
return 0;
}
/**
* mutt_account_getlogin - Retrieve login info into ConnAccount, if necessary
* @param account ConnAccount to fill
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_account_getlogin(struct ConnAccount *account)
{
/* already set */
if (account->flags & MUTT_ACCT_LOGIN)
return 0;
#ifdef USE_IMAP
else if (account->type == MUTT_ACCT_TYPE_IMAP)
{
if (C_ImapLogin)
{
mutt_str_strfcpy(account->login, C_ImapLogin, sizeof(account->login));
account->flags |= MUTT_ACCT_LOGIN;
}
}
#endif
if (!(account->flags & MUTT_ACCT_LOGIN))
{
if (mutt_account_getuser(account) == 0)
{
mutt_str_strfcpy(account->login, account->user, sizeof(account->login));
account->flags |= MUTT_ACCT_LOGIN;
}
else
{
mutt_debug(LL_DEBUG1, "Couldn't get user info\n");
return -1;
}
}
return 0;
}
/**
* mutt_account_getpass - Fetch password into ConnAccount, if necessary
* @param account ConnAccount to fill
* @retval 0 Success
* @retval -1 Failure
*/
int mutt_account_getpass(struct ConnAccount *account)
{
char prompt[256];
if (account->flags & MUTT_ACCT_PASS)
return 0;
#ifdef USE_IMAP
else if ((account->type == MUTT_ACCT_TYPE_IMAP) && C_ImapPass)
mutt_str_strfcpy(account->pass, C_ImapPass, sizeof(account->pass));
#endif
#ifdef USE_POP
else if ((account->type == MUTT_ACCT_TYPE_POP) && C_PopPass)
mutt_str_strfcpy(account->pass, C_PopPass, sizeof(account->pass));
#endif
#ifdef USE_SMTP
else if ((account->type == MUTT_ACCT_TYPE_SMTP) && C_SmtpPass)
mutt_str_strfcpy(account->pass, C_SmtpPass, sizeof(account->pass));
#endif
#ifdef USE_NNTP
else if ((account->type == MUTT_ACCT_TYPE_NNTP) && C_NntpPass)
mutt_str_strfcpy(account->pass, C_NntpPass, sizeof(account->pass));
#endif
else if (OptNoCurses)
return -1;
else
{
snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
(account->flags & MUTT_ACCT_LOGIN) ? account->login : account->user,
account->host);
account->pass[0] = '\0';
if (mutt_get_password(prompt, account->pass, sizeof(account->pass)))
return -1;
}
account->flags |= MUTT_ACCT_PASS;
return 0;
}
/**
* mutt_account_unsetpass - Unset ConnAccount's password
* @param account ConnAccount to modify
*/
void mutt_account_unsetpass(struct ConnAccount *account)
{
account->flags &= ~MUTT_ACCT_PASS;
}
/**
* mutt_account_getoauthbearer - Get an OAUTHBEARER token
* @param account Account to use
* @retval ptr OAuth token
* @retval NULL Error
*
* Run an external command to generate the oauth refresh token for an account,
* then create and encode the OAUTHBEARER token based on RFC7628.
*
* @note Caller should free the token
*/
char *mutt_account_getoauthbearer(struct ConnAccount *account)
{
FILE *fp;
char *cmd = NULL;
char *token = NULL;
size_t token_size = 0;
char *oauthbearer = NULL;
size_t oalen;
char *encoded_token = NULL;
size_t encoded_len;
pid_t pid;
/* The oauthbearer token includes the login */
if (mutt_account_getlogin(account))
return NULL;
#ifdef USE_IMAP
if ((account->type == MUTT_ACCT_TYPE_IMAP) && C_ImapOauthRefreshCmd)
cmd = C_ImapOauthRefreshCmd;
#endif
#ifdef USE_POP
else if ((account->type == MUTT_ACCT_TYPE_POP) && C_PopOauthRefreshCmd)
cmd = C_PopOauthRefreshCmd;
#endif
#ifdef USE_SMTP
else if ((account->type == MUTT_ACCT_TYPE_SMTP) && C_SmtpOauthRefreshCmd)
cmd = C_SmtpOauthRefreshCmd;
#endif
if (!cmd)
{
/* L10N: You will see this error message if (1) you have "oauthbearer" in
one of your $*_authenticators and (2) you do not have the corresponding
$*_oauth_refresh_command defined. So the message does not mean "None of
your $*_oauth_refresh_command's are defined." */
mutt_error(_("No OAUTH refresh command defined"));
return NULL;
}
pid = mutt_create_filter(cmd, NULL, &fp, NULL);
if (pid < 0)
{
mutt_perror(_("Unable to run refresh command"));
return NULL;
}
token = mutt_file_read_line(NULL, &token_size, fp, NULL, 0);
mutt_file_fclose(&fp);
mutt_wait_filter(pid);
if (!token || (*token == '\0'))
{
mutt_error(_("Command returned empty string"));
FREE(&token);
return NULL;
}
/* Determine the length of the keyed message digest, add 50 for overhead. */
oalen = strlen(account->login) + strlen(account->host) + strlen(token) + 50;
oauthbearer = mutt_mem_malloc(oalen);
snprintf(oauthbearer, oalen, "n,a=%s,\001host=%s\001port=%d\001auth=Bearer %s\001\001",
account->login, account->host, account->port, token);
FREE(&token);
encoded_len = strlen(oauthbearer) * 4 / 3 + 10;
encoded_token = mutt_mem_malloc(encoded_len);
mutt_b64_encode(oauthbearer, strlen(oauthbearer), encoded_token, encoded_len);
FREE(&oauthbearer);
return encoded_token;
}