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.
26 #include "rpc_parse.h"
29 extern pstring global_myname;
37 static struct ncacn_np_use **msrpcs = NULL;
38 static uint32 num_msrpcs = 0;
40 /****************************************************************************
41 terminate client connection
42 ****************************************************************************/
43 static void ncacn_np_shutdown(struct ncacn_np *cli)
45 struct ntuser_creds usr;
52 if (cli->smb->initialised)
54 /* cli_nt_session_close(cli->smb, cli->fnum); JERRY */
55 cli_nt_session_close(cli->smb);
57 create_ntc_from_cli_state(&usr, cli->smb);
58 cli_net_use_del(cli->smb->desthost, &usr, False, &closed);
63 static BOOL ncacn_np_establish_connection(struct ncacn_np *cli,
65 const struct ntuser_creds *ntc,
66 const char *pipe_name,
70 cli->smb = cli_net_use_add(srv_name, ntc,
76 /* if (!cli_nt_session_open(cli->smb, pipe_name, &cli->fnum)) by JERRY */
77 if (!cli_nt_session_open(cli->smb, (char *)pipe_name))
79 cli_net_use_del(srv_name, ntc, False, NULL);
82 fstrcpy(cli->pipe_name, pipe_name);
88 /****************************************************************************
89 terminate client connection
90 ****************************************************************************/
91 static void ncacn_np_use_free(struct ncacn_np_use *cli)
95 if (cli->cli->initialised)
97 ncacn_np_shutdown(cli->cli);
99 ZERO_STRUCTP(cli->cli);
106 /****************************************************************************
107 add a client state to the array
108 ****************************************************************************/
109 static struct ncacn_np_use *add_ncacn_np_to_array(uint32 * len,
112 struct ncacn_np_use *cli)
117 /* traverse the list and try to find a previously
118 allocate spot that is not being used */
119 for (i = 0; i < num_msrpcs; i++)
121 if (msrpcs[i] == NULL)
123 /* found and empty spot to
124 store the cli pointer */
130 return (struct ncacn_np_use *)add_item_to_array(len,
138 /****************************************************************************
139 delete a client state
140 ****************************************************************************/
141 BOOL ncacn_np_use_del(const char *srv_name, const char *pipe_name,
142 const vuser_key * key,
143 BOOL force_close, BOOL *connection_closed)
146 DEBUG(10, ("ncacn_np_net_use_del: %s. force close: %s ",
147 pipe_name, BOOLSTR(force_close)));
150 DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
154 if (connection_closed != NULL)
156 *connection_closed = False;
159 if (strnequal("\\PIPE\\", pipe_name, 6))
161 pipe_name = &pipe_name[6];
164 if (strnequal("\\\\", srv_name, 2))
166 srv_name = &srv_name[2];
169 for (i = 0; i < num_msrpcs; i++)
171 char *ncacn_np_name = NULL;
172 char *ncacn_np_srv_name = NULL;
173 struct ncacn_np_use *c = msrpcs[i];
176 if (c == NULL || c->cli == NULL || c->cli->smb == NULL)
179 ncacn_np_name = c->cli->pipe_name;
180 ncacn_np_srv_name = c->cli->smb->desthost;
182 k = c->cli->smb->key;
184 DEBUG(10, ("use_del[%d]: %s %s %s %s [%d,%x]\n",
185 i, ncacn_np_name, ncacn_np_srv_name,
186 c->cli->smb->user_name,
187 c->cli->smb->domain, k.pid, k.vuid));
189 if (strnequal("\\PIPE\\", ncacn_np_name, 6))
191 ncacn_np_name = &ncacn_np_name[6];
193 if (!strequal(ncacn_np_name, pipe_name))
197 if (strnequal("\\\\", ncacn_np_srv_name, 2))
199 ncacn_np_srv_name = &ncacn_np_srv_name[2];
201 if (!strequal(ncacn_np_srv_name, srv_name))
205 if (key->pid != k.pid || key->vuid != k.vuid)
209 /* decrement number of users */
211 DEBUG(10, ("idx: %i num_users now: %d\n",
213 if (force_close || c->num_users == 0)
215 ncacn_np_use_free(c);
217 if (connection_closed != NULL)
219 *connection_closed = True;
228 /****************************************************************************
229 find client state. server name, user name, domain name and password must all
231 ****************************************************************************/
232 static struct ncacn_np_use *ncacn_np_find(const char *srv_name,
233 const char *pipe_name,
234 const vuser_key * key,
235 const struct ntuser_creds
236 *usr_creds, BOOL reuse)
239 const char *sv_name = srv_name;
241 if (strnequal("\\PIPE\\", pipe_name, 6))
243 pipe_name = &pipe_name[6];
246 if (strnequal("\\\\", sv_name, 2))
248 sv_name = &sv_name[2];
251 if (usr_creds != NULL)
253 DEBUG(10, ("ncacn_np_find: %s %s %s",
254 srv_name, usr_creds->user_name, usr_creds->domain));
258 DEBUG(10,("ncacn_np_find: %s (no creds)\n", srv_name));
263 DEBUG(10, ("[%d,%x]", key->pid, key->vuid));
267 for (i = 0; i < num_msrpcs; i++)
269 char *ncacn_np_srv_name = NULL;
270 struct ncacn_np_use *c = msrpcs[i];
273 char *ncacn_np_name = NULL;
275 if (c == NULL || c->cli == NULL || c->cli->smb == NULL ||
276 c->cli->smb->fd == -1 ||
277 !c->cli->initialised)
282 ncacn_np_name = c->cli->pipe_name;
283 ncacn_np_srv_name = c->cli->smb->desthost;
285 k = c->cli->smb->key;
287 DEBUG(10, ("ncacn_np_find[%d]: %s %s %s %s [%d,%x]\n",
288 i, ncacn_np_name, ncacn_np_srv_name,
289 c->cli->smb->user_name,
290 c->cli->smb->domain, k.pid, k.vuid));
292 if (strnequal("\\\\", ncacn_np_srv_name, 2))
294 ncacn_np_srv_name = &ncacn_np_srv_name[2];
297 if (strnequal("\\PIPE\\", ncacn_np_name, 6))
299 ncacn_np_name = &ncacn_np_name[6];
302 if (!strequal(ncacn_np_name, pipe_name))
306 if (!strequal(ncacn_np_srv_name, sv_name))
310 if (key != NULL && (k.pid != key->pid || k.vuid != key->vuid))
314 if (usr_creds == NULL)
326 (usr_creds->user_name, c->cli->smb->user_name))
331 && !pwd_compare((struct pwd_info *)&usr_creds->pwd, &c->cli->smb->pwd))
333 DEBUG(100, ("password doesn't match\n"));
336 if (usr_creds->domain[0] == 0)
340 if (strequal(usr_creds->domain, c->cli->smb->domain))
350 /****************************************************************************
351 initialise a msrpcent structure
352 ****************************************************************************/
353 struct ncacn_np *ncacn_np_initialise(struct ncacn_np *msrpc,
354 const vuser_key * key)
358 msrpc = (struct ncacn_np *)malloc(sizeof(*msrpc));
364 if (msrpc->initialised)
366 ncacn_np_shutdown(msrpc);
372 msrpc->initialised = 1;
377 /****************************************************************************
378 create a new client state from user credentials
379 ****************************************************************************/
380 static struct ncacn_np_use *ncacn_np_use_get(const char *pipe_name,
381 const vuser_key * key)
383 struct ncacn_np_use *cli =
384 (struct ncacn_np_use *)malloc(sizeof(*cli));
391 memset(cli, 0, sizeof(*cli));
393 cli->cli = ncacn_np_initialise(NULL, key);
395 if (cli->cli == NULL)
403 /****************************************************************************
405 ****************************************************************************/
406 struct ncacn_np *ncacn_np_use_add(const char *pipe_name,
407 const vuser_key * key,
408 const char *srv_name,
409 const struct ntuser_creds *ntc,
410 BOOL reuse, BOOL *is_new_connection)
412 struct ncacn_np_use *cli;
413 DEBUG(10, ("ncacn_np_use_add: %s\n", pipe_name));
415 (*is_new_connection) = False;
416 cli = ncacn_np_find(srv_name, pipe_name, key, ntc, reuse);
428 (*is_new_connection) = True;
430 cli = ncacn_np_use_get(pipe_name, key);
432 if (!ncacn_np_establish_connection
433 (cli->cli, srv_name, ntc, pipe_name, True))
435 DEBUG(0, ("ncacn_np_use_add: connection failed\n"));
437 ncacn_np_use_free(cli);
443 cli->cli->smb->key = *key;
447 cli->cli->smb->key.pid = sys_getpid();
448 cli->cli->smb->key.vuid = UID_FIELD_INVALID;
451 add_ncacn_np_to_array(&num_msrpcs, &msrpcs, cli);