1 Index: spamass-milter.cpp
2 ===================================================================
3 RCS file: /cvsroot/spamass-milt/spamass-milt/spamass-milter.cpp,v
4 retrieving revision 1.91
5 diff -u -r1.91 spamass-milter.cpp
6 --- spamass-milter.cpp 24 Jul 2006 19:59:17 -0000 1.91
7 +++ spamass-milter.cpp 10 Mar 2010 18:52:22 -0000
9 bool flag_expand = false; /* alias/virtusertable expansion */
10 bool warnedmacro = false; /* have we logged that we couldn't fetch a macro? */
12 -#if defined(__FreeBSD__) /* popen bug - see PR bin/50770 */
13 -static pthread_mutex_t popen_mutex = PTHREAD_MUTEX_INITIALIZER;
20 send another copy. The milter API will not let you send the
21 message AND return a failure code to the sender, so this is
22 the only way to do it. */
23 -#if defined(__FreeBSD__)
27 -#if defined(HAVE_ASPRINTF)
32 - char *fmt="%s \"%s\"";
33 + char *popen_argv[3];
36 -#if defined(HAVE_ASPRINTF)
37 - asprintf(&buf, fmt, SENDMAIL, spambucket);
39 -#if defined(HAVE_SNPRINTF)
40 - snprintf(buf, sizeof(buf)-1, fmt, SENDMAIL, spambucket);
42 - /* XXX possible buffer overflow here */
43 - sprintf(buf, fmt, SENDMAIL, spambucket);
47 - debug(D_COPY, "calling %s", buf);
48 -#if defined(__FreeBSD__) /* popen bug - see PR bin/50770 */
49 - rv = pthread_mutex_lock(&popen_mutex);
52 - debug(D_ALWAYS, "Could not lock popen mutex: %s", strerror(rv));
56 - p = popen(buf, "w");
57 + popen_argv[0] = SENDMAIL;
58 + popen_argv[1] = spambucket;
59 + popen_argv[2] = NULL;
61 + debug(D_COPY, "calling %s %s", SENDMAIL, spambucket);
62 + p = popenv(popen_argv, "w");
65 - debug(D_COPY, "popen failed(%s). Will not send a copy to spambucket", strerror(errno));
66 + debug(D_COPY, "popenv failed(%s). Will not send a copy to spambucket", strerror(errno));
69 // Send message provided by SpamAssassin
70 fwrite(assassin->d().c_str(), assassin->d().size(), 1, p);
71 - pclose(p); p = NULL;
72 + fclose(p); p = NULL;
74 -#if defined(__FreeBSD__)
75 - rv = pthread_mutex_unlock(&popen_mutex);
78 - debug(D_ALWAYS, "Could not unlock popen mutex: %s", strerror(rv));
82 -#if defined(HAVE_ASPRINTF)
89 /* open a pipe to sendmail so we can do address expansion */
92 - char *fmt="%s -bv \"%s\" 2>&1";
94 -#if defined(HAVE_SNPRINTF)
95 - snprintf(buf, sizeof(buf)-1, fmt, SENDMAIL, envrcpt[0]);
97 - /* XXX possible buffer overflow here */
98 - sprintf(buf, fmt, SENDMAIL, envrcpt[0]);
100 + char *popen_argv[4];
102 + popen_argv[0] = SENDMAIL;
103 + popen_argv[1] = "-bv";
104 + popen_argv[2] = envrcpt[0];
105 + popen_argv[3] = NULL;
107 - debug(D_RCPT, "calling %s", buf);
108 + debug(D_RCPT, "calling %s -bv %s", SENDMAIL, envrcpt[0]);
110 -#if defined(__FreeBSD__) /* popen bug - see PR bin/50770 */
111 - rv = pthread_mutex_lock(&popen_mutex);
114 - debug(D_ALWAYS, "Could not lock popen mutex: %s", strerror(rv));
119 - p = popen(buf, "r");
120 + p = popenv(popen_argv, "r");
123 - debug(D_RCPT, "popen failed(%s). Will not expand aliases", strerror(errno));
124 + debug(D_RCPT, "popenv failed(%s). Will not expand aliases", strerror(errno));
125 assassin->expandedrcpt.push_back(envrcpt[0]);
129 assassin->expandedrcpt.push_back(p+7);
132 - pclose(p); p = NULL;
133 + fclose(p); p = NULL;
135 -#if defined(__FreeBSD__)
136 - rv = pthread_mutex_unlock(&popen_mutex);
139 - debug(D_ALWAYS, "Could not unlock popen mutex: %s", strerror(rv));
145 assassin->expandedrcpt.push_back(envrcpt[0]);
146 @@ -2157,5 +2099,71 @@
151 + untrusted-argument-safe popen function - only supports "r" and "w" modes
152 + for simplicity, and always reads stdout and stderr in "r" mode. Call
153 + fclose to close the FILE.
155 +FILE *popenv(char *const argv[], const char *type)
160 + if ((*type != 'r' && *type != 'w') || type[1])
165 + if (pipe(pdes) < 0)
169 + case -1: /* Error. */
170 + save_errno = errno;
171 + (void)close(pdes[0]);
172 + (void)close(pdes[1]);
173 + errno = save_errno;
176 + case 0: /* Child. */
177 + if (*type == 'r') {
179 + * The dup2() to STDIN_FILENO is repeated to avoid
180 + * writing to pdes[1], which might corrupt the
181 + * parent's copy. This isn't good enough in
182 + * general, since the exit() is no return, so
183 + * the compiler is free to corrupt all the local
186 + (void)close(pdes[0]);
187 + (void)dup2(pdes[1], STDOUT_FILENO);
188 + (void)dup2(pdes[1], STDERR_FILENO);
189 + if (pdes[1] != STDOUT_FILENO && pdes[1] != STDERR_FILENO) {
190 + (void)close(pdes[1]);
193 + if (pdes[0] != STDIN_FILENO) {
194 + (void)dup2(pdes[0], STDIN_FILENO);
195 + (void)close(pdes[0]);
197 + (void)close(pdes[1]);
199 + execv(argv[0], argv);
204 + /* Parent; assume fdopen can't fail. */
205 + if (*type == 'r') {
206 + iop = fdopen(pdes[0], type);
207 + (void)close(pdes[1]);
209 + iop = fdopen(pdes[1], type);
210 + (void)close(pdes[0]);
217 // vim6:ai:noexpandtab
218 Index: spamass-milter.h
219 ===================================================================
220 RCS file: /cvsroot/spamass-milt/spamass-milt/spamass-milter.h,v
221 retrieving revision 1.23
222 diff -u -r1.23 spamass-milter.h
223 --- spamass-milter.h 7 Apr 2005 02:04:24 -0000 1.23
224 +++ spamass-milter.h 10 Mar 2010 18:52:22 -0000
226 void parse_debuglevel(char* string);
227 char *strlwr(char *str);
228 void warnmacro(char *macro, char *scope);
229 +FILE *popenv(char *const argv[], const char *type);