mbx_default.c
7.29 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
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
#include <mailutils/mailbox.h>
#include <mailutils/mutil.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/mu_auth.h>
const char *mu_path_maildir = MU_PATH_MAILDIR;
const char *mu_path_folder_dir = "Mail";
/* Is this a security risk? */
#define USE_ENVIRON 1
static int
is_proto (const char *p)
{
for (; *p && *p != '/'; p++)
if (*p == ':')
return 1;
return 0;
}
static int
split_shortcut (const char *file, const char pfx[], char **user, char **rest)
{
*user = NULL;
*rest = NULL;
if (!strchr (pfx, file[0]))
return 0;
if (*++file == 0)
return 0;
else
{
char *p = strchr (file, '/');
int len;
if (p)
len = p - file + 1;
else
len = strlen (file) + 1;
if (len == 1)
*user = NULL;
else
{
*user = calloc (1, len);
if (!*user)
return ENOMEM;
memcpy (*user, file, len);
(*user)[len-1] = 0;
}
file += len-1;
if (file[0] == '/')
file++;
}
if (file[0])
{
*rest = strdup (file);
if (!*rest)
{
free (*user);
return ENOMEM;
}
}
return 0;
}
static char *
get_homedir (const char *user)
{
char *homedir = NULL;
struct mu_auth_data *auth = NULL;
if (user)
{
auth = mu_get_auth_by_name (user);
if (auth)
homedir = auth->dir;
}
else
{
#ifdef USE_ENVIRON
/* NOTE: Should we honor ${HOME}? */
homedir = getenv ("HOME");
if (homedir == NULL)
{
auth = mu_get_auth_by_name (user);
if (auth)
homedir = auth->dir;
}
#else
auth = mu_get_auth_by_name (user);
if (auth)
homedir = auth->dir;
#endif
}
if (homedir)
homedir = strdup (homedir);
mu_auth_data_free (auth);
return homedir;
}
static int
user_mailbox_name (const char *user, char **mailbox_name)
{
char *p;
p = strchr (mu_path_maildir, ':');
if (p && strncmp (mu_path_maildir, "mbox", p - mu_path_maildir))
{
*mailbox_name = strdup (mu_path_maildir);
return 0;
}
#ifdef USE_ENVIRON
if (!user)
user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
#endif
if (user)
{
*mailbox_name = malloc (strlen (user) + strlen (mu_path_maildir) + 2);
if (*mailbox_name == NULL)
return ENOMEM;
sprintf (*mailbox_name, "%s%s", mu_path_maildir, user);
}
else
{
struct mu_auth_data *auth = mu_get_auth_by_uid (getuid ());
if (!auth)
{
mu_error ("Who am I ?\n");
return EINVAL;
}
*mailbox_name = strdup (auth->mailbox);
mu_auth_data_free (auth);
}
return 0;
}
static int
plus_expand (const char *file, char **buf)
{
char *user = NULL;
char *path = NULL;
char *home;
int status, len;
if ((status = split_shortcut (file, "+=", &path, &user)))
return status;
if (!path)
{
free (user);
return ENOENT;
}
home = get_homedir (user);
if (!home)
{
free (user);
free (path);
return ENOENT;
}
if (mu_path_folder_dir[0] == '/' || is_proto (mu_path_folder_dir))
{
len = strlen (mu_path_folder_dir) + strlen (path) + 2;
*buf = malloc (len);
sprintf (*buf, "%s/%s", mu_path_folder_dir, path);
}
else
{
len = strlen (home) + strlen (mu_path_folder_dir) + strlen (path) + 3;
*buf = malloc (len);
sprintf (*buf, "%s/%s/%s", home, mu_path_folder_dir, path);
}
(*buf)[len-1] = 0;
free (user);
free (path);
free (home);
return 0;
}
/* Do ~ , if necessary. */
static int
tilde_expand (const char *file, char **buf)
{
char *user = NULL;
char *path = NULL;
char *home;
int status;
int len;
if ((status = split_shortcut (file, "~", &user, &path)))
return status;
if (!path)
{
if (user)
free (user);
return ENOENT;
}
home = get_homedir (user);
if (!home)
{
free (user);
free (path);
return MU_ERR_NO_SUCH_USER;
}
free (user); /* not needed anymore */
len = strlen (home) + strlen (path) + 2;
*buf = malloc (len);
if (*buf)
{
sprintf (*buf, "%s/%s", home, path);
(*buf)[len-1] = 0;
}
free (path);
return *buf ? 0 : ENOMEM;
}
static int
percent_expand (const char *file, char **mbox)
{
char *user = NULL;
char *path = NULL;
int status;
if ((status = split_shortcut (file, "%", &user, &path)))
return status;
if (path)
{
free (user);
free (path);
return ENOENT;
}
status = user_mailbox_name (user, mbox);
free (user);
return status;
}
/* We are trying to be smart about the location of the mail.
mailbox_create() is not doing this.
% --> system mailbox for the real uid
%user --> system mailbox for the given user
~/file --> /home/user/file
~user/file --> /home/user/file
+file --> /home/user/Mail/file
=file --> /home/user/Mail/file
*/
int
mailbox_create_default (mailbox_t *pmbox, const char *mail)
{
char *mbox = NULL;
char *tmp_mbox = NULL;
int status = 0;
/* Sanity. */
if (pmbox == NULL)
return MU_ERR_OUT_PTR_NULL;
/* Other utilities may not understand GNU mailutils url namespace, so
use FOLDER instead, to not confuse others by using MAIL. */
if (mail == NULL || *mail == '\0')
{
mail = getenv ("FOLDER");
/* Fallback to wellknown environment. */
if (!mail)
mail = getenv ("MAIL");
if (!mail)
{
if ((status = user_mailbox_name (NULL, &tmp_mbox)))
return status;
mail = tmp_mbox;
}
}
switch (mail[0])
{
case '%':
status = percent_expand (mail, &mbox);
break;
case '~':
status = tilde_expand (mail, &mbox);
break;
case '+':
case '=':
status = plus_expand (mail, &mbox);
break;
case '/':
mbox = strdup (mail);
break;
default:
if (!is_proto (mail))
{
tmp_mbox = mu_getcwd();
mbox = malloc (strlen (tmp_mbox) + strlen (mail) + 2);
sprintf (mbox, "%s/%s", tmp_mbox, mail);
}
else
mbox = strdup (mail);
break;
}
if (tmp_mbox)
free (tmp_mbox);
if (status)
return status;
status = mailbox_create (pmbox, mbox);
free (mbox);
return status;
}