masqmail

view src/online.c @ 18:99c09ed776c1

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