comparison src/online.c @ 310:f10a56dc7481

reworked online_detect to the simpler online_query Only pipe is supported now. Use online_query="/bin/cat /path/to/file" instead of online_detect=file online_file=/path/to/file and online_query="/path/to/some/script foo" instead of online_detect=pipe online_pipe="/path/to/some/script foo" See man page masqmail.conf(5) and admin/config-transition.
author meillo@marmaro.de
date Sun, 24 Apr 2011 19:14:38 +0200
parents 1aa107c6b1e5
children 41958685480d
comparison
equal deleted inserted replaced
309:273f6c9eb6a2 310:f10a56dc7481
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */ 18 */
19 19
20 #include <sys/stat.h>
21 #include <sys/wait.h> 20 #include <sys/wait.h>
22 21
23 #include "masqmail.h" 22 #include "masqmail.h"
24 #include "peopen.h" 23 #include "peopen.h"
25 24
26 gchar *connection_name;
27 25
28 void 26 gchar*
29 set_online_name(gchar * name) 27 online_query()
30 { 28 {
31 connection_name = g_strdup(name); 29 gchar* pipe = conf.online_query;
32 }
33
34 static gchar*
35 detect_online_file(const gchar* file)
36 {
37 struct stat st;
38 int err;
39 FILE *fptr;
40 char buf[256];
41
42 err = stat(conf.online_file, &st);
43
44 if (err) {
45 if (errno==ENOENT) {
46 logwrite(LOG_NOTICE, "not online.\n");
47 return NULL;
48 }
49 logwrite(LOG_ALERT, "stat of %s failed: %s\n", conf.online_file, strerror(errno));
50 return NULL;
51 }
52
53 fptr = fopen(conf.online_file, "r");
54 if (!fptr) {
55 logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
56 return NULL;
57 }
58 if (fgets(buf, 256, fptr) == NULL) {
59 logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
60 fclose(fptr);
61 return NULL;
62 }
63 g_strstrip(buf); /* strip whitespace */
64 fclose(fptr);
65 if (strlen(buf) == 0) {
66 logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
67 return NULL;
68 }
69 return g_strdup(buf);
70 }
71
72 static gchar*
73 detect_online_pipe(const gchar * pipe)
74 {
75 pid_t pid; 30 pid_t pid;
76 void (*old_signal) (int); 31 void (*old_signal) (int);
77 int status; 32 int status;
78 FILE *in; 33 FILE *in;
79 gchar *name = NULL; 34 gchar *name = NULL;
80 35
36 if (!conf.online_query) {
37 return NULL;
38 }
39 DEBUG(3) debugf("online query `%s'\n", pipe);
40
81 old_signal = signal(SIGCHLD, SIG_DFL); 41 old_signal = signal(SIGCHLD, SIG_DFL);
82 42
83 in = peopen(pipe, "r", environ, &pid); 43 in = peopen(pipe, "r", environ, &pid);
84 if (in == NULL) { 44 if (!in) {
85 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno)); 45 logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
86 signal(SIGCHLD, old_signal); 46 signal(SIGCHLD, old_signal);
87 return NULL; 47 return NULL;
88 } 48 }
89 49
109 69
110 signal(SIGCHLD, old_signal); 70 signal(SIGCHLD, old_signal);
111 71
112 return name; 72 return name;
113 } 73 }
114
115 gchar*
116 detect_online()
117 {
118 if (!conf.online_detect) {
119 return NULL;
120 }
121
122 if (strcmp(conf.online_detect, "file") == 0) {
123 DEBUG(3) debugf("online detection method 'file'\n");
124 if (!conf.online_file) {
125 logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
126 return NULL;
127 }
128 return detect_online_file(conf.online_file);
129
130 } else if (strcmp(conf.online_detect, "pipe") == 0) {
131 DEBUG(3) debugf("connection method 'pipe'\n");
132 if (!conf.online_pipe) {
133 logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
134 return NULL;
135 }
136 return detect_online_pipe(conf.online_pipe);
137
138 } else if (strcmp(conf.online_detect, "argument") == 0) {
139 DEBUG(3) debugf("online route literally defined\n");
140 /* use the name set with set_online_name() */
141 return connection_name;
142
143 }
144
145 DEBUG(3) debugf("unknown online detection method `%s'\n", conf.online_detect);
146 return NULL;
147 }