File: user.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 (163 lines) | stat: -rw-r--r-- 3,957 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
/*
 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 <sys/types.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ip.h"
#include "parsing.h"
#include "user.h"
#include "../SftpServer/Access.h"

/*@null@*/ static char *user_name = NULL;

int init_user_info()
{
	struct passwd *info;

	InitAccess();
	if ((info = getpwuid(getuid())))
	{
		if ((user_name = strdup(info->pw_name)) == NULL)
			return (0);
		hash_set("User", (void *) strdup(info->pw_name));
		hash_set("Home", (void *) strdup(info->pw_dir));
		return (1);
	}
	return (0);
}

void free_user_info()
{
	if (user_name != NULL)
		free(user_name);
	user_name = NULL;
}

int is_for_user(/*@null@*/ const char *user, int verbose)
{
	if (user == NULL)
		return (0);
	if (strcmp(user, TAG_ALL) == 0)
	{
		if (verbose >= 2)
			(void) printf("--- Apply restrictions for all users ---\n");
		return (1);
	}
	if (user_name != NULL && strcmp(user, user_name) == 0)
	{
		if (verbose >= 2)
			(void) printf("--- Apply restrictions for user '%s' ---\n", user);
		return (1);
	}
	return (0);
}

int is_for_group(const char *group, int verbose)
{
	struct group *grp;

	if (strcmp(group, TAG_ALL) == 0)
	{
		if (verbose >= 2)
			(void) printf("--- Apply restrictions for all groups ---\n");
		return (1);
	}
	if ((grp = getgrnam(group)) != NULL)
		if (UserIsInThisGroup(grp->gr_gid) == 1)
		{
			if (verbose >= 2)
				(void) printf("--- Apply restrictions for group '%s' ---\n",
						group);
			return (1);
		}
	return (0);
}

int is_for_virtualhost(const char *host, int port, int verbose)
{
	char *current_host;
	int current_port;

	current_host = (char *) hash_get("SERVER_IP");
	current_port = hash_get_int("SERVER_PORT");
	if (current_host != NULL && host != NULL && (strcmp(host, current_host)
			== 0 || strcmp(host, TAG_ALL) == 0))
		if (current_port == 0 || port == current_port)
		{
			if (verbose >= 2)
				(void) printf(
						"--- Apply restriction for virtualhost '%s:%i' ---\n",
						current_host, current_port);
			return (1);
		}
	return (0);
}

int is_for_rangeip(const char *range, int verbose)
{
	char *bip, *ip;
	int pos, size, retValue = 0;

	if (range == NULL)
		return (0);
	size = (int) ((unsigned char) range[8]);
	ip = get_ip(0); //don't resolv dns
	if (ip == NULL)
		return (0);
	bip = TagParseRangeIP(ip);
	if (bip == NULL)
		return (0);
	pos = 0;
	while (size >= 8)
	{
		if (range[pos] <= bip[pos] && bip[pos] <= range[pos + 4])
		{
			pos++;
			size -= 8;
		}
		else
			goto error_is_for_rangeip;
	}
	if (size > 0)
	{
		bip[pos] = (unsigned char) bip[pos] >> (8 - size);
		bip[pos] = (unsigned char) bip[pos] << (8 - size);
		if (range[pos] > bip[pos] || bip[pos] > range[pos + 4])
			goto error_is_for_rangeip;
	}
	if (verbose >= 2)
		(void) printf(
				"--- Apply restrictions for ip range '%i.%i.%i.%i-%i.%i.%i.%i/%i' ---\n",
				(unsigned char) range[0], (unsigned char) range[1],
				(unsigned char) range[2], (unsigned char) range[3],
				(unsigned char) range[4], (unsigned char) range[5],
				(unsigned char) range[6], (unsigned char) range[7],
				(unsigned char) range[8]);
	retValue = 1;

error_is_for_rangeip:
	free(bip);
	free(ip);
	return (retValue);
}