summaryrefslogtreecommitdiff
path: root/source3/lib/vagent.c
blob: 96df03e2f3e8360f983e2deaeab9f03a709c2a96 (plain)
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
/* 
   Unix SMB/Netbios implementation.
   Version 2
   SMB agent/socket plugin
   Copyright (C) Andrew Tridgell 1999
   
   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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "smb.h"

extern int DEBUGLEVEL;


/****************************************************************************
terminate socket connection
****************************************************************************/
static void sock_redir_free(struct vagent_ops *va, struct sock_redir *sock)
{
	if (sock->c != -1)
	{
		close(sock->c);
		sock->c = -1;
	}
	if (sock->n != NULL)
	{
		va->free_sock(sock->n);
		sock->n = NULL;
	}
	free(sock);
}

/****************************************************************************
free a sockent array
****************************************************************************/
static void free_sock_array(struct vagent_ops*va)
{
	void(*fn)(void*) = (void(*)(void*))&va->free_sock;
	free_void_array(va->num_socks, (void**)va->socks, *fn);
}

/****************************************************************************
add a sockent state to the array
****************************************************************************/
static struct sock_redir* add_sock_to_array(uint32 *len,
				struct sock_redir ***array,
				struct sock_redir *sock)
{
	int i;
	for (i = 0; i < (*len); i++)
	{
		if ((*array)[i] == NULL)
		{
			(*array)[i] = sock;
			return sock;
		}
	}

	return (struct sock_redir*)add_item_to_array(len,
	                     (void***)array, (void*)sock);
				
}

/****************************************************************************
initiate sockent array
****************************************************************************/
void init_sock_redir(struct vagent_ops*va)
{
	va->socks = NULL;
	va->num_socks = 0;
}

/****************************************************************************
terminate sockent array
****************************************************************************/
void free_sock_redir(struct vagent_ops*va)
{
	free_sock_array(va);
	init_sock_redir(va);
}

/****************************************************************************
create a new sockent state from user credentials
****************************************************************************/
static struct sock_redir *sock_redir_get(struct vagent_ops *va, int fd)
{
	struct sock_redir *sock = (struct sock_redir*)malloc(sizeof(*sock));

	if (sock == NULL)
	{
		return NULL;
	}

	ZERO_STRUCTP(sock);

	sock->c = fd;
	sock->n = NULL;

	DEBUG(10,("sock_redir_get:\tfd:\t%d\n", fd));

	return sock;
}
/****************************************************************************
init sock state
****************************************************************************/
static void sock_add(struct vagent_ops *va, int fd)
{
	struct sock_redir *sock;
	sock = sock_redir_get(va, fd);
	if (sock != NULL)
	{
		add_sock_to_array(&va->num_socks, &va->socks, sock);
	}
}

/****************************************************************************
delete a sockent state
****************************************************************************/
static BOOL sock_del(struct vagent_ops *va, int fd)
{
	int i;

	for (i = 0; i < va->num_socks; i++)
	{
		if (va->socks[i] == NULL) continue;
		if (va->socks[i]->c == fd) 
		{
			sock_redir_free(va, va->socks[i]);
			va->socks[i] = NULL;
			return True;
		}
	}

	return False;
}

void start_agent(struct vagent_ops *va)
{
	int s, c;

	s = va->get_agent_sock(va->id);

	while (1)
	{
		int i;
		fd_set fds;
		int num;
		struct sockaddr_un addr;
		int in_addrlen = sizeof(addr);
		int maxfd = s;
		
		FD_ZERO(&fds);
		FD_SET(s, &fds);

		for (i = 0; i < va->num_socks; i++)
		{
			if (va->socks[i] != NULL)
			{
				int fd = va->socks[i]->c;
				FD_SET(fd, &fds);
				maxfd = MAX(maxfd, fd);

				if (va->socks[i]->n != NULL)
				{
					fd = va->socks[i]->s;
					FD_SET(fd, &fds);
					maxfd = MAX(fd, maxfd);
				}
			}
		}

		dbgflush();
		num = sys_select(maxfd+1,&fds,NULL, NULL);

		if (num <= 0)
		{
			continue;
		}

		if (FD_ISSET(s, &fds))
		{
			FD_CLR(s, &fds);
			c = accept(s, (struct sockaddr*)&addr, &in_addrlen);
			if (c != -1)
			{
				sock_add(va, c);
			}
		}

		for (i = 0; i < va->num_socks; i++)
		{
			if (va->socks[i] == NULL)
			{
				continue;
			}
			if (FD_ISSET(va->socks[i]->c, &fds))
			{
				FD_CLR(va->socks[i]->c, &fds);
				if (!va->process_cli_sock(va->socks,
				                          va->num_socks,
				                          va->socks[i]))
				{
					sock_redir_free(va, va->socks[i]);
					va->socks[i] = NULL;
				}
			}
			if (va->socks[i] == NULL)
			{
				continue;
			}
			if (va->socks[i]->n == NULL)
			{
				continue;
			}
			if (FD_ISSET(va->socks[i]->s, &fds))
			{
				FD_CLR(va->socks[i]->s, &fds);
				if (!va->process_srv_sock(va->socks,
				                          va->num_socks,
				                          va->socks[i]->s))
				{
					sock_redir_free(va, va->socks[i]);
					va->socks[i] = NULL;
				}
			}
		}
	}
}