1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010-2021 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <stdlib.h>
22 #include <string.h>
23 #include <mailutils/errno.h>
24 #include <mailutils/stream.h>
25 #include <mailutils/secret.h>
26 #include <mailutils/sys/imap.h>
27
28 int
mu_imap_login(mu_imap_t imap,const char * user,const char * pass)29 mu_imap_login (mu_imap_t imap, const char *user, const char *pass)
30 {
31 int status;
32
33 if (imap == NULL)
34 return EINVAL;
35 if (!imap->io)
36 return MU_ERR_NO_TRANSPORT;
37 if (imap->session_state != MU_IMAP_SESSION_NONAUTH)
38 return MU_ERR_SEQ;
39
40 switch (imap->client_state)
41 {
42 case MU_IMAP_CLIENT_READY:
43 if (mu_imap_trace_mask (imap, MU_IMAP_TRACE_QRY, MU_XSCRIPT_SECURE))
44 _mu_imap_xscript_level (imap, MU_XSCRIPT_SECURE);
45 status = _mu_imap_tag_next (imap);
46 MU_IMAP_CHECK_EAGAIN (imap, status);
47 status = mu_imapio_send_command (imap->io, imap->tag_str, NULL,
48 "LOGIN", user, pass, NULL);
49 _mu_imap_xscript_level (imap, MU_XSCRIPT_NORMAL);
50 /* FIXME: how to obscure the passwd in the stream buffer? */
51 MU_IMAP_CHECK_EAGAIN (imap, status);
52 MU_IMAP_FCLR (imap, MU_IMAP_RESP);
53 imap->client_state = MU_IMAP_CLIENT_LOGIN_RX;
54
55 case MU_IMAP_CLIENT_LOGIN_RX:
56 status = _mu_imap_response (imap, NULL, NULL);
57 imap->client_state = MU_IMAP_CLIENT_READY;
58 MU_IMAP_CHECK_EAGAIN (imap, status);
59 switch (imap->response)
60 {
61 case MU_IMAP_OK:
62 imap->session_state = MU_IMAP_SESSION_AUTH;
63 break;
64
65 case MU_IMAP_NO:
66 status = EACCES;
67 break;
68
69 case MU_IMAP_BAD:
70 status = MU_ERR_BADREPLY;
71 break;
72 }
73 break;
74
75 default:
76 status = EINPROGRESS;
77 }
78 return status;
79 }
80
81 int
mu_imap_login_secret(mu_imap_t imap,const char * user,mu_secret_t secret)82 mu_imap_login_secret (mu_imap_t imap, const char *user, mu_secret_t secret)
83 {
84 int rc = mu_imap_login (imap, user, mu_secret_password (secret));
85 mu_secret_password_unref (secret);
86 return rc;
87 }
88
89
90