view src/online.c @ 304:d5ce2ba71e7b

manual formating of Received: hdrs; changed hdr for local receival Now the Received: headers are much friendlier to read. About folding: We must fold any line at 998 chars before transfer. We should fold the lines we produce at 78 chars. That is what RFC 2821 requests. We should think about it, somewhen. The header for locally (i.e. non-SMTP) received mail is changed to the format postfix uses. This matches RFC 2821 better. The `from' clause should contain a domain or IP, not a user name. Also, the `with' clause should contain a registered standard protocol name, which ``local'' is not.
author markus schnalke <meillo@marmaro.de>
date Thu, 09 Dec 2010 18:28:11 -0300
parents 1aa107c6b1e5
children f10a56dc7481
line wrap: on
line source

/*  MasqMail
    Copyright (C) 1999-2001 Oliver Kurth
    Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <sys/stat.h>
#include <sys/wait.h>

#include "masqmail.h"
#include "peopen.h"

gchar *connection_name;

void
set_online_name(gchar * name)
{
	connection_name = g_strdup(name);
}

static gchar*
detect_online_file(const gchar* file)
{
	struct stat st;
	int err;
	FILE *fptr;
	char buf[256];

	err = stat(conf.online_file, &st);

	if (err) {
		if (errno==ENOENT) {
			logwrite(LOG_NOTICE, "not online.\n");
			return NULL;
		}
		logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
		return NULL;
	}

	fptr = fopen(conf.online_file, "r");
	if (!fptr) {
		logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
		return NULL;
	}
	if (fgets(buf, 256, fptr) == NULL) {
		logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
		fclose(fptr);
		return NULL;
	}
	g_strstrip(buf);  /* strip whitespace */
	fclose(fptr);
	if (strlen(buf) == 0) {
		logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
		return NULL;
	}
	return g_strdup(buf);
}

static gchar*
detect_online_pipe(const gchar * pipe)
{
	pid_t pid;
	void (*old_signal) (int);
	int status;
	FILE *in;
	gchar *name = NULL;

	old_signal = signal(SIGCHLD, SIG_DFL);

	in = peopen(pipe, "r", environ, &pid);
	if (in == NULL) {
		logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
		signal(SIGCHLD, old_signal);
		return NULL;
	}

	gchar output[256];
	if (fgets(output, 255, in)) {
		g_strchomp(g_strchug(output));
		if (strlen(output) == 0) {
			logwrite(LOG_ALERT, "only whitespace connection name\n");
			name = NULL;
		} else {
			name = g_strdup(output);
		}
	} else {
		logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
		name = NULL;
	}
	fclose(in);
	waitpid(pid, &status, 0);
	if (WEXITSTATUS(status) != 0) {
		g_free(name);
		name = NULL;
	}

	signal(SIGCHLD, old_signal);

	return name;
}

gchar*
detect_online()
{
	if (!conf.online_detect) {
		return NULL;
	}

	if (strcmp(conf.online_detect, "file") == 0) {
		DEBUG(3) debugf("online detection method 'file'\n");
		if (!conf.online_file) {
			logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
			return NULL;
		}
		return detect_online_file(conf.online_file);

	} else if (strcmp(conf.online_detect, "pipe") == 0) {
		DEBUG(3) debugf("connection method 'pipe'\n");
		if (!conf.online_pipe) {
			logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
			return NULL;
		}
		return detect_online_pipe(conf.online_pipe);

	} else if (strcmp(conf.online_detect, "argument") == 0) {
		DEBUG(3) debugf("online route literally defined\n");
		/* use the name set with set_online_name() */
		return connection_name;

	}

	DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
	return NULL;
}