summaryrefslogtreecommitdiff
path: root/source3/lib/msrpc_use.c
blob: 193867e6100dd0cc0fbdcb2d7b26d16bd61152b8 (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
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
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB client generic functions
   Copyright (C) Andrew Tridgell 1994-1999
   Copyright (C) Luke Kenneth Casson Leighton 1996-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.
*/

#define NO_SYSLOG

#include "includes.h"
#include "trans2.h"

extern int DEBUGLEVEL;
extern pstring scope;
extern pstring global_myname;

struct msrpc_use
{
	struct msrpc_state *cli;
	uint32 num_users;
};

static struct msrpc_use **msrpcs = NULL;
uint32 num_msrpcs = 0;

/****************************************************************************
terminate client connection
****************************************************************************/
static void msrpc_use_free(struct msrpc_use *cli)
{
	if (cli->cli != NULL)
	{
		if (cli->cli->initialised)
		{
			msrpc_shutdown(cli->cli);
		}
		free(cli->cli);
	}

	free(cli);
}

/****************************************************************************
free a client array
****************************************************************************/
static void free_msrpc_array(uint32 num_entries, struct msrpc_use **entries)
{
	void(*fn)(void*) = (void(*)(void*))&msrpc_use_free;
	free_void_array(num_entries, (void**)entries, *fn);
}

/****************************************************************************
add a client state to the array
****************************************************************************/
static struct msrpc_use* add_msrpc_to_array(uint32 *len,
				struct msrpc_use ***array,
				struct msrpc_use *cli)
{
	int i;
	for (i = 0; i < num_msrpcs; i++)
	{
		if (msrpcs[i] == NULL)
		{
			msrpcs[i] = cli;
			return cli;
		}
	}

	return (struct msrpc_use*)add_item_to_array(len,
	                     (void***)array, (void*)cli);
				
}

/****************************************************************************
initiate client array
****************************************************************************/
void init_msrpc_use(void)
{
	msrpcs = NULL;
	num_msrpcs = 0;
}

/****************************************************************************
terminate client array
****************************************************************************/
void free_msrpc_use(void)
{
	free_msrpc_array(num_msrpcs, msrpcs);
	init_msrpc_use();
}

/****************************************************************************
find client state.  server name, user name, domain name and password must all
match.
****************************************************************************/
static struct msrpc_use *msrpc_find(const char* pipe_name,
				const struct user_creds *usr_creds)
{
	int i;
	struct user_creds null_usr;

	copy_user_creds(&null_usr, usr_creds);
	usr_creds = &null_usr;
		
	DEBUG(10,("msrpc_find: %s %s %s\n",
			pipe_name,
			usr_creds->ntc.user_name,
			usr_creds->ntc.domain));

	for (i = 0; i < num_msrpcs; i++)
	{
		char *msrpc_name = NULL;
		struct msrpc_use *c = msrpcs[i];

		if (c == NULL) continue;

		msrpc_name = c->cli->pipe_name;

		DEBUG(10,("msrpc_find[%d]: %s %s %s\n",
				i, msrpc_name,
		                c->cli->usr.ntc.user_name,
				c->cli->usr.ntc.domain));
				
		if (!strequal(msrpc_name, pipe_name))
		{
			continue;
		}
		if (!strequal(usr_creds->ntc.user_name, c->cli->usr.ntc.user_name))
		{
			continue;
		}
		if (!usr_creds->reuse &&
		    !pwd_compare(&usr_creds->ntc.pwd, &c->cli->usr.ntc.pwd))
		{
			DEBUG(100,("password doesn't match\n"));
			continue;
		}
		if (usr_creds->ntc.domain[0] == 0)
		{
			return c;
		}
		if (strequal(usr_creds->ntc.domain, c->cli->usr.ntc.domain))
		{
			return c;
		}
	}

	return NULL;
}

/****************************************************************************
create a new client state from user credentials
****************************************************************************/
static struct msrpc_use *msrpc_use_get(const char* pipe_name,
				const struct user_creds *usr_creds)
{
	struct msrpc_use *cli = (struct msrpc_use*)malloc(sizeof(*cli));

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

	memset(cli, 0, sizeof(*cli));

	cli->cli = msrpc_initialise(NULL);

	if (cli->cli == NULL)
	{
		return NULL;
	}

	msrpc_init_creds(cli->cli, usr_creds);

	return cli;
}

/****************************************************************************
init client state
****************************************************************************/
struct msrpc_state *msrpc_use_add(const char* pipe_name,
				const struct user_creds *usr_creds,
				BOOL redir)
{
	struct msrpc_use *cli = msrpc_find(pipe_name, usr_creds); 

	if (cli != NULL)
	{
		cli->num_users++;
		return cli->cli;
	}

	/* reuse an existing connection requested, and one was not found */
	if (usr_creds != NULL && usr_creds->reuse && !redir)
	{
		return False;
	}

	/*
	 * allocate
	 */

	cli = msrpc_use_get(pipe_name, usr_creds);
	cli->cli->redirect = redir;

	if (!msrpc_establish_connection(cli->cli, pipe_name))
	{
		DEBUG(0,("msrpc_net_use_add: connection failed\n"));
		cli->cli = NULL;
		msrpc_use_free(cli);
		return NULL;
	}

	add_msrpc_to_array(&num_msrpcs, &msrpcs, cli);
	cli->num_users++;

	return cli->cli;
}

/****************************************************************************
delete a client state
****************************************************************************/
BOOL msrpc_use_del(const char* pipe_name,
				const struct user_creds *usr_creds,
				BOOL force_close,
				BOOL *connection_closed)
{
	int i;

	DEBUG(10,("msrpc_net_use_del: %s. force close: %s\n",
	           pipe_name, BOOLSTR(force_close)));

	if (connection_closed != NULL)
	{
		*connection_closed = False;
	}

	for (i = 0; i < num_msrpcs; i++)
	{
		char *msrpc_name = NULL;

		if (msrpcs[i] == NULL) continue;
		if (msrpcs[i]->cli == NULL) continue;

		msrpc_name = msrpcs[i]->cli->pipe_name;

		if (!strequal(msrpc_name, pipe_name)) continue;

		if (strequal(usr_creds->ntc.user_name,
                             msrpcs[i]->cli->usr.ntc.user_name) &&
		    strequal(usr_creds->ntc.domain,
		             msrpcs[i]->cli->usr.ntc.domain))
		{
			/* decrement number of users */
			msrpcs[i]->num_users--;

			DEBUG(10,("idx: %i num_users now: %d\n",
				   i, msrpcs[i]->num_users));

			if (force_close || msrpcs[i]->num_users == 0)
			{
				msrpc_use_free(msrpcs[i]);
				msrpcs[i] = NULL;
				if (connection_closed != NULL)
				{
					*connection_closed = True;
				}
			}
			return True;
		}
	}

	return False;
}

/****************************************************************************
enumerate client states
****************************************************************************/
void msrpc_net_use_enum(uint32 *num_cons, struct use_info ***use)
{
	int i;

	*num_cons = 0;
	*use = NULL;

	for (i = 0; i < num_msrpcs; i++)
	{
		struct use_info item;

		ZERO_STRUCT(item);

		if (msrpcs[i] == NULL) continue;

		item.connected = msrpcs[i]->cli != NULL ? True : False;
	
		if (item.connected)
		{
			item.srv_name  = msrpcs[i]->cli->pipe_name;
			item.user_name = msrpcs[i]->cli->usr.ntc.user_name;
			item.domain    = msrpcs[i]->cli->usr.ntc.domain;
		}

		add_use_info_to_array(num_cons, use, &item);
	}
}