masqmail

annotate src/online.c @ 156:ee2afbf92428

require host_name to be set in config file exit otherwise there is no portable way to determine the hostname (actually the hostname that masqmail should use) thus it must be set by the administrator
author meillo@marmaro.de
date Thu, 08 Jul 2010 09:49:05 +0200
parents 8a92de5e8907
children 5b621742b2e7
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@76 3 Copyright (C) 2008 markus schnalke <meillo@marmaro.de>
meillo@0 4
meillo@0 5 This program is free software; you can redistribute it and/or modify
meillo@0 6 it under the terms of the GNU General Public License as published by
meillo@0 7 the Free Software Foundation; either version 2 of the License, or
meillo@0 8 (at your option) any later version.
meillo@0 9
meillo@0 10 This program is distributed in the hope that it will be useful,
meillo@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 13 GNU General Public License for more details.
meillo@0 14
meillo@0 15 You should have received a copy of the GNU General Public License
meillo@0 16 along with this program; if not, write to the Free Software
meillo@0 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 18 */
meillo@0 19
meillo@0 20 #include <sys/stat.h>
meillo@0 21 #include <sys/wait.h>
meillo@15 22
meillo@0 23 #include "masqmail.h"
meillo@0 24 #include "mserver.h"
meillo@0 25 #include "peopen.h"
meillo@0 26
meillo@0 27 gchar *connection_name;
meillo@0 28
meillo@10 29 void
meillo@10 30 set_online_name(gchar * name)
meillo@0 31 {
meillo@10 32 connection_name = g_strdup(name);
meillo@0 33 }
meillo@0 34
meillo@10 35 static gchar*
meillo@10 36 detect_online_pipe(const gchar * pipe)
meillo@0 37 {
meillo@10 38 pid_t pid;
meillo@10 39 void (*old_signal) (int);
meillo@10 40 int status;
meillo@10 41 FILE *in;
meillo@10 42 gchar *name = NULL;
meillo@0 43
meillo@10 44 old_signal = signal(SIGCHLD, SIG_DFL);
meillo@0 45
meillo@10 46 in = peopen(pipe, "r", environ, &pid);
meillo@33 47 if (in == NULL) {
meillo@33 48 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
meillo@33 49 signal(SIGCHLD, old_signal);
meillo@33 50 return NULL;
meillo@33 51 }
meillo@33 52
meillo@33 53 gchar output[256];
meillo@33 54 if (fgets(output, 255, in)) {
meillo@33 55 g_strchomp(g_strchug(output));
meillo@33 56 if (strlen(output) == 0) {
meillo@33 57 logwrite(LOG_ALERT, "only whitespace connection name\n");
meillo@33 58 name = NULL;
meillo@18 59 } else {
meillo@33 60 name = g_strdup(output);
meillo@10 61 }
meillo@33 62 } else {
meillo@33 63 logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
meillo@33 64 name = NULL;
meillo@33 65 }
meillo@33 66 fclose(in);
meillo@33 67 waitpid(pid, &status, 0);
meillo@33 68 if (WEXITSTATUS(status) != EXIT_SUCCESS) {
meillo@33 69 g_free(name);
meillo@33 70 name = NULL;
meillo@33 71 }
meillo@0 72
meillo@10 73 signal(SIGCHLD, old_signal);
meillo@0 74
meillo@10 75 return name;
meillo@0 76 }
meillo@0 77
meillo@10 78 gchar*
meillo@10 79 detect_online()
meillo@0 80 {
meillo@33 81 if (conf.online_detect == NULL) {
meillo@33 82 return NULL;
meillo@33 83 }
meillo@33 84
meillo@33 85 if (strcmp(conf.online_detect, "file") == 0) {
meillo@33 86 DEBUG(3) debugf("online detection method 'file'\n");
meillo@51 87 if (conf.online_file == NULL) {
meillo@33 88 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
meillo@33 89 return NULL;
meillo@33 90 }
meillo@33 91
meillo@33 92 struct stat st;
meillo@33 93 if (stat(conf.online_file, &st) == 0) {
meillo@33 94 FILE *fptr = fopen(conf.online_file, "r");
meillo@33 95 if (!fptr) {
meillo@33 96 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
meillo@10 97 return NULL;
meillo@10 98 }
meillo@33 99 char buf[256];
meillo@33 100 if (fgets(buf, 256, fptr) == NULL) {
meillo@33 101 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
meillo@33 102 fclose(fptr);
meillo@33 103 return NULL;
meillo@33 104 }
meillo@33 105 g_strchomp(g_strchug(buf));
meillo@33 106 fclose(fptr);
meillo@33 107 if (strlen(buf) == 0) {
meillo@33 108 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
meillo@33 109 return NULL;
meillo@33 110 }
meillo@33 111 return g_strdup(buf);
meillo@33 112 } else if (errno == ENOENT) {
meillo@33 113 logwrite(LOG_NOTICE, "not online.\n");
meillo@33 114 return NULL;
meillo@10 115 } else {
meillo@33 116 logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
meillo@33 117 return NULL;
meillo@10 118 }
meillo@33 119
meillo@33 120 #ifdef ENABLE_MSERVER
meillo@33 121 } else if (strcmp(conf.online_detect, "mserver") == 0) {
meillo@33 122 DEBUG(3) debugf("connection method 'mserver'\n");
meillo@33 123 return mserver_detect_online(conf.mserver_iface);
meillo@33 124 #endif
meillo@33 125 } else if (strcmp(conf.online_detect, "pipe") == 0) {
meillo@33 126 DEBUG(3) debugf("connection method 'pipe'\n");
meillo@33 127 if (conf.online_pipe)
meillo@33 128 return detect_online_pipe(conf.online_pipe);
meillo@33 129 else {
meillo@33 130 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
meillo@33 131 return NULL;
meillo@33 132 }
meillo@33 133 } else if (strcmp(conf.online_detect, "argument") == 0) {
meillo@33 134 return connection_name;
meillo@33 135 } else {
meillo@33 136 DEBUG(3) debugf("no connection method selected\n");
meillo@0 137 }
meillo@33 138
meillo@0 139 return NULL;
meillo@0 140 }