- /* send our request */
- if (send(spamd_sock, spamd_buffer, Ustrlen(spamd_buffer), 0) < 0) {
- close(spamd_sock);
- log_write(0, LOG_MAIN|LOG_PANIC,
- "spam acl condition: spamd send failed: %s", strerror(errno));
- fclose(mbox_file);
- close(spamd_sock);
- return DEFER;
- };
-
- /* now send the file */
- /* spamd sometimes accepts conections but doesn't read data off
- * the connection. We make the file descriptor non-blocking so
- * that the write will only write sufficient data without blocking
- * and we poll the desciptor to make sure that we can write without
- * blocking. Short writes are gracefully handled and if the whole
- * trasaction takes too long it is aborted.
- * Note: poll() is not supported in OSX 10.2.
- */
-#ifndef NO_POLL_H
- pollfd.fd = spamd_sock;
- pollfd.events = POLLOUT;
-#endif
- fcntl(spamd_sock, F_SETFL, O_NONBLOCK);
- do {
- read = fread(spamd_buffer,1,sizeof(spamd_buffer),mbox_file);
- if (read > 0) {
- offset = 0;
-again:
-#ifndef NO_POLL_H
- result = poll(&pollfd, 1, 1000);
- if (result == -1 && errno == EINTR)
- continue;
- else if (result < 1) {
- if (result == -1)
- log_write(0, LOG_MAIN|LOG_PANIC,
- "spam acl condition: %s on spamd socket", strerror(errno));
- else {
- if (time(NULL) - start < SPAMD_TIMEOUT)
- goto again;
- log_write(0, LOG_MAIN|LOG_PANIC,
- "spam acl condition: timed out writing spamd socket");
- }
- close(spamd_sock);
- fclose(mbox_file);
- return DEFER;
- }
-#endif
- wrote = send(spamd_sock,spamd_buffer + offset,read - offset,0);
- if (wrote == -1)
+if (Ustrncmp(param, "tmo=", 4) == 0)
+ {
+ int sec = readconf_readtime((s = param+4), '\0', FALSE);
+ name = US"timeout";
+ if (sec < 0)
+ goto badval;
+ spamd->timeout = sec;
+ return 0;
+ }
+
+if (Ustrncmp(param, "retry=", 6) == 0)
+ {
+ int sec = readconf_readtime((s = param+6), '\0', FALSE);
+ name = US"retry";
+ if (sec < 0)
+ goto badval;
+ spamd->retry = sec;
+ return 0;
+ }
+
+log_write(0, LOG_MAIN, "%s warning - invalid spamd parameter: '%s'",
+ loglabel, param);
+return -1; /* syntax error */
+
+badval:
+ log_write(0, LOG_MAIN,
+ "%s warning - invalid spamd %s value: '%s'", loglabel, name, s);
+ return -1; /* syntax error */
+}
+
+
+static int
+spamd_get_server(spamd_address_container ** spamds, int num_servers)
+{
+unsigned int i;
+spamd_address_container * sd;
+long weights;
+unsigned pri;
+
+/* speedup, if we have only 1 server */
+if (num_servers == 1)
+ return (spamds[0]->is_failed ? -1 : 0);
+
+/* scan for highest pri */
+for (pri = 0, i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority > pri) pri = sd->priority;
+ }
+
+/* get sum of weights */
+for (weights = 0, i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority == pri) weights += sd->weight;
+ }
+if (weights == 0) /* all servers failed */
+ return -1;
+
+for (long rnd = random_number(weights), i = 0; i < num_servers; i++)
+ {
+ sd = spamds[i];
+ if (!sd->is_failed && sd->priority == pri)
+ if ((rnd -= sd->weight) < 0)
+ return i;
+ }
+
+log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s unknown error (memory/cpu corruption?)", loglabel);
+return -1;
+}
+
+
+int
+spam(const uschar **listptr)
+{
+int sep = 0;
+const uschar *list = *listptr;
+uschar *user_name;
+unsigned long mbox_size;
+FILE *mbox_file;
+client_conn_ctx spamd_cctx = {.sock = -1};
+uschar spamd_buffer[32600];
+int i, j, offset, result;
+uschar spamd_version[8];
+uschar spamd_short_result[8];
+uschar spamd_score_char;
+double spamd_threshold, spamd_score, spamd_reject_score;
+int spamd_report_offset;
+uschar *p,*q;
+int override = 0;
+time_t start;
+size_t read, wrote;
+uschar *spamd_address_work;
+spamd_address_container * sd;
+
+/* stop compiler warning */
+result = 0;
+
+/* find the username from the option list */
+if (!(user_name = string_nextinlist(&list, &sep, NULL, 0)))
+ {
+ /* no username given, this means no scanning should be done */
+ return FAIL;
+ }
+
+/* if username is "0" or "false", do not scan */
+if (Ustrcmp(user_name, "0") == 0 || strcmpic(user_name, US"false") == 0)
+ return FAIL;
+
+/* if there is an additional option, check if it is "true" */
+if (strcmpic(list,US"true") == 0)
+ /* in that case, always return true later */
+ override = 1;
+
+/* expand spamd_address if needed */
+if (*spamd_address != '$')
+ spamd_address_work = spamd_address;
+else if (!(spamd_address_work = expand_string(spamd_address)))
+ {
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s spamd_address starts with $, but expansion failed: %s",
+ loglabel, expand_string_message);
+ return DEFER;
+ }
+
+DEBUG(D_acl) debug_printf_indent("spamd: addrlist '%s'\n", spamd_address_work);
+
+/* check if previous spamd_address was expanded and has changed. dump cached results if so */
+if ( spam_ok
+ && prev_spamd_address_work != NULL
+ && Ustrcmp(prev_spamd_address_work, spamd_address_work) != 0
+ )
+ spam_ok = 0;
+
+/* if we scanned for this username last time, just return */
+if (spam_ok && Ustrcmp(prev_user_name, user_name) == 0)
+ return override ? OK : spam_rc;
+
+/* make sure the eml mbox file is spooled up */
+
+if (!(mbox_file = spool_mbox(&mbox_size, NULL, NULL)))
+ { /* error while spooling */
+ log_write(0, LOG_MAIN|LOG_PANIC,
+ "%s error while creating mbox spool file", loglabel);
+ return DEFER;
+ }
+
+start = time(NULL);
+
+ {
+ int num_servers = 0;
+ int current_server;
+ uschar * address;
+ const uschar * spamd_address_list_ptr = spamd_address_work;
+ spamd_address_container * spamd_address_vector[32];
+
+ /* Check how many spamd servers we have
+ and register their addresses */
+ sep = 0; /* default colon-sep */
+ while ((address = string_nextinlist(&spamd_address_list_ptr, &sep, NULL, 0)))
+ {
+ const uschar * sublist;
+ int sublist_sep = -(int)' '; /* default space-sep */
+ unsigned args;
+ uschar * s;
+
+ DEBUG(D_acl) debug_printf_indent("spamd: addr entry '%s'\n", address);
+ sd = store_get(sizeof(spamd_address_container), GET_UNTAINTED);
+
+ for (sublist = address, args = 0, spamd_param_init(sd);
+ (s = string_nextinlist(&sublist, &sublist_sep, NULL, 0));
+ args++
+ )