Commit 131ddb25 131ddb25a51862bb31fe5fd7a1111896fbb20c58 by Sergey Poznyakoff

Honor MAIL and FOLDER envars if _mu_mailbox_pattern is not set.

The _mu_mailbox_pattern variable is supposed to be set only from
configuration file.  However, the mu_mailbox_url function would
also set it as a side effect.  This produced strange results;
in particular unsetting HOME variable caused mail utilities to ignore
MAIL variable as well (as reported by Dan Jacobson on 2015-04-30,
<87vbgemcc7.fsf@jidanni.org>).

* configure.ac (MU_PATH_MAILDIR): Don't end with a slash.
* libmailutils/mailbox/mbx_default.c
(mu_normalize_mailbox_url): Use mu_make_file_name.
(mu_mailbox_url): Don't set _mu_mailbox_pattern.
1 parent 22b58503
...@@ -350,7 +350,7 @@ AH_BOTTOM( ...@@ -350,7 +350,7 @@ AH_BOTTOM(
350 #ifdef MU_CONF_MAILDIR 350 #ifdef MU_CONF_MAILDIR
351 # define MU_PATH_MAILDIR MU_CONF_MAILDIR 351 # define MU_PATH_MAILDIR MU_CONF_MAILDIR
352 #else 352 #else
353 # define MU_PATH_MAILDIR PATH_MAILDIR "/" 353 # define MU_PATH_MAILDIR PATH_MAILDIR
354 #endif]) 354 #endif])
355 355
356 ################################## 356 ##################################
......
...@@ -49,12 +49,12 @@ static char *_mu_mailbox_pattern; ...@@ -49,12 +49,12 @@ static char *_mu_mailbox_pattern;
49 static char *_default_folder_dir = "Mail"; 49 static char *_default_folder_dir = "Mail";
50 static char *_mu_folder_dir; 50 static char *_mu_folder_dir;
51 51
52 #define USERSUFFIX "${user}"
53
52 static int 54 static int
53 mu_normalize_mailbox_url (char **pout, const char *dir) 55 mu_normalize_mailbox_url (char **pout, const char *dir)
54 { 56 {
55 int len; 57 int len;
56 int addslash = 0;
57 #define USERSUFFIX "${user}"
58 58
59 if (!pout) 59 if (!pout)
60 return MU_ERR_OUT_PTR_NULL; 60 return MU_ERR_OUT_PTR_NULL;
...@@ -67,18 +67,12 @@ mu_normalize_mailbox_url (char **pout, const char *dir) ...@@ -67,18 +67,12 @@ mu_normalize_mailbox_url (char **pout, const char *dir)
67 else 67 else
68 return MU_ERR_BAD_FILENAME; 68 return MU_ERR_BAD_FILENAME;
69 } 69 }
70 else if (dir[len-1] != '/') 70 else
71 addslash = 1; 71 *pout = mu_make_file_name (dir, USERSUFFIX);
72 72
73 *pout = malloc (strlen (dir) + (addslash ? 1 : 0) + sizeof USERSUFFIX);
74 if (!*pout) 73 if (!*pout)
75 return ENOMEM; 74 return errno;
76 75
77 strcpy (*pout, dir);
78 if (addslash)
79 strcat (*pout, "/");
80 strcat (*pout, USERSUFFIX);
81 #undef USERSUFFIX
82 return 0; 76 return 0;
83 } 77 }
84 78
...@@ -129,9 +123,9 @@ mu_set_folder_directory (const char *p) ...@@ -129,9 +123,9 @@ mu_set_folder_directory (const char *p)
129 const char * 123 const char *
130 mu_mailbox_url () 124 mu_mailbox_url ()
131 { 125 {
132 if (!_mu_mailbox_pattern) 126 if (_mu_mailbox_pattern)
133 mu_set_mail_directory (MU_PATH_MAILDIR); 127 return _mu_mailbox_pattern;
134 return _mu_mailbox_pattern; 128 return MU_PATH_MAILDIR "/" USERSUFFIX;
135 } 129 }
136 130
137 const char * 131 const char *
...@@ -145,6 +139,7 @@ mu_folder_directory () ...@@ -145,6 +139,7 @@ mu_folder_directory ()
145 int 139 int
146 mu_construct_user_mailbox_url (char **pout, const char *name) 140 mu_construct_user_mailbox_url (char **pout, const char *name)
147 { 141 {
142 int rc;
148 const char *pat = mu_mailbox_url (); 143 const char *pat = mu_mailbox_url ();
149 const char *env[3]; 144 const char *env[3];
150 struct mu_wordsplit ws; 145 struct mu_wordsplit ws;
...@@ -153,13 +148,15 @@ mu_construct_user_mailbox_url (char **pout, const char *name) ...@@ -153,13 +148,15 @@ mu_construct_user_mailbox_url (char **pout, const char *name)
153 env[1] = (char*) name; 148 env[1] = (char*) name;
154 env[2] = NULL; 149 env[2] = NULL;
155 ws.ws_env = env; 150 ws.ws_env = env;
156 if (mu_wordsplit (pat, &ws, 151 rc = mu_wordsplit (pat, &ws,
157 MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD | 152 MU_WRDSF_NOSPLIT | MU_WRDSF_NOCMD |
158 MU_WRDSF_ENV | MU_WRDSF_ENV_KV)) 153 MU_WRDSF_ENV | MU_WRDSF_ENV_KV);
154
155 if (rc)
159 { 156 {
160 mu_error (_("cannot expand line `%s': %s"), pat, 157 mu_error (_("cannot expand line `%s': %s"), pat,
161 mu_wordsplit_strerror (&ws)); 158 mu_wordsplit_strerror (&ws));
162 return errno; 159 return rc;
163 } 160 }
164 161
165 if (ws.ws_wordc == 0) 162 if (ws.ws_wordc == 0)
...@@ -173,9 +170,6 @@ mu_construct_user_mailbox_url (char **pout, const char *name) ...@@ -173,9 +170,6 @@ mu_construct_user_mailbox_url (char **pout, const char *name)
173 return 0; 170 return 0;
174 } 171 }
175 172
176 /* Is this a security risk? */
177 #define USE_ENVIRON 1
178
179 static int 173 static int
180 split_shortcut (const char *file, const char pfx[], char **user, char **rest) 174 split_shortcut (const char *file, const char pfx[], char **user, char **rest)
181 { 175 {
...@@ -239,7 +233,6 @@ get_homedir (const char *user) ...@@ -239,7 +233,6 @@ get_homedir (const char *user)
239 } 233 }
240 else 234 else
241 { 235 {
242 #ifdef USE_ENVIRON
243 /* NOTE: Should we honor ${HOME}? */ 236 /* NOTE: Should we honor ${HOME}? */
244 homedir = getenv ("HOME"); 237 homedir = getenv ("HOME");
245 if (homedir == NULL) 238 if (homedir == NULL)
...@@ -248,11 +241,6 @@ get_homedir (const char *user) ...@@ -248,11 +241,6 @@ get_homedir (const char *user)
248 if (auth) 241 if (auth)
249 homedir = auth->dir; 242 homedir = auth->dir;
250 } 243 }
251 #else
252 auth = mu_get_auth_by_name (user);
253 if (auth)
254 homedir = auth->dir;
255 #endif
256 } 244 }
257 245
258 if (homedir) 246 if (homedir)
...@@ -264,10 +252,8 @@ get_homedir (const char *user) ...@@ -264,10 +252,8 @@ get_homedir (const char *user)
264 static int 252 static int
265 user_mailbox_name (const char *user, char **mailbox_name) 253 user_mailbox_name (const char *user, char **mailbox_name)
266 { 254 {
267 #ifdef USE_ENVIRON
268 if (!user) 255 if (!user)
269 user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER"); 256 user = (getenv ("LOGNAME")) ? getenv ("LOGNAME") : getenv ("USER");
270 #endif
271 257
272 if (user) 258 if (user)
273 { 259 {
...@@ -416,10 +402,8 @@ mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail) ...@@ -416,10 +402,8 @@ mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail)
416 use FOLDER instead, to not confuse others by using MAIL. */ 402 use FOLDER instead, to not confuse others by using MAIL. */
417 mail = getenv ("FOLDER"); 403 mail = getenv ("FOLDER");
418 if (!mail) 404 if (!mail)
419 { 405 /* Fallback to well-known environment. */
420 /* Fallback to well-known environment. */ 406 mail = getenv ("MAIL");
421 mail = getenv ("MAIL");
422 }
423 } 407 }
424 408
425 if (!mail) 409 if (!mail)
...@@ -429,7 +413,7 @@ mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail) ...@@ -429,7 +413,7 @@ mu_mailbox_create_default (mu_mailbox_t *pmbox, const char *mail)
429 mail = tmp_mbox; 413 mail = tmp_mbox;
430 } 414 }
431 } 415 }
432 416
433 p = mu_tilde_expansion (mail, MU_HIERARCHY_DELIMITER, NULL); 417 p = mu_tilde_expansion (mail, MU_HIERARCHY_DELIMITER, NULL);
434 if (tmp_mbox) 418 if (tmp_mbox)
435 free (tmp_mbox); 419 free (tmp_mbox);
......