Skip to content

Commit

Permalink
Do some expansion refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
flowerysong committed Jan 11, 2022
1 parent 424995b commit e36ee31
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 281 deletions.
243 changes: 83 additions & 160 deletions address.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,23 +294,33 @@ add_address(struct expand *exp, char *addr, struct envelope *error_env,
}


int
simta_address_status
address_expand(struct expand *exp) {
struct exp_addr * e_addr;
const ucl_object_t *red = NULL;
ucl_object_iter_t iter = NULL;
const ucl_object_t *rule = NULL;
const char * type = NULL;
const char * path = NULL;
struct exp_addr * e_addr;
const ucl_object_t * red = NULL;
ucl_object_iter_t iter = NULL;
const ucl_object_t * rule = NULL;
const char * type = NULL;
const char * src = NULL;
const char * status = NULL;
simta_address_status rc = ADDRESS_NOT_FOUND;

e_addr = exp->exp_addr_cursor;

switch (e_addr->e_addr_type) {
case ADDRESS_TYPE_EMAIL:
#ifdef HAVE_LDAP
if (e_addr->e_addr_type == ADDRESS_TYPE_LDAP) {
type = "ldap";
rule = e_addr->e_addr_parent_rule;
src = ucl_object_tostring(ucl_object_lookup_path(rule, "ldap.uri"));
rc = simta_ldap_expand(rule, exp, e_addr);
}
#endif /* HAVE_LDAP */

if (e_addr->e_addr_type == ADDRESS_TYPE_EMAIL) {
if (strlen(e_addr->e_addr_at + 1) > SIMTA_MAX_HOST_NAME_LEN) {
syslog(LOG_ERR, "Expand env <%s>: <%s>: domain too long",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_SYSERROR);
return ADDRESS_SYSERROR;
}

/* Check to see if domain is off the local host */
Expand All @@ -319,170 +329,83 @@ address_expand(struct expand *exp) {
if ((red == NULL) || !red_does_expansion(red)) {
simta_debuglog(1, "Expand env <%s>: <%s>: expansion complete",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);
}
break;

#ifdef HAVE_LDAP
case ADDRESS_TYPE_LDAP:
rule = e_addr->e_addr_parent_rule;
;
simta_debuglog(2, "Expand env <%s>: <%s>: LDAP data",
exp->exp_env->e_id, e_addr->e_addr);
switch (simta_ldap_expand(rule, exp, e_addr)) {
case ADDRESS_EXCLUDE:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: expanded",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_EXCLUDE);

case ADDRESS_FINAL:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: terminal",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);

case ADDRESS_NOT_FOUND:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: not found",
exp->exp_env->e_id, e_addr->e_addr);
goto not_found;

case ADDRESS_SYSERROR:
return (ADDRESS_SYSERROR);

default:
panic("address_expand ldap_expand out of range");
return ADDRESS_OK;
}

#endif /* HAVE_LDAP */

default:
panic("address_expand: address type out of range");
}
/* Expand user using expansion table for domain */
iter = ucl_object_iterate_new(ucl_object_lookup(red, "rule"));
while ((rule = ucl_object_iterate_safe(iter, false)) != NULL) {
exp->exp_current_rule = rule;

/* At this point, we should have a valid address destined for
* a local domain. Now we use the expansion table to resolve it.
*/

/* Expand user using expansion table for domain */
iter = ucl_object_iterate_new(ucl_object_lookup(red, "rule"));
while ((rule = ucl_object_iterate_safe(iter, false)) != NULL) {
exp->exp_current_rule = rule;

if (!ucl_object_toboolean(
ucl_object_lookup_path(rule, "expand.enabled"))) {
simta_debuglog(3,
"Expand env <%s>: <%s>: skipping non-expand rule %s",
exp->exp_env->e_id, e_addr->e_addr,
ucl_object_tostring_forced(rule));
continue;
}

type = ucl_object_tostring(ucl_object_lookup(rule, "type"));

#ifdef HAVE_LMDB
if (strcasecmp(type, "alias") == 0) {
path = ucl_object_tostring(
ucl_object_lookup_path(rule, "alias.path"));
switch (alias_expand(exp, e_addr, rule)) {
case ADDRESS_EXCLUDE:
simta_debuglog(1, "Expand.alias env <%s>: <%s>: found in DB %s",
exp->exp_env->e_id, e_addr->e_addr, path);
return (ADDRESS_EXCLUDE);

case ADDRESS_NOT_FOUND:
simta_debuglog(1, "Expand.alias env <%s>: <%s>: not in DB %s",
exp->exp_env->e_id, e_addr->e_addr, path);
if (!ucl_object_toboolean(
ucl_object_lookup_path(rule, "expand.enabled"))) {
simta_debuglog(3,
"Expand env <%s>: <%s>: skipping non-expand rule %s",
exp->exp_env->e_id, e_addr->e_addr,
ucl_object_tostring_forced(rule));
continue;
}

case ADDRESS_SYSERROR:
return (ADDRESS_SYSERROR);
type = ucl_object_tostring(ucl_object_lookup(rule, "type"));

default:
panic("address_expand default alias switch");
#ifdef HAVE_LMDB
if (strcasecmp(type, "alias") == 0) {
src = ucl_object_tostring(
ucl_object_lookup_path(rule, "alias.path"));
rc = alias_expand(exp, e_addr, rule);
}
}
#endif /* HAVE_LMDB */

if (strcasecmp(type, "password") == 0) {
path = ucl_object_tostring(
ucl_object_lookup_path(rule, "password.path"));
switch (password_expand(exp, e_addr, rule)) {
case ADDRESS_EXCLUDE:
simta_debuglog(1,
"Expand.password env <%s>: <%s>: found in file %s",
exp->exp_env->e_id, e_addr->e_addr, path);
return (ADDRESS_EXCLUDE);

case ADDRESS_FINAL:
simta_debuglog(1,
"Expand.password env <%s>: <%s>: terminal in file %s",
exp->exp_env->e_id, e_addr->e_addr, path);
return (ADDRESS_FINAL);

case ADDRESS_NOT_FOUND:
simta_debuglog(1,
"Expand.password env <%s>: <%s>: not in file %s",
exp->exp_env->e_id, e_addr->e_addr, path);
continue;

case ADDRESS_SYSERROR:
return (ADDRESS_SYSERROR);

default:
panic("address_expand default password switch");
if (strcasecmp(type, "password") == 0) {
src = ucl_object_tostring(
ucl_object_lookup_path(rule, "password.path"));
rc = password_expand(exp, e_addr, rule);
}
}

if (strcasecmp(type, "srs") == 0) {
switch (srs_expand(exp, e_addr, rule)) {
case ADDRESS_EXCLUDE:
simta_debuglog(1, "Expand.SRS env <%s>: <%s>: valid",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_EXCLUDE);

case ADDRESS_NOT_FOUND:
simta_debuglog(1, "Expand.SRS env <%s>: <%s>: not valid",
exp->exp_env->e_id, e_addr->e_addr);
continue;
if (strcasecmp(type, "srs") == 0) {
src = "SRS";
rc = srs_expand(exp, e_addr, rule);
}

case ADDRESS_SYSERROR:
return (ADDRESS_SYSERROR);
#ifdef HAVE_LDAP
if (strcasecmp(type, "ldap") == 0) {
src = ucl_object_tostring(
ucl_object_lookup_path(rule, "ldap.uri"));
rc = simta_ldap_expand(rule, exp, e_addr);
}
#endif /* HAVE_LDAP */

default:
panic("address_expand srs_expand out of range");
if (rc != ADDRESS_NOT_FOUND) {
break;
}
}
}

#ifdef HAVE_LDAP
if (strcasecmp(type, "ldap") == 0) {
switch (simta_ldap_expand(rule, exp, e_addr)) {
case ADDRESS_EXCLUDE:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: expanded",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_EXCLUDE);

case ADDRESS_FINAL:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: terminal",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);

case ADDRESS_NOT_FOUND:
simta_debuglog(1, "Expand.LDAP env <%s>: <%s>: not found",
exp->exp_env->e_id, e_addr->e_addr);
continue;
switch (rc) {
case ADDRESS_SYSERROR:
status = "error";
break;

case ADDRESS_SYSERROR:
return (ADDRESS_SYSERROR);
case ADDRESS_OK:
case ADDRESS_OK_SPAM:
status = "terminal";
break;

default:
panic("address_expand ldap_expand out of range");
}
}
#endif /* HAVE_LDAP */
case ADDRESS_EXCLUDE:
status = "found";
break;

case ADDRESS_NOT_FOUND:
status = "not found";
break;
}

#ifdef HAVE_LDAP
not_found:
#endif /* HAVE_LDAP */
simta_debuglog(1, "Expand.%s env <%s>: <%s>: %s in %s", type,
exp->exp_env->e_id, e_addr->e_addr, status, src);

if (rc != ADDRESS_NOT_FOUND) {
return rc;
}

/* If we can't resolve postmaster, add it to the dead queue. */
if (strncasecmp(e_addr->e_addr, "postmaster@", strlen("postmaster@")) ==
Expand All @@ -501,7 +424,7 @@ address_expand(struct expand *exp) {
"Expand env <%s>: <%s>: can't resolve postmaster, "
"expanding to dead queue",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);
return ADDRESS_OK;
}
}

Expand All @@ -511,10 +434,10 @@ address_expand(struct expand *exp) {
if (bounce_text(e_addr->e_addr_errors, TEXT_ERROR,
"address not found: ", e_addr->e_addr, NULL) != 0) {
/* bounce_text syslogs errors */
return (ADDRESS_SYSERROR);
return ADDRESS_SYSERROR;
}

return (ADDRESS_EXCLUDE);
return ADDRESS_EXCLUDE;
}


Expand Down Expand Up @@ -640,7 +563,7 @@ password_expand(
syslog(LOG_INFO,
"Expand.password env <%s>: <%s>: expanded to /dev/null",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);
return (ADDRESS_OK);
}

/* Check password file */
Expand All @@ -655,15 +578,15 @@ password_expand(
return (ADDRESS_NOT_FOUND);
}

ret = ADDRESS_FINAL;
ret = ADDRESS_OK;

/* Check .forward */
fname = yaslcat(yaslauto(passwd->pw_dir), "/.forward");

if (access(fname, F_OK) != 0) {
simta_debuglog(2, "Expand.password env <%s>: <%s>: no .forward",
exp->exp_env->e_id, e_addr->e_addr);
return (ADDRESS_FINAL);
return (ADDRESS_OK);
}

buf = simta_slurp(fname);
Expand Down
2 changes: 1 addition & 1 deletion expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ expand(struct envelope *unexpanded_env) {
/* the address is not a terminal local address */
break;

case ADDRESS_FINAL:
case ADDRESS_OK:
exp.exp_addr_cursor->e_addr_terminal = 1;
break;

Expand Down
21 changes: 12 additions & 9 deletions expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
#define EXPAND_FATAL 2

/* return codes for address expansion */
#define ADDRESS_SYSERROR 1
#define ADDRESS_NOT_FOUND 2
#define ADDRESS_FINAL 3
#define ADDRESS_EXCLUDE 4
typedef enum {
ADDRESS_OK,
ADDRESS_OK_SPAM,
ADDRESS_EXCLUDE,
ADDRESS_NOT_FOUND,
ADDRESS_SYSERROR,
} simta_address_status;

/* address types */
#define ADDRESS_TYPE_EMAIL 1
Expand Down Expand Up @@ -101,11 +104,11 @@ struct passwd *simta_getpwnam(const char *, const char *);
int address_error(struct envelope *, char *, char *, char *);
void expansion_stab_stdout(void *);
int add_address(struct expand *, char *, struct envelope *, int, char *);
struct envelope *address_bounce_create(struct expand *);
int address_expand(struct expand *);
void expand_tree_stdout(struct exp_addr *, int);
int address_string_recipients(
struct expand *, char *, struct exp_addr *, char *);
struct envelope * address_bounce_create(struct expand *);
simta_address_status address_expand(struct expand *);
void expand_tree_stdout(struct exp_addr *, int);
int address_string_recipients(
struct expand *, char *, struct exp_addr *, char *);

#ifdef HAVE_LDAP
int exp_addr_link(struct exp_link **, struct exp_addr *);
Expand Down
Loading

0 comments on commit e36ee31

Please sign in to comment.