File: conf.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (379 lines) | stat: -rw-r--r-- 11,057 bytes parent folder | download | duplicates (4)
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
/*
 MySecureShell permit to add restriction to modified sftp-server
 when using MySecureShell as shell.
 Copyright (C) 2007-2014 MySecureShell Team

 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 (version 2)

 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.
 */

#include "../config.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "conf.h"
#include "FileSpec.h"
#include "ip.h"
#include "parsing.h"
#include "string.h"
#include "security.h"
#include "user.h"

#define CONF_IS_EMPTY				0
#define CONF_IS_STRING				1
#define CONF_IS_STRING_MAYBE_EMPTY	2
#define CONF_IS_PATH_RESOLVE_ENV	3
#define CONF_IS_INT					4
#define CONF_IS_BOOLEAN				5
#define CONF_IS_SPEED				6
#define CONF_IS_MODE				7
#define CONF_IS_TIME				8
#define CONF_IS_FILE_AND_DIR		9
#define CONF_DEPRECATED				10

#define CONF_SHOW			0
#define CONF_SHOW_ALWAYS	1
#define CONF_NOT_SHOW		2

typedef struct sConf
{
	char *name;
	int type;
	int show;
} tConf;

static const tConf confParams[] =
{
	{ "GlobalDownload", CONF_IS_SPEED, CONF_SHOW },
	{ "GlobalUpload", CONF_IS_SPEED, CONF_SHOW },
	{ "Download", CONF_IS_SPEED, CONF_SHOW },
	{ "Upload", CONF_IS_SPEED, CONF_SHOW },
	{ "StayAtHome", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "VirtualChroot", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "LimitConnection", CONF_IS_INT, CONF_SHOW },
	{ "LimitConnectionByUser", CONF_IS_INT, CONF_SHOW },
	{ "LimitConnectionByIP", CONF_IS_INT, CONF_SHOW },
	{ "Home", CONF_IS_PATH_RESOLVE_ENV, CONF_SHOW },
	{ "Shell", CONF_IS_STRING, CONF_SHOW },
	{ "ResolveIP", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "IdleTimeOut", CONF_IS_TIME, CONF_SHOW },
	{ "IgnoreHidden", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DirFakeUser", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DirFakeGroup", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DirFakeMode", CONF_IS_MODE, CONF_SHOW },
	{ "HideNoAccess", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "ByPassGlobalDownload", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "ByPassGlobalUpload", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "MaxOpenFilesForUser", CONF_IS_INT, CONF_SHOW },
	{ "MaxReadFilesForUser", CONF_IS_INT, CONF_SHOW },
	{ "MaxWriteFilesForUser", CONF_IS_INT, CONF_SHOW },
	{ "ShowLinksAsLinks", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "SftpProtocol", CONF_IS_INT, CONF_SHOW_ALWAYS },
	{ "LogFile", CONF_IS_STRING, CONF_SHOW_ALWAYS },
	{ "LogSyslog", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "ConnectionMaxLife", CONF_IS_TIME, CONF_SHOW },
	{ "DisableAccount", CONF_IS_BOOLEAN, CONF_SHOW },
#ifdef MSS_HAVE_ADMIN
	{ "IsAdmin", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "IsSimpleAdmin", CONF_IS_BOOLEAN, CONF_SHOW },
#endif
	{ "DisableRemoveDir", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableRemoveFile", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableReadFile", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableReadDir", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableWriteFile", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableSetAttribute", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableMakeDir", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableRename", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableSymLink", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableOverwrite", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DisableStatsFs", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "Charset", CONF_IS_STRING, CONF_SHOW },
	{ "CanChangeRights", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "CanChangeTime", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "ExpireDate", CONF_IS_STRING_MAYBE_EMPTY, CONF_SHOW },
	{ "ForceUser", CONF_IS_STRING, CONF_SHOW },
	{ "ForceGroup", CONF_IS_STRING, CONF_SHOW },
	{ "CreateHome", CONF_IS_BOOLEAN, CONF_SHOW },
	{ "DefaultRights", CONF_IS_FILE_AND_DIR, CONF_SHOW },
	{ "MinimumRights", CONF_IS_FILE_AND_DIR, CONF_SHOW },
	{ "MaximumRights", CONF_IS_FILE_AND_DIR, CONF_SHOW },
	{ "ForceRights", CONF_IS_FILE_AND_DIR, CONF_SHOW },
	{ "ApplyFileSpec", CONF_IS_STRING, CONF_SHOW_ALWAYS },

	{ "CanRemoveDir", CONF_DEPRECATED, CONF_SHOW },
	{ "CanRemoveFile", CONF_DEPRECATED, CONF_SHOW },
	{ "GMTTime", CONF_DEPRECATED, CONF_NOT_SHOW },
	{ "HideFiles", CONF_DEPRECATED, CONF_SHOW },
	{ "PathAllowFilter", CONF_DEPRECATED, CONF_SHOW },
	{ "PathDenyFilter", CONF_DEPRECATED, CONF_SHOW },
	{ "{last item}", CONF_IS_EMPTY, CONF_NOT_SHOW }
};

void load_config(int verbose)
{
	if (init_user_info() == 0)
	{
		(void) fprintf(stderr, "[ERROR]Error when fetching user information\n");
		exit(2);
	}
	hash_set_int("SERVER_PORT", get_port_server());
	hash_set("SERVER_IP", get_ip_server());
	hash_set_int("CanChangeRights", 1);
	hash_set_int("CanChangeTime", 1);
	if (load_config_file(CONFIG_FILE, verbose, 10) == 0)
		if (load_config_file(CONFIG_FILE2, verbose, 10) == 0)
		{
			(void) fprintf(stderr,
					"[ERROR]No valid config file were found. Please correct this.\n");
			exit(2);
		}
	free_user_info();
	if (verbose > 0)
	{
		size_t maxLen;
		char bTmp[256];
		int i, r;

		(void) printf("--- %s ---\n", (char *) hash_get("User"));
		for (i = 0, maxLen = 0; confParams[i].type != CONF_IS_EMPTY; i++)
		{
			size_t len = strlen(confParams[i].name);

			if (len > maxLen)
				maxLen = len;
		}
		for (i = 0; confParams[i].type != CONF_IS_EMPTY; i++)
		{
			size_t j;
			char *ptr;
			int vInt;

			if (confParams[i].show != CONF_SHOW_ALWAYS && hash_exists(
					confParams[i].name) == 0)
				continue;
			(void) printf("%s", confParams[i].name);
			for (j = maxLen - strlen(confParams[i].name) + 1; j > 0; j--)
				(void) printf(" ");
			(void) printf("= ");
			switch (confParams[i].type)
			{
			case CONF_IS_STRING:
			case CONF_IS_PATH_RESOLVE_ENV:
				ptr = (char *) hash_get(confParams[i].name);
				if (ptr == NULL && confParams[i].show == CONF_SHOW_ALWAYS)
					(void) printf("{default}");
				else
					(void) printf("%s", ptr);
				break;
			case CONF_IS_STRING_MAYBE_EMPTY:
				ptr = (char *) hash_get(confParams[i].name);
				(void) printf("%s", ptr != NULL ? ptr : "{nothing}");
				break;
			case CONF_IS_INT:
				vInt = hash_get_int(confParams[i].name);
				if (vInt == 0 && confParams[i].show == CONF_SHOW_ALWAYS)
					(void) printf("{default}");
				else
					(void) printf("%i", vInt);
				break;
			case CONF_IS_BOOLEAN:
				(void) printf("%s",
						hash_get_int(confParams[i].name) == 0 ? "false" : "true");
				break;
			case CONF_IS_SPEED:
				(void) printf("%i bytes/s", hash_get_int(confParams[i].name));
				break;
			case CONF_IS_MODE:
				vInt = hash_get_int(confParams[i].name);
				if (vInt == 0)
					(void) printf("{default}");
				else
					(void) printf("%i", vInt);
				break;
			case CONF_IS_TIME:
				(void) printf("%is", hash_get_int(confParams[i].name));
				break;
			case CONF_IS_FILE_AND_DIR:
				(void) snprintf(bTmp, sizeof(bTmp), "%sFile", confParams[i].name);
				r = hash_get_int(bTmp);
				(void) printf("%i%i%i%i", r / (8 * 8 * 8), (r / (8 * 8)) % 8,
						(r / 8) % 8, r % 8);
				(void) snprintf(bTmp, sizeof(bTmp), "%sDirectory", confParams[i].name);
				r = hash_get_int(bTmp);
				if (r > 0)
				{
					(void) printf(" %i%i%i%i", r / (8 * 8 * 8), (r / (8 * 8)) % 8, (r / 8) % 8, r % 8);
				}
				break;
			case CONF_DEPRECATED:
				(void) printf("%s is deprecated and unused", confParams[i].name);
				break;
			}
			(void) printf("\n");
		}
	}
}

int load_config_file(const char *file, int verbose, int max_recursive_left)
{
	size_t len;
	FILE *fh;
	char buffer[1024];
	char **tb, *str;
	int line, processTag;
	int openedTag = 0;

	if (max_recursive_left == 0)
	{
		(void) fprintf(stderr, "[ERROR]Too much inclusions !!!\n");
		return (0);
	}
	processTag = 1;
	if ((fh = fopen(file, "r")))
	{
		if (verbose > 1)
			(void) printf("- Parse config file: %s -\n", file);
		line = 0;
		while (fgets(buffer, (int) sizeof(buffer), fh))
		{
			line++;
			if ((str = clean_buffer(buffer)))
			{
				len = strlen(str) - 1;
				if (*str == '<')
				{
					if (str[len] == '>')
					{
						openedTag += TagParse(str);
						if (openedTag < 0)
						{
							(void) fprintf(
									stderr,
									"[ERROR]Too much tag closed at line %i in file '%s'!\n",
									line, file);
							exit(2);
						}
					}
					else
					{
						(void) fprintf(
								stderr,
								"[ERROR]Error parsing line %i is not valid in file '%s'!\n",
								line, file);
						exit(2);
					}
					processTag = TagIsActive(verbose);
				}
				else if (processTag == 0)
					continue;
				else if ((tb = ParseCutString(str)))
				{
					if (tb[0] != NULL)
					{
						if (TagIsOpen(VTAG_FILESPEC) == 1)
							FileSpecParse(tb);
						else
							processLine(tb, max_recursive_left, verbose);
					}
					free(tb);
				}
			}
		}
		if (openedTag != 0)
		{
			(void) fprintf(stderr,
					"[ERROR]Missing %i close(s) tag(s) in file '%s'!!!\n",
					openedTag, file);
			exit(2);
		}
		xfclose(fh);
	}
	else
	{
		(void) fprintf(stderr,
				"[ERROR]Couldn't load config file '%s'. Error : %s\n", file,
				strerror(errno));
		return (0);
	}
	return (1);
}

void processLine(char **tb, int max_recursive_left, int verbose)
{
	char bTmp[256];
	int notRecognized;
	int i;

	notRecognized = 1;
	for (i = 0; confParams[i].type != CONF_IS_EMPTY; i++)
		if (strcmp(tb[0], confParams[i].name) == 0 && (tb[1] != NULL
				|| confParams[i].type == CONF_IS_STRING_MAYBE_EMPTY))
		{
			notRecognized = 0;
			switch (confParams[i].type)
			{
			case CONF_IS_STRING:
				hash_set(tb[0], (void *) strdup(tb[1]));
				break;
			case CONF_IS_STRING_MAYBE_EMPTY:
				hash_set(tb[0], (void *) (tb[1] ? strdup(tb[1]) : 0));
				break;
			case CONF_IS_PATH_RESOLVE_ENV:
			{
				char *path = convert_str_with_resolv_env_to_str(tb[1]);

				if (path != NULL)
					hash_set(tb[0], (void *) convert_to_path(path));
			}
				break;
			case CONF_IS_INT:
				hash_set_int(tb[0], atoi(tb[1]));
				break;
			case CONF_IS_BOOLEAN:
				hash_set_int(tb[0], convert_boolean_to_int(tb[1]));
				break;
			case CONF_IS_SPEED:
				hash_set_int(tb[0], convert_speed_to_int(tb + 1));
				break;
			case CONF_IS_MODE:
				hash_set_int(tb[0], convert_mode_to_int(tb[1]));
				break;
			case CONF_IS_TIME:
				hash_set_int(tb[0], convert_time_to_int(tb + 1));
				break;
			case CONF_IS_FILE_AND_DIR:
				hash_set_int(tb[0], 42);
				(void) snprintf(bTmp, sizeof(bTmp), "%sFile", tb[0]);
				hash_set_int(bTmp, convert_mode_to_int(tb[1]));
				if (tb[2] != NULL)
				{
					(void) snprintf(bTmp, sizeof(bTmp), "%sDirectory", tb[0]);
					hash_set_int(bTmp, convert_mode_to_int(tb[2]));
				}
				break;
			}
			break;
		}
	if (notRecognized == 1)
	{
		if (strcmp(tb[0], "Include") == 0 && tb[1] != NULL)
		{
			notRecognized = 0;
			(void) load_config_file(tb[1], verbose, max_recursive_left - 1);
		}
		if (notRecognized == 1)
			(void) fprintf(stderr, "Property '%s' is not recognized !\n", tb[0]);
	}
}