2 Unix SMB/Netbios implementation.
4 SMB client generic functions
5 Copyright (C) Andrew Tridgell 1994-2000
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "rpc_parse.h"
30 extern int DEBUGLEVEL;
31 extern pstring global_myname;
39 static struct ncacn_np_use **msrpcs = NULL;
40 static uint32 num_msrpcs = 0;
42 /****************************************************************************
43 terminate client connection
44 ****************************************************************************/
45 static void ncacn_np_shutdown(struct ncacn_np *cli)
47 struct ntuser_creds usr;
54 if (cli->smb->initialised)
56 /* cli_nt_session_close(cli->smb, cli->fnum); JERRY */
57 cli_nt_session_close(cli->smb);
59 create_ntc_from_cli_state(&usr, cli->smb);
60 cli_net_use_del(cli->smb->desthost, &usr, False, &closed);
65 static BOOL ncacn_np_establish_connection(struct ncacn_np *cli,
67 const struct ntuser_creds *ntc,
68 const char *pipe_name,
72 cli->smb = cli_net_use_add(srv_name, ntc,
78 /* if (!cli_nt_session_open(cli->smb, pipe_name, &cli->fnum)) by JERRY */
79 if (!cli_nt_session_open(cli->smb, (char *)pipe_name))
81 cli_net_use_del(srv_name, ntc, False, NULL);
84 fstrcpy(cli->pipe_name, pipe_name);
90 /****************************************************************************
91 terminate client connection
92 ****************************************************************************/
93 static void ncacn_np_use_free(struct ncacn_np_use *cli)
97 if (cli->cli->initialised)
99 ncacn_np_shutdown(cli->cli);
101 ZERO_STRUCTP(cli->cli);
108 /****************************************************************************
109 add a client state to the array
110 ****************************************************************************/
111 static struct ncacn_np_use *add_ncacn_np_to_array(uint32 * len,
114 struct ncacn_np_use *cli)
119 /* traverse the list and try to find a previously
120 allocate spot that is not being used */
121 for (i = 0; i < num_msrpcs; i++)
123 if (msrpcs[i] == NULL)
125 /* found and empty spot to
126 store the cli pointer */
132 return (struct ncacn_np_use *)add_item_to_array(len,
140 /****************************************************************************
141 delete a client state
142 ****************************************************************************/
143 BOOL ncacn_np_use_del(const char *srv_name, const char *pipe_name,
144 const vuser_key * key,
145 BOOL force_close, BOOL *connection_closed)
148 DEBUG(10, ("ncacn_np_net_use_del: %s. force close: %s ",
149 pipe_name, BOOLSTR(force_close)));
152 DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
156 if (connection_closed != NULL)
158 *connection_closed = False;
161 if (strnequal("\\PIPE\\", pipe_name, 6))
163 pipe_name = &pipe_name[6];
166 if (strnequal("\\\\", srv_name, 2))
168 srv_name = &srv_name[2];
171 for (i = 0; i < num_msrpcs; i++)
173 char *ncacn_np_name = NULL;
174 char *ncacn_np_srv_name = NULL;
175 struct ncacn_np_use *c = msrpcs[i];
178 if (c == NULL || c->cli == NULL || c->cli->smb == NULL)
181 ncacn_np_name = c->cli->pipe_name;
182 ncacn_np_srv_name = c->cli->smb->desthost;
184 k = c->cli->smb->key;
186 DEBUG(10, ("use_del[%d]: %s %s %s %s [%d,%x]\n",
187 i, ncacn_np_name, ncacn_np_srv_name,
188 c->cli->smb->user_name,
189 c->cli->smb->domain, k.pid, k.vuid));
191 if (strnequal("\\PIPE\\", ncacn_np_name, 6))
193 ncacn_np_name = &ncacn_np_name[6];
195 if (!strequal(ncacn_np_name, pipe_name))
199 if (strnequal("\\\\", ncacn_np_srv_name, 2))
201 ncacn_np_srv_name = &ncacn_np_srv_name[2];
203 if (!strequal(ncacn_np_srv_name, srv_name))
207 if (key->pid != k.pid || key->vuid != k.vuid)
211 /* decrement number of users */
213 DEBUG(10, ("idx: %i num_users now: %d\n",
215 if (force_close || c->num_users == 0)
217 ncacn_np_use_free(c);
219 if (connection_closed != NULL)
221 *connection_closed = True;
230 /****************************************************************************
231 find client state. server name, user name, domain name and password must all
233 ****************************************************************************/
234 static struct ncacn_np_use *ncacn_np_find(const char *srv_name,
235 const char *pipe_name,
236 const vuser_key * key,
237 const struct ntuser_creds
238 *usr_creds, BOOL reuse)
241 const char *sv_name = srv_name;
243 if (strnequal("\\PIPE\\", pipe_name, 6))
245 pipe_name = &pipe_name[6];
248 if (strnequal("\\\\", sv_name, 2))
250 sv_name = &sv_name[2];
253 if (usr_creds != NULL)
255 DEBUG(10, ("ncacn_np_find: %s %s %s",
256 srv_name, usr_creds->user_name, usr_creds->domain));
260 DEBUG(10,("ncacn_np_find: %s (no creds)\n", srv_name));
265 DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
269 for (i = 0; i < num_msrpcs; i++)
271 char *ncacn_np_srv_name = NULL;
272 struct ncacn_np_use *c = msrpcs[i];
275 char *ncacn_np_name = NULL;
277 if (c == NULL || c->cli == NULL || c->cli->smb == NULL ||
278 c->cli->smb->fd == -1 ||
279 !c->cli->initialised)
284 ncacn_np_name = c->cli->pipe_name;
285 ncacn_np_srv_name = c->cli->smb->desthost;
287 k = c->cli->smb->key;
289 DEBUG(10, ("ncacn_np_find[%d]: %s %s %s %s [%d,%x]\n",
290 i, ncacn_np_name, ncacn_np_srv_name,
291 c->cli->smb->user_name,
292 c->cli->smb->domain, k.pid, k.vuid));
294 if (strnequal("\\\\", ncacn_np_srv_name, 2))
296 ncacn_np_srv_name = &ncacn_np_srv_name[2];
299 if (strnequal("\\PIPE\\", ncacn_np_name, 6))
301 ncacn_np_name = &ncacn_np_name[6];
304 if (!strequal(ncacn_np_name, pipe_name))
308 if (!strequal(ncacn_np_srv_name, sv_name))
312 if (key != NULL && (k.pid != key->pid || k.vuid != key->vuid))
316 if (usr_creds == NULL)
328 (usr_creds->user_name, c->cli->smb->user_name))
333 && !pwd_compare((struct pwd_info *)&usr_creds->pwd, &c->cli->smb->pwd))
335 DEBUG(100, ("password doesn't match\n"));
338 if (usr_creds->domain[0] == 0)
342 if (strequal(usr_creds->domain, c->cli->smb->domain))
352 /****************************************************************************
353 initialise a msrpcent structure
354 ****************************************************************************/
355 struct ncacn_np *ncacn_np_initialise(struct ncacn_np *msrpc,
356 const vuser_key * key)
360 msrpc = (struct ncacn_np *)malloc(sizeof(*msrpc));
366 if (msrpc->initialised)
368 ncacn_np_shutdown(msrpc);
374 msrpc->initialised = 1;
379 /****************************************************************************
380 create a new client state from user credentials
381 ****************************************************************************/
382 static struct ncacn_np_use *ncacn_np_use_get(const char *pipe_name,
383 const vuser_key * key)
385 struct ncacn_np_use *cli =
386 (struct ncacn_np_use *)malloc(sizeof(*cli));
393 memset(cli, 0, sizeof(*cli));
395 cli->cli = ncacn_np_initialise(NULL, key);
397 if (cli->cli == NULL)
405 /****************************************************************************
407 ****************************************************************************/
408 struct ncacn_np *ncacn_np_use_add(const char *pipe_name,
409 const vuser_key * key,
410 const char *srv_name,
411 const struct ntuser_creds *ntc,
412 BOOL reuse, BOOL *is_new_connection)
414 struct ncacn_np_use *cli;
415 DEBUG(10, ("ncacn_np_use_add: %s\n", pipe_name));
417 (*is_new_connection) = False;
418 cli = ncacn_np_find(srv_name, pipe_name, key, ntc, reuse);
430 (*is_new_connection) = True;
432 cli = ncacn_np_use_get(pipe_name, key);
434 if (!ncacn_np_establish_connection
435 (cli->cli, srv_name, ntc, pipe_name, True))
437 DEBUG(0, ("ncacn_np_use_add: connection failed\n"));
439 ncacn_np_use_free(cli);
445 cli->cli->smb->key = *key;
449 cli->cli->smb->key.pid = sys_getpid();
450 cli->cli->smb->key.vuid = UID_FIELD_INVALID;
453 add_ncacn_np_to_array(&num_msrpcs, &msrpcs, cli);