File: ftp-main.c

package info (click to toggle)
proxy-suite 1.9.2.4-10
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 1,988 kB
  • ctags: 519
  • sloc: ansic: 6,534; sh: 2,968; makefile: 279
file content (417 lines) | stat: -rw-r--r-- 9,915 bytes parent folder | download | duplicates (6)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * $Id: ftp-main.c,v 1.6.6.1 2003/05/07 11:08:55 mt Exp $
 *
 * Main program of the FTP Proxy
 *
 * Author(s): Jens-Gero Boehm <jens-gero.boehm@suse.de>
 *            Pieter Hollants <pieter.hollants@suse.de>
 *            Marius Tomaschewski <mt@suse.de>
 *            Volker Wiegand <volker.wiegand@suse.de>
 *
 * This file is part of the SuSE Proxy Suite
 *            See also  http://proxy-suite.suse.de/
 *
 * 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 of the License, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * A history log can be found at the end of this file.
 */

#ifndef lint
static char rcsid[] = "$Id: ftp-main.c,v 1.6.6.1 2003/05/07 11:08:55 mt Exp $";
#endif

#include <config.h>

#if defined(STDC_HEADERS)
#  include <stdio.h>
#  include <string.h>
#  include <stdlib.h>
#  include <stdarg.h>
#  include <errno.h>
#endif

#if defined(HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#if defined(TIME_WITH_SYS_TIME)
#  include <sys/time.h>
#  include <time.h>
#else
#  if defined(HAVE_SYS_TIME_H)
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
#endif

#include <signal.h>

#include "com-config.h"
#include "com-debug.h"
#include "com-misc.h"
#include "com-socket.h"
#include "com-syslog.h"
#include "ftp-client.h"
#include "ftp-daemon.h"
#include "ftp-main.h"


/* ------------------------------------------------------------ */

#include "ftp-vers.c"		/* We need the version strings	*/

#define SELECT_TIMEOUT		60	/* Wake up regularly	*/

#  define DEBUG_FILE		"/tmp/ftp-proxy.debug"
#if defined(COMPILE_DEBUG)
#  define OPTS_LIST		"cdinf:v:V?"
#else
#  define OPTS_LIST		"cdinf:V?"
#endif


/* ------------------------------------------------------------ */

static char progname[1024];

static char *usage_arr[] = {
	progname,
	"    -c          Dump Config-File contents and exit",
	"    -d          Forced to run in standalone mode",
	"    -i          Forced to run in inetd mode",
	"    -n          Do not detach from controlling terminal",
	"    -f file     Name of the configuration file",
	"                  (Default: " DEFAULT_CONFIG ")",
	"    -v level    Send debuging output to " DEBUG_FILE,
	"                  (Level: 0 = silence, 4 = chatterbox)",
#if defined(COMPILE_DEBUG)
	"                  !!! DO NOT USE -v FOR PRODUCTION !!!",
#else
	"                  !!! DISABLED AT COMPILE TIME FOR PRODUCTION USE !!!",
#endif
	"    -V          Display program version and exit",
	"",
	NULL
};


/* ------------------------------------------------------------ */

static RETSIGTYPE main_signal(int signo);

static char *cfg_file  = 0;	/* Name of the config file	*/
static int close_flag  = 0;	/* Program termination request	*/
static int config_flag = 0;	/* Config refresh request	*/
static int rotate_flag = 0;	/* Log file rotation request	*/


/* ------------------------------------------------------------ */

#define ST_NONE		0	/* Unknown ServerType		*/
#define ST_INETD	1	/* Run from (x)inetd		*/
#define ST_DAEMON	2	/* Run as daemon		*/

static int srv_type;		/* The actual server type	*/


/* ------------------------------------------------------------ **
**
**	Function......:	config_filename
**
**	Parameters....:	(none)
**
**	Return........:	name of current config file
**
**	Purpose.......: read-only access to config file name
**
** ------------------------------------------------------------ */

const char* config_filename()
{
	return cfg_file;
}


/* ------------------------------------------------------------ **
**
**	Function......:	main_signal
**
**	Parameters....:	signo		Signal to be handled
**
**	Return........:	(most probably, none)
**
**	Purpose.......: Handler for signals, mainly killing.
**
** ------------------------------------------------------------ */

static RETSIGTYPE main_signal(int signo)
{
#if defined(COMPILE_DEBUG)
	debug(2, "server signal %d", signo);
#endif

	switch (signo) {
		case SIGHUP:
			config_flag = 1;
			break;
		case SIGUSR1:
			rotate_flag = 1;
			break;
		default:
			close_flag  = 1;
	}

	signal(signo, main_signal);
#if RETSIGTYPE != void
	return 0;
#endif
}


/* ------------------------------------------------------------ **
**
**	Function......:	main
**
**	Parameters....:	argc & argv	As usual
**
**	Return........:	EXIT_SUCCESS or EXIT_FAILURE
**
**	Purpose.......: Yes.
**
** ------------------------------------------------------------ */

int main(int argc, char *argv[])
{
	int c, detach, cfg_dump;
	char *p;

#if defined(SIGWINCH)
	/*
	** Make sure we don't get confused by resizing windows
	*/
	signal(SIGWINCH, SIG_IGN);
#endif

	/*
	** Set a reasonable program name for debug/logging
	*/
	p = misc_setprog(argv[0], usage_arr);
#if defined(HAVE_SNPRINTF)
	snprintf(progname, sizeof(progname),
	                  "Usage: %s [option ...]", p);
#else
	sprintf(progname, "Usage: %s [option ...]", p);
#endif
	misc_setvers(prog_vers);
	misc_setdate(prog_date);

	/*
	** Preset some variables
	*/
	cfg_file = DEFAULT_CONFIG;
	cfg_dump = 0;
	srv_type = ST_NONE;	/* Undetermined yet		*/
	detach   = 1;		/* Usually detach from CtlTerm	*/

	/*
	** Read the command line options
	*/
	while ((c = getopt(argc, argv, OPTS_LIST)) != EOF) {
		switch (c) {
		case 'c':
			cfg_dump = 1;		/* Dump config	*/
			break;
		case 'd':
			srv_type = ST_DAEMON;	/* Force daemon	*/
			break;
		case 'i':
			srv_type = ST_INETD;	/* Force inetd	*/
			break;
		case 'n':
			detach = 0;	/* Don't detach (e.g. AIX) */
			break;
		case 'f':
			cfg_file = misc_strtrim(optarg);
			break;
#if defined(COMPILE_DEBUG)
		case 'v':
			debug_init(atoi(misc_strtrim(optarg)), DEBUG_FILE);
			break;
#endif
		case 'V':
			fprintf(stderr, "%s\n", misc_getvsdt());
			exit(EXIT_SUCCESS);
			break;
		case '?':
		default:
			misc_usage(NULL);
		}
	}

	/*
	** Redirect errors and faults to stderr durring the
	** initialisation phase. The log file will be opened
	** after the chroot is done and uid/gid are dropped
	** (if they are requested....)
	*/
	syslog_stderr();

	/*
	** Read the configuration file (this will die on error)
	*/
	config_read(cfg_file, cfg_dump);

	/*
	** Complain if no default DestinationAddress is given
	** while the AllowTransProxy feature is disabled...
	**
	** FIXME: is this really needed?
	*/
	if( (NULL == config_str(NULL, "DestinationAddress", NULL)) &&
	    (0    == config_bool(NULL, "AllowTransProxy", 0))      &&
            (0    == config_bool(NULL, "AllowMagicUser", 0)))
	{
		syslog_error("can't run without an destination address");
		exit(EXIT_FAILURE);
	}

	/*
	** Determine ServerType (inetd/standalone)
	*/
	if (srv_type == ST_NONE) {
		p = config_str(NULL, "ServerType", "inetd");
		if (strcasecmp(p, "standalone") == 0)
			srv_type = ST_DAEMON;
		else
			srv_type = ST_INETD;
	}

	if (srv_type == ST_INETD) {
#if defined(COMPILE_DEBUG)
		debug(1, "{{{{{ %s client-start", misc_getprog());
#endif
		/*
		** Change root directory
		*/
		misc_chroot(config_str(NULL, "ServerRoot", NULL));

		/*
		** Change our user- and group-id if requested
		*/
		misc_uidgid(CONFIG_UID, CONFIG_GID);

		/*
		** Open the log if requested
		*/
		if ((p = config_str(NULL, "LogDestination", NULL)) != NULL)
			syslog_open(p, config_str(NULL, "LogLevel", NULL));
		else	syslog_close();

		client_run();
		exit(EXIT_SUCCESS);
	}

#if defined(COMPILE_DEBUG)
	debug(1, "{{{{{ %s daemon-start", misc_getprog());
#endif

	/*
	** The rest of this file is "daemon only" code ...
	*/
	daemon_init(detach);

	/*
	** Setup signal handling (mostly graceful exit)
	*/
	signal(SIGINT,  main_signal);
	signal(SIGTERM, main_signal);
	signal(SIGQUIT, main_signal);
	signal(SIGHUP,  main_signal);
	signal(SIGUSR1, main_signal);

	/*
	** Well, it's time for the main loop now ...
	*/
	while (close_flag == 0) {
		/*
		** Shall we re-read the config file?
		*/
		if (config_flag) {

			/*
			** reread config
			*/
			config_flag = 0;
			config_read(cfg_file, 0);

			/*
			** reopen / rotate log
			*/
			syslog_open(config_str(NULL,
			                       "LogDestination", NULL),
			            config_str(NULL, "LogLevel", NULL));
		}

		/*
		** Check for log file rotation
		*/
		if (rotate_flag) {
			rotate_flag = 0;
			syslog_rotate();
		}

		/*
		** Now perform the "real" main loop work
		*/
		socket_exec(SELECT_TIMEOUT, &close_flag);
	}

#if defined(COMPILE_DEBUG)
	debug(1, "}}}}} %s daemon-exit", misc_getprog());
#endif
	exit(EXIT_SUCCESS);
}


/* ------------------------------------------------------------
 * $Log: ftp-main.c,v $
 * Revision 1.6.6.1  2003/05/07 11:08:55  mt
 * added strtrim arround optarg
 *
 * Revision 1.6  2002/01/14 19:24:46  mt
 * reordered config reading, chroot and syslog opening
 * added config_filename function for runtime queries
 * added snprintf usage if supported on built-platform
 *
 * Revision 1.5  2001/11/06 23:04:44  mt
 * applied / merged with transparent proxy patches v8
 * see ftp-proxy/NEWS for more detailed release news
 *
 * Revision 1.4  1999/09/29 09:58:25  wiegand
 * complain about missing DestAddr after LogOpen (incl. stderr)
 *
 * Revision 1.3  1999/09/21 07:13:07  wiegand
 * syslog / abort cleanup and review
 *
 * Revision 1.2  1999/09/16 16:29:57  wiegand
 * minor updates improving code quality
 *
 * Revision 1.1  1999/09/15 14:06:22  wiegand
 * initial checkin
 *
 * ------------------------------------------------------------ */