Debian Codegraph @

src/ui_unix.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 NOEXPORT int main_unix(int, char*[]);
41 #if !defined(__vms) && !defined(USE_OS2)
42 NOEXPORT int daemonize(int);
43 NOEXPORT int create_pid(void);
44 NOEXPORT void delete_pid(void);
45 #endif
46 #ifndef USE_OS2
47 NOEXPORT void signal_handler(int);
48 #endif
49
50 int main(int argc, char* argv[]) { /* execution begins here 8-) */
51 int retval;
52
53 #ifdef M_MMAP_THRESHOLD
54 mallopt(M_MMAP_THRESHOLD, 4096);
55 #endif
56 retval=stunnel_init();
57 if(retval)
58 return retval;
59 retval=main_unix(argc, argv);
60 main_cleanup();
61 return retval;
62 }
63
64 NOEXPORT int main_unix(int argc, char* argv[]) {
65 int configure_status;
66
67 #if !defined(__vms) && !defined(USE_OS2)
68 int fd;
69
70 fd=open("/dev/null", O_RDWR); /* open /dev/null before chroot */
71 if(fd==INVALID_SOCKET)
72 fatal("Could not open /dev/null");
73 #endif
74 main_init();
75 configure_status=main_configure(argc>1 ? argv[1] : NULL,
76 argc>2 ? argv[2] : NULL);
77 switch(configure_status) {
78 case 1: /* error -> exit with 1 to indicate error */
79 close(fd);
80 return 1;
81 case 2: /* information printed -> exit with 0 to indicate success */
82 close(fd);
83 return 0;
84 }
85 if(service_options.next) { /* there are service sections -> daemon mode */
86 #if !defined(__vms) && !defined(USE_OS2)
87 if(daemonize(fd)) {
88 close(fd);
89 return 1;
90 }
91 close(fd);
92 /* create_pid() must be called after drop_privileges()
93 * or it won't be possible to remove the file on exit */
94 /* create_pid() must be called after daemonize()
95 * since the final pid is not known beforehand */
96 if(create_pid())
97 return 1;
98 #endif
99 #ifndef USE_OS2
100 signal(SIGCHLD, signal_handler); /* handle dead children */
101 signal(SIGHUP, signal_handler); /* configuration reload */
102 signal(SIGUSR1, signal_handler); /* log reopen */
103 signal(SIGUSR2, signal_handler); /* connections */
104 signal(SIGPIPE, SIG_IGN); /* ignore broken pipe */
105 if(signal(SIGTERM, SIG_IGN)!=SIG_IGN)
106 signal(SIGTERM, signal_handler); /* fatal */
107 if(signal(SIGQUIT, SIG_IGN)!=SIG_IGN)
108 signal(SIGQUIT, signal_handler); /* fatal */
109 if(signal(SIGINT, SIG_IGN)!=SIG_IGN)
110 signal(SIGINT, signal_handler); /* fatal */
111 #endif
112 #ifdef USE_FORK
113 setpgid(0, 0); /* create a new process group if needed */
114 #endif
115 daemon_loop();
116 #ifdef USE_FORK
117 s_log(LOG_NOTICE, "Terminating service processes");
118 signal(SIGCHLD, SIG_IGN);
119 signal(SIGTERM, SIG_IGN);
120 kill(0, SIGTERM); /* kill the whole process group */
121 while(wait(NULL)!=-1)
122 ;
123 s_log(LOG_NOTICE, "Service processes terminated");
124 #endif
125 #if !defined(__vms) && !defined(USE_OS2)
126 delete_pid();
127 #endif /* standard Unix */
128 } else { /* inetd mode */
129 CLI *c;
130 #if !defined(__vms) && !defined(USE_OS2)
131 close(fd);
132 #endif /* standard Unix */
133 #ifndef USE_OS2
134 signal(SIGCHLD, SIG_IGN); /* ignore dead children */
135 signal(SIGPIPE, SIG_IGN); /* ignore broken pipe */
136 #endif
137 set_nonblock(0, 1); /* stdin */
138 set_nonblock(1, 1); /* stdout */
139 c=alloc_client_session(&service_options, 0, 1);
140 tls_alloc(c, ui_tls, NULL);
141 service_up_ref(&service_options);
142 client_main(c);
143 client_free(c);
144 }
145 return 0;
146 }
147
148 #ifndef USE_OS2
149 NOEXPORT void signal_handler(int sig) {
150 int saved_errno;
151
152 saved_errno=errno;
153 signal_post((uint8_t)sig);
154 signal(sig, signal_handler);
155 errno=saved_errno;
156 }
157 #endif
158
159 #if !defined(__vms) && !defined(USE_OS2)
160
161 NOEXPORT int daemonize(int fd) { /* go to background */
162 if(global_options.option.foreground)
163 return 0;
164 dup2(fd, 0);
165 dup2(fd, 1);
166 dup2(fd, 2);
167 #if defined(HAVE_DAEMON) && !defined(__BEOS__)
168 /* set noclose option when calling daemon() function,
169 * so it does not require /dev/null device in the chrooted directory */
170 if(daemon(0, 1)==-1) {
171 ioerror("daemon");
172 return 1;
173 }
174 #else
175 chdir("/");
176 switch(fork()) {
177 case -1: /* fork failed */
178 ioerror("fork");
179 return 1;
180 case 0: /* child */
181 break;
182 default: /* parent */
183 exit(0);
184 }
185 #endif
186 tls_alloc(NULL, ui_tls, "main"); /* reuse thread-local storage */
187 #ifdef HAVE_SETSID
188 setsid(); /* ignore the error */
189 #endif
190 return 0;
191 }
192
193 NOEXPORT int create_pid(void) {
194 int pf;
195 char *pid;
196
197 if(!global_options.pidfile) {
198 s_log(LOG_DEBUG, "No pid file being created");
199 return 0;
200 }
201
202 /* silently remove the old pid file */
203 unlink(global_options.pidfile);
204
205 /* create a new pid file */
206 pf=open(global_options.pidfile, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644);
207 if(pf==-1) {
208 s_log(LOG_ERR, "Cannot create pid file %s", global_options.pidfile);
209 ioerror("create");
210 return 1;
211 }
212 pid=str_printf("%lu\n", (unsigned long)getpid());
213 if(write(pf, pid, strlen(pid))<(int)strlen(pid)) {
214 s_log(LOG_ERR, "Cannot write pid file %s", global_options.pidfile);
215 ioerror("write");
216 return 1;
217 }
218 str_free(pid);
219 close(pf);
220 s_log(LOG_DEBUG, "Created pid file %s", global_options.pidfile);
221 return 0;
222 }
223
224 NOEXPORT void delete_pid(void) {
225 if(global_options.pidfile) {
226 if(unlink(global_options.pidfile)<0)
227 ioerror(global_options.pidfile); /* not critical */
228 else
229 s_log(LOG_DEBUG, "Removed pid file %s", global_options.pidfile);
230 } else {
231 s_log(LOG_DEBUG, "No pid file to remove");
232 }
233 }
234
235 #endif /* standard Unix */
236
237 /**************************************** options callbacks */
238
239 void ui_config_reloaded(void) {
240 /* no action */
241 }
242
243 #ifdef ICON_IMAGE
244
245 ICON_IMAGE load_icon_default(ICON_TYPE icon) {
246 (void)icon; /* squash the unused parameter warning */
247 return (ICON_IMAGE)0;
248 }
249
250 ICON_IMAGE load_icon_file(const char *file) {
251 (void)file; /* squash the unused parameter warning */
252 return (ICON_IMAGE)0;
253 }
254
255 #endif
256
257 /**************************************** client callbacks */
258
259 void ui_new_chain(const unsigned section_number) {
260 (void)section_number; /* squash the unused parameter warning */
261 }
262
263 void ui_clients(const long num) {
264 (void)num; /* squash the unused parameter warning */
265 }
266
267 /**************************************** s_log callbacks */
268
269 void ui_new_log(const char *line) {
270 fprintf(stderr, "%s\n", line);
271 }
272
273 /**************************************** ctx callbacks */
274
275 int ui_passwd_cb(char *buf, int size, int rwflag, void *userdata) {
276 return PEM_def_callback(buf, size, rwflag, userdata);
277 }
278
279 #if !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L
280
281 int (*ui_get_opener(void)) (UI *) {
282 return UI_method_get_opener(UI_OpenSSL());
283 }
284
285 int (*ui_get_writer(void)) (UI *, UI_STRING *) {
286 return UI_method_get_writer(UI_OpenSSL());
287 }
288
289 int (*ui_get_reader(void)) (UI *, UI_STRING *) {
290 return UI_method_get_reader(UI_OpenSSL());
291 }
292
293 int (*ui_get_closer(void)) (UI *) {
294 return UI_method_get_closer(UI_OpenSSL());
295 }
296
297 #endif /* !defined(OPENSSL_NO_ENGINE) || OPENSSL_VERSION_NUMBER>=0x10101000L */
298
299 /* end of ui_unix.c */