summaryrefslogtreecommitdiffabout
path: root/libetpan/src/driver/implementation/pop3/pop3storage.c
blob: 1cff65019ff5941d197cf7df575852e33412cd33 (plain)
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
/*
 * libEtPan! -- a mail stuff library
 *
 * Copyright (C) 2001, 2005 - DINH Viet Hoa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the libEtPan! project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * $Id$
 */

#include "pop3storage.h"

#include <stdlib.h>
#include <string.h>

#include "mail.h"
#include "mailstorage_tools.h"
#include "maildriver.h"

/* pop3 storage */

#define POP3_DEFAULT_PORT  110
#define POP3S_DEFAULT_PORT 995

static int pop3_mailstorage_connect(struct mailstorage * storage);
static int pop3_mailstorage_get_folder_session(struct mailstorage * storage,
    char * pathname, mailsession ** result);
static void pop3_mailstorage_uninitialize(struct mailstorage * storage);

static mailstorage_driver pop3_mailstorage_driver = {
  .sto_name               = "pop3",
  .sto_connect            = pop3_mailstorage_connect,
  .sto_get_folder_session = pop3_mailstorage_get_folder_session,
  .sto_uninitialize       = pop3_mailstorage_uninitialize,
};

int pop3_mailstorage_init(struct mailstorage * storage,
    char * pop3_servername, uint16_t pop3_port,
    char * pop3_command,
    int pop3_connection_type, int pop3_auth_type,
    char * pop3_login, char * pop3_password,
    int pop3_cached, char * pop3_cache_directory, char * pop3_flags_directory)
{
  struct pop3_mailstorage * pop3_storage;

  pop3_storage = malloc(sizeof(* pop3_storage));
  if (pop3_storage == NULL)
    goto err;
  
  pop3_storage->pop3_servername = strdup(pop3_servername);
  if (pop3_storage->pop3_servername == NULL)
    goto free;

  pop3_storage->pop3_connection_type = pop3_connection_type;
  
  if (pop3_port == 0) {
    switch (pop3_connection_type) {
    case CONNECTION_TYPE_PLAIN:
    case CONNECTION_TYPE_TRY_STARTTLS:
    case CONNECTION_TYPE_STARTTLS:
    case CONNECTION_TYPE_COMMAND:
    case CONNECTION_TYPE_COMMAND_TRY_STARTTLS:
    case CONNECTION_TYPE_COMMAND_STARTTLS:
      pop3_port = POP3_DEFAULT_PORT;
      break;

    case CONNECTION_TYPE_TLS:
    case CONNECTION_TYPE_COMMAND_TLS:
      pop3_port = POP3S_DEFAULT_PORT;
      break;
    }
  }

  pop3_storage->pop3_port = pop3_port;
  
  if (pop3_command != NULL) {
    pop3_storage->pop3_command = strdup(pop3_command);
    if (pop3_storage->pop3_command == NULL)
      goto free_servername;
  }
  else
    pop3_storage->pop3_command = NULL;
  
  pop3_storage->pop3_auth_type = pop3_auth_type;
  
  if (pop3_login != NULL) {
    pop3_storage->pop3_login = strdup(pop3_login);
    if (pop3_storage->pop3_login == NULL)
      goto free_command;
  }
  else
    pop3_storage->pop3_login = NULL;
  
  if (pop3_password != NULL) {
    pop3_storage->pop3_password = strdup(pop3_password);
    if (pop3_storage->pop3_password == NULL)
      goto free_login;
  }
  else
    pop3_storage->pop3_password = NULL;

  pop3_storage->pop3_cached = pop3_cached;

  if (pop3_cached && (pop3_cache_directory != NULL) &&
      (pop3_flags_directory != NULL)) {
    pop3_storage->pop3_cache_directory = strdup(pop3_cache_directory);
    if (pop3_storage->pop3_cache_directory == NULL)
      goto free_password;
    pop3_storage->pop3_flags_directory = strdup(pop3_flags_directory);
    if (pop3_storage->pop3_flags_directory == NULL)
      goto free_cache_directory;
  }
  else {
    pop3_storage->pop3_cached = FALSE;
    pop3_storage->pop3_cache_directory = NULL;
    pop3_storage->pop3_flags_directory = NULL;
  }

  storage->sto_data = pop3_storage;
  storage->sto_driver = &pop3_mailstorage_driver;

  return MAIL_NO_ERROR;

 free_cache_directory:
  free(pop3_storage->pop3_cache_directory);
 free_password:
  if (pop3_storage->pop3_password != NULL)
    free(pop3_storage->pop3_password);
 free_login:
  if (pop3_storage->pop3_login != NULL)
    free(pop3_storage->pop3_login);
 free_command:
  if (pop3_storage->pop3_command != NULL)
    free(pop3_storage->pop3_command);
 free_servername:
  if (pop3_storage->pop3_servername != NULL)
    free(pop3_storage->pop3_servername);
 free:
  free(pop3_storage);
 err:
  return MAIL_ERROR_MEMORY;
}

static void pop3_mailstorage_uninitialize(struct mailstorage * storage)
{
  struct pop3_mailstorage * pop3_storage;

  pop3_storage = storage->sto_data;

  if (pop3_storage->pop3_flags_directory != NULL)
    free(pop3_storage->pop3_flags_directory);
  if (pop3_storage->pop3_cache_directory != NULL)
    free(pop3_storage->pop3_cache_directory);
  if (pop3_storage->pop3_password != NULL)
    free(pop3_storage->pop3_password);
  if (pop3_storage->pop3_login != NULL)
    free(pop3_storage->pop3_login);
  if (pop3_storage->pop3_command != NULL)
    free(pop3_storage->pop3_command);
  free(pop3_storage->pop3_servername);
  free(pop3_storage);
  
  storage->sto_data = pop3_storage;
}

static int pop3_mailstorage_connect(struct mailstorage * storage)
{
  struct pop3_mailstorage * pop3_storage;
  mailsession_driver * driver;
  int r;
  int res;
  mailsession * session;
  int auth_type;

  pop3_storage = storage->sto_data;

  if (pop3_storage->pop3_cached)
    driver = pop3_cached_session_driver;
  else
    driver = pop3_session_driver;

  r = mailstorage_generic_connect(driver,
      pop3_storage->pop3_servername,
      pop3_storage->pop3_port, pop3_storage->pop3_command,
      pop3_storage->pop3_connection_type,
      POP3DRIVER_CACHED_SET_CACHE_DIRECTORY,
      pop3_storage->pop3_cache_directory,
      POP3DRIVER_CACHED_SET_FLAGS_DIRECTORY,
      pop3_storage->pop3_flags_directory,
      &session);
  switch (r) {
  case MAIL_NO_ERROR_NON_AUTHENTICATED:
  case MAIL_NO_ERROR_AUTHENTICATED:
  case MAIL_NO_ERROR:
    break;
  default:
    res = r;
    goto err;
  }

  auth_type = -1;
  switch (pop3_storage->pop3_auth_type) {
  case POP3_AUTH_TYPE_PLAIN:
    auth_type = POP3DRIVER_AUTH_TYPE_PLAIN;
    break;
  case POP3_AUTH_TYPE_APOP:
    auth_type = POP3DRIVER_AUTH_TYPE_APOP;
    break;
  case POP3_AUTH_TYPE_TRY_APOP:
    auth_type = POP3DRIVER_AUTH_TYPE_TRY_APOP;
    break;
  }

  if (auth_type != -1) {
    mailsession_parameters(session, POP3DRIVER_SET_AUTH_TYPE, &auth_type);
  }

  r = mailstorage_generic_auth(session, r,
      pop3_storage->pop3_auth_type,
      pop3_storage->pop3_login,
      pop3_storage->pop3_password);
  if (r != MAIL_NO_ERROR) {
    if (pop3_storage->pop3_auth_type == POP3_AUTH_TYPE_TRY_APOP) {
      /* try in clear authentication */
      mailsession_free(session);
      
      pop3_storage->pop3_auth_type = POP3_AUTH_TYPE_PLAIN;
      r = mailstorage_connect(storage);
      if (r != MAIL_NO_ERROR) {
        res = r;
        return res;
      }
      pop3_storage->pop3_auth_type = POP3_AUTH_TYPE_TRY_APOP;
      
      return MAIL_NO_ERROR;
    }
    
    res = r;
    goto free;
  }
  
  storage->sto_session = session;
  
  return MAIL_NO_ERROR;

 free:
  mailsession_free(session);
 err:
  return res;
}

static int pop3_mailstorage_get_folder_session(struct mailstorage * storage,
    char * pathname, mailsession ** result)
{
  * result = storage->sto_session;
 
  return MAIL_NO_ERROR;
}