summaryrefslogtreecommitdiffhomepage
path: root/src/tinyproxy.c
blob: e0363d185efd8c7038587dc345f6ee0d916e4ece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* $Id: tinyproxy.c,v 1.8 2000-12-08 03:35:07 rjkaes Exp $
 *
 * The initialise routine. Basically sets up all the initial stuff (logfile,
 * listening socket, config options, etc.) and then sits there and loops
 * over the new connections until the daemon is closed. Also has additional
 * functions to handle the "user friendly" aspects of a program (usage,
 * stats, etc.) Like any good program, most of the work is actually done
 * elsewhere.
 *
 * Copyright (C) 1998  Steven Young
 * Copyright (C) 1999  Robert James Kaes (rjkaes@flarenet.com)
 * Copyright (C) 2000  Chris Lightfoot (chris@ex-parrot.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include "tinyproxy.h"

#include <grp.h>
#include <pwd.h>
#include <signal.h>
#include <sysexits.h>
#include <syslog.h>

#include "anonymous.h"
#include "buffer.h"
#include "dnscache.h"
#include "filter.h"
#include "log.h"
#include "reqs.h"
#include "sock.h"
#include "stats.h"
#include "thread.h"
#include "utils.h"

void takesig(int sig);

extern int yyparse(void);
extern FILE *yyin;

/* 
 * Global Structures
 */
struct config_s config;
float load = 0.00;

/*
 * Handle a signal
 */
void takesig(int sig)
{
	switch (sig) {
	case SIGHUP:
		if (config.logf)
			ftruncate(fileno(config.logf), 0);

		log(LOG_NOTICE, "SIGHUP received, cleaning up...");

#ifdef FILTER_ENABLE
		if (config.filter) {
			filter_destroy();
			filter_init();
		}
		log(LOG_NOTICE, "Re-reading filter file.");
#endif				/* FILTER_ENABLE */
		log(LOG_NOTICE, "Finished cleaning memory/connections.");		       
		break;
	case SIGTERM:
#ifdef FILTER_ENABLE
		if (config.filter)
			filter_destroy();
#endif				/* FILTER_ENABLE */
		config.quit = TRUE;
		log(LOG_INFO, "SIGTERM received.");
		break;
	}

	if (sig != SIGTERM)
		signal(sig, takesig);
	signal(SIGPIPE, SIG_IGN);
}

/*
 * Display the version information for the user.
 */
static void versiondisp(void)
{
	printf("tinyproxy " VERSION "\n");
	printf("\
Copyright 1998       Steven Young (sdyoung@well.com)\n\
Copyright 1998-2000  Robert James Kaes (rjkaes@flarenet.com)\n\
Copyright 2000       Chris Lightfoot (chris@ex-parrot.com)\n\
\n\
This program is free software; you can redistribute it and/or modify it\n\
under the terms of the GNU General Public License as published by the Free\n\
Software Foundation; either version 2, or (at your option) any later\n\
version.\n\
\n\
This program is distributed in the hope that it will be useful, but WITHOUT\n\
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n\
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\n\
more details.\n");
}

/*
 * Display usage to the user.
 */
static void usagedisp(void)
{
	printf("\
Usage: tinyproxy [options]\n\
Options:\n\
  -d		Operate in DEBUG mode.\n\
  -c FILE	Use an alternate configuration file.\n\
  -h		Display this usage information.\n\
  -v            Display the version number.\n");


	/* Display the modes compiled into tinyproxy */
	printf("\nFeatures Compiled In:\n");
#ifdef XTINYPROXY_ENABLE
	printf("    XTinyproxy Header\n");
#endif				/* XTINYPROXY */
#ifdef FILTER_ENABLE
	printf("    Filtering\n");
	printf("        * with Regular Expression support\n");
#endif				/* FILTER_ENABLE */
#ifndef NDEBUG
	printf("    Debugging code\n");
#endif				/* NDEBUG */
#ifdef TUNNEL_SUPPORT
	printf("    TCP Tunneling\n");
#endif				/* TUNNEL_SUPPORT */
}

int main(int argc, char **argv)
{
	int optch;
	bool_t godaemon = TRUE;
	struct passwd *thisuser = NULL;
	struct group *thisgroup = NULL;
	char *conf_file = DEFAULT_CONF_FILE;

	while ((optch = getopt(argc, argv, "c:vdh")) !=
	       EOF) {
		switch (optch) {
		case 'v':
			versiondisp();
			exit(EX_OK);
		case 'd':
			godaemon = FALSE;
			break;
		case 'c':
			conf_file = strdup(optarg);
			if (!conf_file) {
				log(LOG_EMERG, "tinyproxy: could not allocate memory");
				exit(EX_SOFTWARE);
			}
			break;
		case 'h':
		default:
			usagedisp();
			exit(EX_OK);
		}
	}

	/*
	 * Read in the settings from the config file.
	 */
	yyin = fopen(conf_file, "r");
	if (!yyin) {
		log(LOG_ERR, "Could not open %s file", conf_file);
		exit(EX_SOFTWARE);
	}
	yyparse();

	/* Open the log file if not using syslog */
	if (config.syslog == FALSE) {
		if (!config.logf_name) {
			log(LOG_INFO, "Setting Logfile to \"%s\"", DEFAULT_LOG);
			config.logf_name = DEFAULT_LOG;
		}

		if (!(config.logf = fopen(config.logf_name, "a"))) {
			fprintf(stderr,
				"Unable to open logfile %s for appending!\n",
				config.logf_name);
			exit(EX_CANTCREAT);
		}
	} else {
		if (godaemon == TRUE)
			openlog("tinyproxy", LOG_PID, LOG_DAEMON);
		else
			openlog("tinyproxy", LOG_PID, LOG_USER);
	}

	log(LOG_INFO, PACKAGE " " VERSION " starting...");

	/*
	 * Set the default values if they were not set in the config file.
	 */
	if (config.port == 0) {
		log(LOG_INFO, "Setting Port to %u", DEFAULT_PORT);
		config.port = DEFAULT_PORT;
	}
	if (!config.stathost) {
		log(LOG_INFO, "Setting stathost to \"%s\"", DEFAULT_STATHOST);
		config.stathost = DEFAULT_STATHOST;
	}
	if (!config.username) {
		log(LOG_INFO, "Setting user to \"%s\"", DEFAULT_USER);
		config.username = DEFAULT_USER;
	}
	if (config.idletimeout == 0) {
		log(LOG_INFO, "Setting idle timeout to %u seconds", MAX_IDLE_TIME);
		config.idletimeout = MAX_IDLE_TIME;
	}

	init_stats();

	/*
	 * If ANONYMOUS is turned on, make sure that Content-Length is
	 * in the list of allowed headers, since it is required in a
	 * HTTP/1.0 request. Also add the Content-Type header since it goes
	 * hand in hand with Content-Length.
	 *     - rjkaes
	 */
	if (config.anonymous) {
		anon_insert("Content-Length:");
		anon_insert("Content-Type:");
	}

	if (godaemon == TRUE)
		makedaemon();

	if (config.pidpath) {
		pidfile_create(config.pidpath);
	}

	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
		log(LOG_CRIT, "Could not set SIGPIPE\n");
		exit(EX_OSERR);
	}

#ifdef FILTER_ENABLE
	if (config.filter)
		filter_init();
#endif				/* FILTER_ENABLE */

	/*
	 * Start listening on the selected port.
	 */
	if (thread_listening_sock(config.port) < 0) {
		log(LOG_CRIT, "Problem creating listening socket");
		exit(EX_OSERR);
	}

	/*
	 * Switch to a different user.
	 */
	if (geteuid() == 0) {
		if (config.group && strlen(config.group) > 0) {
			thisgroup = getgrnam(config.group);
			if (!thisgroup) {
				log(LOG_ERR, "Unable to find group '%s'!", config.group);
				exit(EX_NOUSER);
			}
			if (setgid(thisgroup->gr_gid) < 0) {
				log(LOG_ERR, "Unable to change to group '%s'", config.group);
				exit(EX_CANTCREAT);
			}
			log(LOG_INFO, "Now running as group %s", config.group);
		}
		if (config.username && strlen(config.username) > 0) {
			thisuser = getpwnam(config.username);
			if (!thisuser) {
				log(LOG_ERR, "Unable to find user '%s'!", config.username);
				exit(EX_NOUSER);
			}
			if (setuid(thisuser->pw_uid) < 0) {
				log(LOG_ERR, "Unable to change to user '%s'", config.username);
				exit(EX_CANTCREAT);
			}
			log(LOG_INFO, "Now running as user %s", config.username);
		}
	} else {
		log(LOG_WARNING, "Not running as root, so not changing UID/GID.");
	}

	if (thread_pool_create() < 0) {
		log(LOG_ERR, "Could not create the pool of threads");
		exit(EX_SOFTWARE);
	}

	/*
	 * These signals are only for the main thread.
	 */
	log(LOG_INFO, "Setting the various signals.");
	if (signal(SIGTERM, takesig) == SIG_ERR) {
		log(LOG_CRIT, "Could not set SIGTERM\n");
		exit(EX_OSERR);
	}
	if (signal(SIGHUP, takesig) == SIG_ERR) {
		log(LOG_CRIT, "Could not set SIGHUP\n");
		exit(EX_OSERR);
	}

	/*
	 * Initialize the various subsystems...
	 */
	log(LOG_INFO, "Starting the DNS caching subsystem.");
	if (!new_dnscache())
		exit(EX_SOFTWARE);
	log(LOG_INFO, "Starting the Anonymous header subsystem.");
	if (!new_anonymous())
		exit(EX_SOFTWARE);

	/*
	 * Start the main loop.
	 */
	log(LOG_INFO, "Starting main loop. Accepting connections.");
	do {
		thread_main_loop();
		sleep(1);
	} while (!config.quit);

	log(LOG_INFO, "Shutting down.");
	thread_close_sock();

	/*
	 * Remove the PID file.
	 */
	if (unlink(config.pidpath) < 0) {
		log(LOG_WARNING, "Could not remove PID file %s: %s",
		    config.pidpath, strerror(errno));
	}

#ifdef FILTER_ENABLE
	if (config.filter)
		filter_destroy();
#endif				/* FILTER_ENABLE */

	if (config.syslog == FALSE)
		fclose(config.logf);
	else
		closelog();

	exit(EX_OK);
}