Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/master/main.c @ 615:0d852af6842e HEAD
Master process generates DH/RSA parameters now and stores them into file
which login processes read. Added setting for regeneration interval. Some
other SSL cleanups.
Also fixed default login process path to be ../run/dovecot/login/ like
example config file said, instead of just ../run/dovecot/ which it actually
was until now.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 17 Nov 2002 11:42:07 +0200 |
parents | bc766660efc4 |
children | 90a65c017bf0 |
rev | line source |
---|---|
0 | 1 /* Copyright (C) 2002 Timo Sirainen */ |
2 | |
3 #include "common.h" | |
4 #include "ioloop.h" | |
5 #include "lib-signals.h" | |
6 #include "network.h" | |
7 | |
8 #include "auth-process.h" | |
9 #include "login-process.h" | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
10 #include "ssl-init.h" |
0 | 11 |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
12 #include <stdio.h> |
0 | 13 #include <stdlib.h> |
14 #include <unistd.h> | |
15 #include <fcntl.h> | |
16 #include <syslog.h> | |
17 #include <sys/stat.h> | |
18 #include <sys/wait.h> | |
19 | |
20 const char *process_names[PROCESS_TYPE_MAX] = { | |
21 "unknown", | |
22 "auth", | |
23 "login", | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
24 "imap", |
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
25 "ssl-param" |
0 | 26 }; |
27 | |
28 static IOLoop ioloop; | |
29 static Timeout to_children; | |
30 | |
31 HashTable *pids; | |
32 int null_fd, imap_fd, imaps_fd; | |
33 | |
34 int validate_str(const char *str, int max_len) | |
35 { | |
36 int i; | |
37 | |
38 for (i = 0; i < max_len; i++) { | |
39 if (str[i] == '\0') | |
40 return TRUE; | |
41 } | |
42 | |
43 return FALSE; | |
44 } | |
45 | |
46 void clean_child_process(void) | |
47 { | |
48 extern char **environ; | |
49 | |
50 /* remove all environment, we don't need them */ | |
51 if (environ != NULL) | |
52 *environ = NULL; | |
53 | |
54 /* set the failure log */ | |
35
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
55 if (set_log_path != NULL) { |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
56 putenv((char *) t_strconcat("IMAP_LOGFILE=", |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
57 set_log_path, NULL)); |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
58 } |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
59 if (set_log_timestamp != NULL) { |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
60 putenv((char *) t_strconcat("IMAP_LOGSTAMP=", |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
61 set_log_timestamp, NULL)); |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
62 } |
0 | 63 |
64 (void)close(null_fd); | |
65 (void)close(imap_fd); | |
66 (void)close(imaps_fd); | |
67 | |
68 /* close fds for auth/login processes */ | |
69 login_processes_cleanup(); | |
70 auth_processes_cleanup(); | |
71 | |
72 closelog(); | |
73 } | |
74 | |
75 static void sig_quit(int signo __attr_unused__) | |
76 { | |
77 io_loop_stop(ioloop); | |
78 } | |
79 | |
10
82b7de533f98
s/user_data/context/ and some s/Data/Context/
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
80 static void children_check_timeout(void *context __attr_unused__, |
0 | 81 Timeout timeout __attr_unused__) |
82 { | |
83 const char *process_type_name; | |
84 pid_t pid; | |
85 int status, process_type; | |
86 | |
87 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { | |
88 /* get the type and remove from hash */ | |
89 process_type = PID_GET_PROCESS_TYPE(pid); | |
90 PID_REMOVE_PROCESS_TYPE(pid); | |
91 | |
92 if (process_type == PROCESS_TYPE_IMAP) | |
93 imap_process_destroyed(pid); | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
94 if (process_type == PROCESS_TYPE_SSL_PARAM) |
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
95 ssl_parameter_process_destroyed(pid); |
0 | 96 |
97 /* write errors to syslog */ | |
98 process_type_name = process_names[process_type]; | |
99 if (WIFEXITED(status)) { | |
100 status = WEXITSTATUS(status); | |
101 if (status != 0) { | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
102 login_process_abormal_exit(pid); |
0 | 103 i_error("child %d (%s) returned error %d", |
104 (int)pid, process_type_name, status); | |
105 } | |
106 } else if (WIFSIGNALED(status)) { | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
107 login_process_abormal_exit(pid); |
0 | 108 i_error("child %d (%s) killed with signal %d", |
109 (int)pid, process_type_name, WTERMSIG(status)); | |
110 } | |
111 } | |
112 | |
113 if (pid == -1 && errno != EINTR && errno != ECHILD) | |
114 i_warning("waitpid() failed: %m"); | |
115 } | |
116 | |
117 static IPADDR *resolve_ip(const char *name) | |
118 { | |
119 IPADDR *ip; | |
120 int ret, ips_count; | |
121 | |
36 | 122 if (name == NULL || *name == '\0') |
0 | 123 return NULL; |
124 | |
125 ret = net_gethostbyname(name, &ip, &ips_count); | |
126 if (ret != 0) | |
36 | 127 i_fatal("Can't resolve address: %s", name); |
0 | 128 |
129 if (ips_count < 1) | |
36 | 130 i_fatal("No IPs for address: %s", name); |
0 | 131 |
132 return ip; | |
133 } | |
134 | |
135 static void open_fds(void) | |
136 { | |
137 IPADDR *imap_ip, *imaps_ip; | |
138 | |
139 imap_ip = resolve_ip(set_imap_listen); | |
140 imaps_ip = resolve_ip(set_imaps_listen); | |
141 | |
142 if (imaps_ip == NULL && set_imaps_listen == NULL) | |
143 imaps_ip = imap_ip; | |
144 | |
145 null_fd = open("/dev/null", O_RDONLY); | |
146 if (null_fd == -1) | |
147 i_fatal("Can't open /dev/null: %m"); | |
148 | |
149 imap_fd = set_imap_port == 0 ? dup(null_fd) : | |
150 net_listen(imap_ip, &set_imap_port); | |
151 if (imap_fd == -1) { | |
468 | 152 i_fatal("listen(%d) failed: %m", set_imap_port); |
0 | 153 } |
154 | |
575
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
155 #ifdef HAVE_SSL |
37
8b4c5ea7e6da
Some fixes to checking if SSL should be used.
Timo Sirainen <tss@iki.fi>
parents:
36
diff
changeset
|
156 imaps_fd = set_ssl_cert_file == NULL || *set_ssl_cert_file == '\0' || |
8b4c5ea7e6da
Some fixes to checking if SSL should be used.
Timo Sirainen <tss@iki.fi>
parents:
36
diff
changeset
|
157 set_ssl_key_file == NULL || *set_ssl_key_file == '\0' || |
0 | 158 set_imaps_port == 0 ? dup(null_fd) : |
159 net_listen(imaps_ip, &set_imaps_port); | |
575
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
160 #else |
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
161 imaps_fd = dup(null_fd); |
ba6f89be8237
Never open imaps port if we're building with SSL disabled.
Timo Sirainen <tss@iki.fi>
parents:
468
diff
changeset
|
162 #endif |
0 | 163 if (imaps_fd == -1) { |
164 i_fatal("listen(%d) failed: %m", set_imaps_port); | |
165 } | |
166 } | |
167 | |
50
d493b9cc265e
Introduced uoff_t which is the unsigned-equilevant of off_t. This was needed
Timo Sirainen <tss@iki.fi>
parents:
37
diff
changeset
|
168 static void main_init(void) |
0 | 169 { |
170 lib_init_signals(sig_quit); | |
171 | |
172 /* deny file access from everyone else except owner */ | |
173 (void)umask(0077); | |
174 | |
35
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
175 if (set_log_path == NULL) { |
0 | 176 openlog("imap-master", LOG_NDELAY, LOG_MAIL); |
177 | |
178 i_set_panic_handler(i_syslog_panic_handler); | |
179 i_set_fatal_handler(i_syslog_fatal_handler); | |
180 i_set_error_handler(i_syslog_error_handler); | |
181 i_set_warning_handler(i_syslog_warning_handler); | |
182 } else { | |
183 /* log failures into specified log file */ | |
35
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
184 i_set_failure_file(set_log_path, "imap-master"); |
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
185 i_set_failure_timestamp_format(set_log_timestamp); |
0 | 186 } |
187 | |
188 pids = hash_create(default_pool, 128, NULL, NULL); | |
189 to_children = timeout_add(100, children_check_timeout, NULL); | |
190 | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
191 ssl_init(); |
0 | 192 auth_processes_init(); |
193 login_processes_init(); | |
194 } | |
195 | |
196 static void main_deinit(void) | |
197 { | |
198 if (lib_signal_kill != 0) | |
199 i_warning("Killed with signal %d", lib_signal_kill); | |
200 | |
201 login_processes_deinit(); | |
202 auth_processes_deinit(); | |
615
0d852af6842e
Master process generates DH/RSA parameters now and stores them into file
Timo Sirainen <tss@iki.fi>
parents:
610
diff
changeset
|
203 ssl_deinit(); |
0 | 204 |
205 timeout_remove(to_children); | |
206 | |
207 (void)close(null_fd); | |
208 (void)close(imap_fd); | |
209 (void)close(imaps_fd); | |
210 | |
211 hash_destroy(pids); | |
212 closelog(); | |
213 } | |
214 | |
215 static void daemonize(void) | |
216 { | |
217 pid_t pid; | |
218 | |
219 pid = fork(); | |
220 if (pid < 0) | |
221 i_fatal("fork() failed: %m"); | |
222 | |
223 if (pid != 0) | |
224 _exit(0); | |
610 | 225 |
226 setsid(); | |
0 | 227 } |
228 | |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
229 static void print_help(void) |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
230 { |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
231 printf("Usage: imap-master [-F] [-c <config file>]\n"); |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
232 } |
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
233 |
0 | 234 int main(int argc, char *argv[]) |
235 { | |
236 /* parse arguments */ | |
237 const char *configfile = SYSCONFDIR "/" PACKAGE ".conf"; | |
238 int foreground = FALSE; | |
239 int i; | |
240 | |
241 lib_init(); | |
242 | |
243 for (i = 1; i < argc; i++) { | |
244 if (strcmp(argv[i], "-F") == 0) { | |
245 /* foreground */ | |
246 foreground = TRUE; | |
247 } else if (strcmp(argv[i], "-c") == 0) { | |
248 /* config file */ | |
249 i++; | |
250 if (i == argc) i_fatal("Missing config file argument"); | |
251 configfile = argv[i]; | |
252 } else { | |
279
49f4b0be0d87
Print usage with invalid parameters.
Timo Sirainen <tss@iki.fi>
parents:
50
diff
changeset
|
253 print_help(); |
0 | 254 i_fatal("Unknown argument: %s", argv[1]); |
255 } | |
256 } | |
257 | |
258 /* read and verify settings before forking */ | |
259 settings_read(configfile); | |
260 open_fds(); | |
261 | |
262 if (!foreground) | |
263 daemonize(); | |
264 | |
389
60040a9d243f
ioloop_create() takes now pool-parameter. io_buffer_create_mmaped() takes
Timo Sirainen <tss@iki.fi>
parents:
279
diff
changeset
|
265 ioloop = io_loop_create(system_pool); |
0 | 266 |
35
b420373f88f6
Added log_path and log_timestamp settings to config file. Removed -l command
Timo Sirainen <tss@iki.fi>
parents:
10
diff
changeset
|
267 main_init(); |
0 | 268 io_loop_run(ioloop); |
269 main_deinit(); | |
270 | |
271 io_loop_destroy(ioloop); | |
272 lib_deinit(); | |
273 | |
274 return 0; | |
275 } |