summaryrefslogtreecommitdiff
path: root/source3/rpc_client/cli_use.c
blob: 131edfeb5bc04e7db3494ff60ad2c81a6c3cc8f2 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* 
   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 cli_use
{
	struct cli_state *cli;
	uint32 num_users;
};

static struct cli_use **clis = NULL;
uint32 num_clis = 0;

/****************************************************************************
terminate client connection
****************************************************************************/
static void cli_use_free(struct cli_use *cli)
{
	if (cli->cli != NULL)
	{
		if (cli->cli->initialised)
		{
			cli_ulogoff(cli->cli);
			cli_shutdown(cli->cli);
		}
		free(cli->cli);
	}

	free(cli);
}

/****************************************************************************
free a client array
****************************************************************************/
static void free_cli_array(uint32 num_entries, struct cli_use **entries)
{
	void(*fn)(void*) = (void(*)(void*))&cli_use_free;
	free_void_array(num_entries, (void**)entries, *fn);
}

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

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

/****************************************************************************
initiate client array
****************************************************************************/
void init_cli_use(void)
{
	clis = NULL;
	num_clis = 0;
}

/****************************************************************************
terminate client array
****************************************************************************/
void free_cli_use(void)
{
	free_cli_array(num_clis, clis);
	init_cli_use();
}

/****************************************************************************
find client state.  server name, user name, domain name and password must all
match.
****************************************************************************/
static struct cli_use *cli_find(const char* srv_name,
				const struct user_credentials *usr_creds)
{
	int i;
	const char *sv_name = srv_name;
	struct user_credentials null_usr;

	copy_user_creds(&null_usr, usr_creds);
	usr_creds = &null_usr;
		
	if (strnequal("\\\\", sv_name, 2))
	{
		sv_name = &sv_name[2];
	}

	DEBUG(10,("cli_find: %s %s %s\n",
			srv_name,
			usr_creds->user_name,
			usr_creds->domain));

	for (i = 0; i < num_clis; i++)
	{
		char *cli_name = NULL;
		struct cli_use *c = clis[i];

		if (c == NULL) continue;

		cli_name = c->cli->desthost;

		DEBUG(10,("cli_find[%d]: %s %s %s\n",
				i, cli_name,
		                c->cli->usr.user_name,
				c->cli->usr.domain));
				
		if (strnequal("\\\\", cli_name, 2))
		{
			cli_name = &cli_name[2];
		}

		if (!strequal(cli_name, sv_name))
		{
			continue;
		}
		if (!strequal(usr_creds->user_name, c->cli->usr.user_name))
		{
			continue;
		}
		if (!usr_creds->reuse &&
		    !pwd_compare(&usr_creds->pwd, &c->cli->usr.pwd))
		{
			continue;
		}
		if (usr_creds->domain[0] == 0)
		{
			return c;
		}
		if (strequal(usr_creds->domain, c->cli->usr.domain))
		{
			return c;
		}
	}

	return NULL;
}

/****************************************************************************
create a new client state from user credentials
****************************************************************************/
static struct cli_use *cli_use_get(const char* srv_name,
				const struct user_credentials *usr_creds)
{
	struct cli_use *cli = (struct cli_use*)malloc(sizeof(*cli));

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

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

	cli->cli = cli_initialise(NULL);

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

	cli_init_creds(cli->cli, usr_creds);

	return cli;
}

/****************************************************************************
init client state
****************************************************************************/
struct cli_state *cli_net_use_add(const char* srv_name,
				const struct user_credentials *usr_creds,
				BOOL redir)
{
	struct nmb_name calling;
	struct nmb_name called;
	struct in_addr *dest_ip = NULL;
	fstring dest_host;
	struct in_addr ip;

	struct cli_use *cli = cli_find(srv_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)
	{
		return False;
	}

	/*
	 * allocate
	 */

	cli = cli_use_get(srv_name, usr_creds);
	cli->cli->redirect = redir;

	if (resolve_srv_name(srv_name, dest_host, &ip))
	{
		dest_ip = &ip;
	}
	else
	{
		cli_use_free(cli);
		return NULL;
	}

	make_nmb_name(&called , dns_to_netbios_name(dest_host    ), 32, scope);
	make_nmb_name(&calling, dns_to_netbios_name(global_myname),  0, scope);

	/*
	 * connect
	 */

	if (!cli_establish_connection(cli->cli, 
	                          dest_host, dest_ip,
	                          &calling, &called,
	                          "IPC$", "IPC",
	                          False, True))
	{
		DEBUG(0,("cli_net_use_add: connection failed\n"));
		cli_use_free(cli);
		return NULL;
	}

	cli->cli->ntlmssp_cli_flgs = 0x0;

	add_cli_to_array(&num_clis, &clis, cli);
	cli->num_users++;

	return cli->cli;
}

/****************************************************************************
delete a client state
****************************************************************************/
BOOL cli_net_use_del(const char* srv_name,
				const struct user_credentials *usr_creds,
				BOOL force_close,
				BOOL *connection_closed)
{
	int i;
	const char *sv_name = srv_name;

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

	if (strnequal("\\\\", sv_name, 2))
	{
		sv_name = &sv_name[2];
	}

	*connection_closed = False;

	for (i = 0; i < num_clis; i++)
	{
		char *cli_name = NULL;

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

		cli_name = clis[i]->cli->desthost;
		if (strnequal("\\\\", cli_name, 2))
		{
			cli_name = &cli_name[2];
		}

		if (!strequal(cli_name, sv_name)) continue;

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

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

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

	return False;
}

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

	*num_cons = 0;
	*use = NULL;

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

		ZERO_STRUCT(item);

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

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

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


/****************************************************************************
wait for keyboard activity, swallowing network packets on all client states.
****************************************************************************/
void cli_use_wait_keyboard(void)
{
	fd_set fds;
	struct timeval timeout;
  
	while (1)
	{
		int i;
		int maxfd = fileno(stdin);
		FD_ZERO(&fds);
		FD_SET(fileno(stdin),&fds);
		for (i = 0; i < num_clis; i++)
		{
			if (clis[i] != NULL && clis[i]->cli != NULL)
			{
				int fd = clis[i]->cli->fd;
				FD_SET(fd,&fds);
				maxfd = MAX(fd, maxfd);
			}
		}

		timeout.tv_sec = 20;
		timeout.tv_usec = 0;
		sys_select(maxfd+1,NULL, &fds,&timeout);
      
		if (FD_ISSET(fileno(stdin),&fds))
			return;

		/* We deliberately use receive_smb instead of
		   client_receive_smb as we want to receive
		   session keepalives and then drop them here.
		*/
		for (i = 0; i < num_clis; i++)
		{
			int fd = clis[i]->cli->fd;
			if (FD_ISSET(fd,&fds))
					receive_smb(fd,clis[i]->cli->inbuf,0);
		}
	}  
}