File: SftpServer.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 (271 lines) | stat: -rw-r--r-- 7,506 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
/*
 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 <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "../Core/FileSpec.h"
#include "Access.h"
#include "Defines.h"
#include "Encoding.h"
#include "FileSystem.h"
#include "Global.h"
#include "Handle.h"
#include "Log.h"
#include "Sftp.h"
#include "GetUsersInfos.h"
#include "SftpServer.h"

tGlobal *gl_var = NULL;


static void end_sftp()
{
	if (gl_var != NULL)
	{
		if (cVersion != SSH2_ADMIN_VERSION)
		{
			CloseInfoForOpenFiles();
			mylog_printf(MYLOG_CONNECTION, "[%s][%s][%i]Quit.", gl_var->user, gl_var->ip, gl_var->portSource);
		}
		mylog_close_and_free();
		SftpWhoReleaseStruct(gl_var->who);
		if (gl_var->force_user != NULL)
		{
			free(gl_var->force_user);
			gl_var->force_user = NULL;
		}
		if (gl_var->force_group != NULL)
		{
			free(gl_var->force_group);
			gl_var->force_group = NULL;
		}
		free(gl_var->user);
		free(gl_var->ip);
		free(gl_var->home);
		free(gl_var);
		gl_var = NULL;
		setCharset(NULL);
		BufferDelete(bIn);
		BufferDelete(bOut);
		free_usersinfos();
		HandleCloseAll();
		FreeAccess();
		FileSpecDestroy();
		FSShutdown();
	}
	_exit(0);
}

static void end_sftp_by_signal(int signal)
{
	gl_var->must_shutdown = 1;
}

static void reopen_log_file(int signal)
{
	mylog_reopen();
}

void ParseConf(tGlobal *params, int sftpProtocol)
{
	gl_var = params;
	(void) atexit(end_sftp);
	(void) signal(SIGHUP, end_sftp_by_signal);
	(void) signal(SIGINT, end_sftp_by_signal);
	(void) signal(SIGTERM, end_sftp_by_signal);
	(void) signal(SIGUSR1, reopen_log_file);
	(void) signal(SIGUSR2, reopen_log_file);
	if (sftpProtocol > 0)
		cVersion = sftpProtocol;
}

void DoInitUser()
{
	t_info *pw;
	int uid, gid;

	mylog_printf(MYLOG_CONNECTION, "New client [%s] from [%s][%i]", gl_var->user, gl_var->ip, gl_var->portSource);
	umask(000);
	uid = getuid();
	if (gl_var->force_user != NULL)
	{
		if ((pw = mygetpwnam(gl_var->force_user)) != NULL)
			uid = pw->id;
		else
			mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Unable to force user: %s (user unknown)",
					gl_var->user, gl_var->ip, gl_var->portSource, gl_var->force_user);
	}
	gid = getgid();
	if (gl_var->force_group != NULL)
	{
		if ((pw = mygetgrnam(gl_var->force_group)) != NULL)
			gid = pw->id;
		else
			mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Unable to force group: %s (group unknown)",
					gl_var->user, gl_var->ip, gl_var->portSource, gl_var->force_group);
	}
	if (HAS_BIT(gl_var->flagsGlobals, SFTPWHO_CREATE_HOME)
			&& chdir(gl_var->home) == -1 && errno == ENOENT)
	{
		int mode = 0755;

		mode |= gl_var->minimum_rights_directory;
		mode &= gl_var->maximum_rights_directory;
		if (mkdir(gl_var->home, mode) == -1)
		{
			mylog_printf(MYLOG_ERROR, "[%s][%s][%i]Couldn't create to home '%s' : %s",
					gl_var->user, gl_var->ip, gl_var->portSource, gl_var->home,
					strerror(errno));
		}
		else if (chown(gl_var->home, uid, gid) == -1)
			mylog_printf(MYLOG_ERROR, "[%s][%s][%i]Couldn't chown the home '%s' : %s",
					gl_var->user, gl_var->ip, gl_var->portSource, gl_var->home,
					strerror(errno));
	}
	if (chdir(gl_var->home) == -1)
		mylog_printf(MYLOG_ERROR, "[%s][%s][%i]Couldn't go to home '%s' : %s",
				gl_var->user, gl_var->ip, gl_var->portSource, gl_var->home,
				strerror(errno));
	if (HAS_BIT(gl_var->flagsGlobals, SFTPWHO_VIRTUAL_CHROOT))
	{
		gl_var->flagsGlobals &= ~SFTPWHO_STAY_AT_HOME;
		FSInit(gl_var->home, "/");
	}
	else if (HAS_BIT(gl_var->flagsGlobals, SFTPWHO_STAY_AT_HOME))
		FSInit(gl_var->home, NULL);
	else
		FSInit(gl_var->home, NULL);
	if (gl_var->force_group != NULL)
	{
		mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Using force group: %s",
				gl_var->user, gl_var->ip, gl_var->portSource, gl_var->force_group);
		if (setgid(gid) == -1)
			mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Unable to force group: %s (%s)",
					gl_var->user, gl_var->ip, gl_var->portSource,
					gl_var->force_group, strerror(errno));
	}
	if (gl_var->force_user != NULL)
	{
		mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Using force user: %s",
				gl_var->user, gl_var->ip, gl_var->portSource, gl_var->force_user);
		if (setuid(uid) == -1)
			mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Unable to force user: %s (%s)",
					gl_var->user, gl_var->ip, gl_var->portSource, gl_var->force_user, strerror(errno));
	}
	if (getuid() != geteuid()) //revoke root rights in user mode !
	{
		if (seteuid(uid) == -1 || setegid(gid) == -1)
		{
			mylog_printf(MYLOG_ERROR, "[%s][%s][%i]Couldn't revoke root rights : %s",
					gl_var->user, gl_var->ip, gl_var->portSource, strerror(errno));
			exit(255);
		}
	}
}

int CheckRulesAboutMaxFiles()
{
	t_sftpwho *who;
	int i, fileread, filewrite, fileall;

	if ((who = SftWhoGetAllStructs()) != NULL)
	{
		fileread = 0;
		filewrite = 0;
		fileall = 0;
		for (i = 0; i < SFTPWHO_MAXCLIENT; i++)
			if (strcmp(who[i].user, gl_var->user) == 0)
			{
				switch (who[i].status & SFTPWHO_STATUS_MASK)
				{
				case SFTPWHO_PUT:
					filewrite++;
					fileall++;
					break;
				case SFTPWHO_GET:
					fileread++;
					fileall++;
					break;
				}
			}
		if ((fileread > gl_var->max_readfiles && gl_var->max_readfiles != 0)
				|| (filewrite > gl_var->max_writefiles
						&& gl_var->max_writefiles != 0) || (fileall
				> gl_var->max_openfiles && gl_var->max_openfiles != 0))
			return SSH2_FX_PERMISSION_DENIED;
	}
	return SSH2_FX_OK;
}

void UpdateInfoForOpenFiles()
{
	tHandle *lastFile;

	lastFile = HandleGetLastOpen(HANDLE_FILE);
	if (lastFile != NULL)
	{
		(void) snprintf(gl_var->who->file, sizeof(gl_var->who->file), "%s", lastFile->path);
		if (lastFile->flags & O_WRONLY)
			gl_var->who->status = (gl_var->who->status & SFTPWHO_ARGS_MASK) | SFTPWHO_PUT;
		else
			gl_var->who->status = (gl_var->who->status & SFTPWHO_ARGS_MASK) | SFTPWHO_GET;
		if (lastFile->fileSize > 0)
			gl_var->who->download_pos = lastFile->filePos * 100 / lastFile->fileSize;
		else
			gl_var->who->download_pos = 0;
	}
	else
	{
		gl_var->who->file[0] = '\0';
		gl_var->who->status = (gl_var->who->status & SFTPWHO_ARGS_MASK) | SFTPWHO_IDLE;
	}
}

void CloseInfoForOpenFiles()
{
	tHandle *hdl;
	int pourcentage;

	while ((hdl = HandleGetLastOpen(HANDLE_FILE)) != NULL)
	{
		if (hdl->fileSize > 0)
			pourcentage = hdl->filePos * 100 / hdl->fileSize;
		else
			pourcentage = 0;
		if (FILE_IS_UPLOAD(hdl->flags))
		{
			mylog_printf(MYLOG_TRANSFERT, "[%s][%s][%i]Interrupt upload into file '%s'",
					gl_var->user, gl_var->ip, gl_var->portSource, hdl->path);
		}
		else
		{
			mylog_printf(MYLOG_TRANSFERT, "[%s][%s][%i]Interrupt download file '%s' : %i%%",
					gl_var->user, gl_var->ip, gl_var->portSource, hdl->path, pourcentage);
		}
		HandleClose(hdl->id);
	}
}