Skip to content

Commit c878317

Browse files
ian-abbottcorecode
authored andcommitted
dma.c: Improve override of 'from' host by MASQUERADE config setting
This allows a MASQUERADE config setting containing only a host name to override any host name set by the -f command line option or by the EMAIL environment variable. Previously, that only worked if the MASQUERADE config setting also overrode the user name. The overall algorithm is as follows: 1. Select (incomplete) address from -f arg (highest priority), or EMAIL env (lower priority) or nothing. 2. If got address from step 1, split into user and host parts, either of which could be empty. 3. Replace empty host or empty user parts with defaults. 4. Apply MASQUERADE, possibly replacing host and/or user parts. 5. Construct final sender address from host and user parts. Fixes #114
1 parent 1b10f76 commit c878317

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

dma.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,57 @@ static char *
9999
set_from(struct queue *queue, const char *osender)
100100
{
101101
const char *addr;
102+
const char *from_user = NULL;
103+
const char *from_host = NULL;
102104
char *sender;
105+
int len_user = 0; /* length of user */
103106

104-
if (config.masquerade_user) {
105-
addr = config.masquerade_user;
106-
} else if (osender) {
107+
/* Get any sender address from parameters or environment. */
108+
if (osender) {
107109
addr = osender;
108-
} else if (getenv("EMAIL") != NULL) {
109-
addr = getenv("EMAIL");
110110
} else {
111-
addr = username;
111+
addr = getenv("EMAIL");
112112
}
113113

114-
if (!strchr(addr, '@')) {
115-
const char *from_host = hostname();
114+
if (addr) {
115+
/*
116+
* Try and split into user and host. User is not
117+
* null-terminated if the split is successful.
118+
*/
119+
from_user = addr;
120+
from_host = strchr(addr, '@');
121+
if (from_host) {
122+
len_user = from_host - addr;
123+
from_host++;
124+
} else {
125+
len_user = strlen(from_user);
126+
}
127+
}
116128

117-
if (config.masquerade_host)
118-
from_host = config.masquerade_host;
129+
/* Replace empty user name and/or host name with defaults. */
130+
if (len_user == 0) {
131+
from_user = username;
132+
}
133+
if (from_host == NULL || strlen(from_host) == 0) {
134+
from_host = hostname();
135+
}
119136

120-
if (asprintf(&sender, "%s@%s", addr, from_host) <= 0)
121-
return (NULL);
122-
} else {
123-
sender = strdup(addr);
124-
if (sender == NULL)
125-
return (NULL);
137+
/* Deal with masquerades. */
138+
if (config.masquerade_user) {
139+
from_user = config.masquerade_user;
140+
}
141+
if (config.masquerade_host) {
142+
from_host = config.masquerade_host;
143+
}
144+
145+
if (from_user != addr) {
146+
/* Fix up length of user. */
147+
len_user = strlen(from_user);
148+
}
149+
150+
/* Construct final sender address. */
151+
if (asprintf(&sender, "%.*s@%s", len_user, from_user, from_host) <= 0) {
152+
return (NULL);
126153
}
127154

128155
if (strchr(sender, '\n') != NULL) {

0 commit comments

Comments
 (0)