File: GetUsersInfos.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 (128 lines) | stat: -rw-r--r-- 3,645 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
/*
 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 <stdlib.h>
#include <string.h>
#include "Global.h"
#include "GetUsersInfos.h"
#include "Log.h"

#define	TB_ALLOC_SIZE	42

static t_info *_users = NULL;
static t_info *_groups = NULL;
static int _usersSize = 0;
static int _groupsSize = 0;

static t_info *add_element_in_table(t_info **tb, int *tbSize, char *name, int id)
{
	DEBUG((MYLOG_DEBUG, "add_element_in_table(%p, %i, %s, %i)", *tb, *tbSize, name, id));
	if ((*tbSize % TB_ALLOC_SIZE) == 0)
	{
		*tb = realloc(*tb, (*tbSize + TB_ALLOC_SIZE) * sizeof(t_info));
		DEBUG((MYLOG_DEBUG, "Increaze size to: %i", *tbSize + TB_ALLOC_SIZE));
	}
	(*tb)[*tbSize].name = name;
	(*tb)[*tbSize].id = id;
	return &((*tb)[(*tbSize)++]);
}

void free_usersinfos()
{
	int i;

	if (_users != NULL)
	{
		for (i = 0; i < _usersSize; i++)
			free(_users[i].name);
		free(_users);
		_users = NULL;
	}
	if (_groups != NULL)
	{
		for (i = 0; i < _groupsSize; i++)
			free(_groups[i].name);
		free(_groups);
		_groups = NULL;
	}
}

t_info *mygetpwnam(const char *login)
{
	struct passwd *pwd;
	int i;

	if (login == NULL)
		return (NULL);
	for (i = 0; i < _usersSize; i++)
		if (strcmp(_users[i].name, login) == 0)
			return (&_users[i]);
	if ((pwd = getpwnam(login)) != NULL)
		return (add_element_in_table(&_users, &_usersSize, strdup(pwd->pw_name), pwd->pw_uid));
	mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Couldn't resolve user name %i", gl_var->user, gl_var->ip, gl_var->portSource, login);
	return (NULL);
}

t_info *mygetpwuid(u_int32_t uid)
{
	struct passwd *pwd;
	int i;

	for (i = 0; i < _usersSize; i++)
		if (_users[i].id == uid)
			return (&_users[i]);
	if ((pwd = getpwuid(uid)) != NULL)
		return (add_element_in_table(&_users, &_usersSize, strdup(pwd->pw_name), pwd->pw_uid));
	mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Couldn't resolve user id %i", gl_var->user, gl_var->ip, gl_var->portSource, uid);
	return (NULL);
}

t_info *mygetgrnam(const char *group)
{
	struct group *grp;
	int i;

	if (group == NULL)
		return (NULL);
	for (i = 0; i < _groupsSize; i++)
		if (strcmp(_groups[i].name, group) == 0)
			return (&_groups[i]);
	if ((grp = getgrnam(group)) != NULL)
		return (add_element_in_table(&_groups, &_groupsSize, strdup(grp->gr_name), grp->gr_gid));
	mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Couldn't resolve group name %i", gl_var->user, gl_var->ip, gl_var->portSource, group);
	return (NULL);
}

t_info *mygetgrgid(u_int32_t gid)
{
	struct group *grp;
	int i;

	for (i = 0; i < _groupsSize; i++)
		if (_groups[i].id == gid)
			return (&_groups[i]);
	if ((grp = getgrgid(gid)) != NULL)
		return (add_element_in_table(&_groups, &_groupsSize, strdup(grp->gr_name), grp->gr_gid));
	mylog_printf(MYLOG_WARNING, "[%s][%s][%i]Couldn't resolve group id %i", gl_var->user, gl_var->ip, gl_var->portSource, gid);
	return (NULL);
}