Postfix3.3.1
cleanup_map11.c
[詳解]
1 /*++
2 /* NAME
3 /* cleanup_map11 3
4 /* SUMMARY
5 /* one-to-one mapping
6 /* SYNOPSIS
7 /* #include <cleanup.h>
8 /*
9 /* int cleanup_map11_external(state, addr, maps, propagate)
10 /* CLEANUP_STATE *state;
11 /* VSTRING *addr;
12 /* MAPS *maps;
13 /* int propagate;
14 /*
15 /* int cleanup_map11_internal(state, addr, maps, propagate)
16 /* CLEANUP_STATE *state;
17 /* VSTRING *addr;
18 /* MAPS *maps;
19 /* int propagate;
20 /*
21 /* int cleanup_map11_tree(state, tree, maps, propagate)
22 /* CLEANUP_STATE *state;
23 /* TOK822 *tree;
24 /* MAPS *maps;
25 /* int propagate;
26 /* DESCRIPTION
27 /* This module performs one-to-one map lookups.
28 /*
29 /* If an address has a mapping, the lookup result is
30 /* subjected to another iteration of rewriting and mapping.
31 /* Recursion continues until an address maps onto itself,
32 /* or until an unreasonable recursion level is reached.
33 /* An unmatched address extension is propagated when
34 /* \fIpropagate\fR is non-zero.
35 /* These functions return non-zero when the address was changed.
36 /*
37 /* cleanup_map11_external() looks up the external (quoted) string
38 /* form of an address in the maps specified via the \fImaps\fR argument.
39 /*
40 /* cleanup_map11_internal() is a wrapper around the
41 /* cleanup_map11_external() routine that transforms from
42 /* internal (quoted) string form to external form and back.
43 /*
44 /* cleanup_map11_tree() is a wrapper around the
45 /* cleanup_map11_external() routine that transforms from
46 /* internal parse tree form to external form and back.
47 /* DIAGNOSTICS
48 /* Recoverable errors: the global \fIcleanup_errs\fR flag is updated.
49 /* SEE ALSO
50 /* mail_addr_find(3) address lookups
51 /* mail_addr_map(3) address mappings
52 /* LICENSE
53 /* .ad
54 /* .fi
55 /* The Secure Mailer license must be distributed with this software.
56 /* AUTHOR(S)
57 /* Wietse Venema
58 /* IBM T.J. Watson Research
59 /* P.O. Box 704
60 /* Yorktown Heights, NY 10598, USA
61 /*
62 /* Wietse Venema
63 /* Google, Inc.
64 /* 111 8th Avenue
65 /* New York, NY 10011, USA
66 /*--*/
67 
68 /* System library. */
69 
70 #include <sys_defs.h>
71 #include <string.h>
72 
73 /* Utility library. */
74 
75 #include <msg.h>
76 #include <vstring.h>
77 #include <dict.h>
78 #include <mymalloc.h>
79 #include <stringops.h>
80 
81 /* Global library. */
82 
83 #include <cleanup_user.h>
84 #include <mail_addr_map.h>
85 #include <quote_822_local.h>
86 
87 /* Application-specific. */
88 
89 #include "cleanup.h"
90 
91 #define STR vstring_str
92 #define MAX_RECURSION 10
93 
94 /* cleanup_map11_external - one-to-one table lookups */
95 
97  MAPS *maps, int propagate)
98 {
99  int count;
100  int expand_to_self;
101  ARGV *new_addr;
102  char *saved_addr;
103  int did_rewrite = 0;
104 
105  /*
106  * Produce sensible output even in the face of a recoverable error. This
107  * simplifies error recovery considerably because we can do delayed error
108  * checking in one place, instead of having error handling code all over
109  * the place.
110  */
111  for (count = 0; count < MAX_RECURSION; count++) {
112  if ((new_addr = mail_addr_map_opt(maps, STR(addr), propagate,
114  MA_FORM_EXTERNAL)) != 0) {
115  if (new_addr->argc > 1)
116  msg_warn("%s: multi-valued %s entry for %s",
117  state->queue_id, maps->title, STR(addr));
118  saved_addr = mystrdup(STR(addr));
119  did_rewrite |= strcmp(new_addr->argv[0], STR(addr));
120  vstring_strcpy(addr, new_addr->argv[0]);
121  expand_to_self = !strcasecmp_utf8(saved_addr, STR(addr));
122  myfree(saved_addr);
123  argv_free(new_addr);
124  if (expand_to_self)
125  return (did_rewrite);
126  } else if (maps->error != 0) {
127  msg_warn("%s: %s map lookup problem for %s -- "
128  "message not accepted, try again later",
129  state->queue_id, maps->title, STR(addr));
130  state->errs |= CLEANUP_STAT_WRITE;
131  return (did_rewrite);
132  } else {
133  return (did_rewrite);
134  }
135  }
136  msg_warn("%s: unreasonable %s map nesting for %s -- "
137  "message not accepted, try again later",
138  state->queue_id, maps->title, STR(addr));
139  return (did_rewrite);
140 }
141 
142 /* cleanup_map11_tree - rewrite address node */
143 
145  MAPS *maps, int propagate)
146 {
147  VSTRING *temp = vstring_alloc(100);
148  int did_rewrite;
149 
150  /*
151  * Produce sensible output even in the face of a recoverable error. This
152  * simplifies error recovery considerably because we can do delayed error
153  * checking in one place, instead of having error handling code all over
154  * the place.
155  */
157  did_rewrite = cleanup_map11_external(state, temp, maps, propagate);
158  tok822_free_tree(tree->head);
159  tree->head = tok822_scan(STR(temp), &tree->tail);
160  vstring_free(temp);
161  return (did_rewrite);
162 }
163 
164 /* cleanup_map11_internal - rewrite address internal form */
165 
167  MAPS *maps, int propagate)
168 {
169  VSTRING *temp = vstring_alloc(100);
170  int did_rewrite;
171 
172  /*
173  * Produce sensible output even in the face of a recoverable error. This
174  * simplifies error recovery considerably because we can do delayed error
175  * checking in one place, instead of having error handling code all over
176  * the place.
177  */
178  quote_822_local(temp, STR(addr));
179  did_rewrite = cleanup_map11_external(state, temp, maps, propagate);
180  unquote_822_local(addr, STR(temp));
181  vstring_free(temp);
182  return (did_rewrite);
183 }
void myfree(void *ptr)
Definition: mymalloc.c:207
#define STR
Definition: cleanup_map11.c:91
char * mystrdup(const char *str)
Definition: mymalloc.c:225
ARGV * argv_free(ARGV *argvp)
Definition: argv.c:136
Definition: argv.h:17
#define tok822_scan(cp, ptr)
Definition: tok822.h:83
#define MAX_RECURSION
Definition: cleanup_map11.c:92
Definition: tok822.h:27
Definition: maps.h:22
char ** argv
Definition: argv.h:20
VSTRING * unquote_822_local(VSTRING *dst, const char *mbox)
#define strcasecmp_utf8(s1, s2)
Definition: stringops.h:75
VSTRING * vstring_strcpy(VSTRING *vp, const char *src)
Definition: vstring.c:431
TOK822 * tok822_free_tree(TOK822 *)
Definition: tok822_tree.c:262
struct TOK822 * head
Definition: tok822.h:32
char * title
Definition: maps.h:23
void msg_warn(const char *fmt,...)
Definition: msg.c:215
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
VSTRING * tok822_externalize(VSTRING *, TOK822 *, int)
Definition: tok822_parse.c:270
char * queue_id
Definition: cleanup.h:56
int cleanup_map11_tree(CLEANUP_STATE *state, TOK822 *tree, MAPS *maps, int propagate)
int error
Definition: maps.h:25
#define TOK822_STR_DEFL
Definition: tok822.h:91
#define quote_822_local(dst, src)
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
#define CLEANUP_STAT_WRITE
Definition: cleanup_user.h:58
ssize_t argc
Definition: argv.h:19
int cleanup_map11_internal(CLEANUP_STATE *state, VSTRING *addr, MAPS *maps, int propagate)
ARGV * mail_addr_map_opt(MAPS *path, const char *address, int propagate, int in_form, int query_form, int out_form)
#define MA_FORM_EXTERNAL
struct TOK822 * tail
Definition: tok822.h:33
int cleanup_map11_external(CLEANUP_STATE *state, VSTRING *addr, MAPS *maps, int propagate)
Definition: cleanup_map11.c:96