2 Unix SMB/Netbios implementation.
4 SMB client generic functions
5 Copyright (C) Andrew Tridgell 1994-1999
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1999
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.
28 extern int DEBUGLEVEL;
30 extern pstring global_myname;
34 struct msrpc_state *cli;
38 static struct msrpc_use **msrpcs = NULL;
39 uint32 num_msrpcs = 0;
41 /****************************************************************************
42 terminate client connection
43 ****************************************************************************/
44 static void msrpc_use_free(struct msrpc_use *cli)
48 if (cli->cli->initialised)
50 msrpc_shutdown(cli->cli);
58 /****************************************************************************
60 ****************************************************************************/
61 static void free_msrpc_array(uint32 num_entries, struct msrpc_use **entries)
63 void(*fn)(void*) = (void(*)(void*))&msrpc_use_free;
64 free_void_array(num_entries, (void**)entries, *fn);
67 /****************************************************************************
68 add a client state to the array
69 ****************************************************************************/
70 static struct msrpc_use* add_msrpc_to_array(uint32 *len,
71 struct msrpc_use ***array,
72 struct msrpc_use *cli)
75 for (i = 0; i < num_msrpcs; i++)
77 if (msrpcs[i] == NULL)
84 return (struct msrpc_use*)add_item_to_array(len,
85 (void***)array, (void*)cli);
89 /****************************************************************************
91 ****************************************************************************/
92 void init_msrpc_use(void)
98 /****************************************************************************
99 terminate client array
100 ****************************************************************************/
101 void free_msrpc_use(void)
103 free_msrpc_array(num_msrpcs, msrpcs);
107 /****************************************************************************
108 find client state. server name, user name, domain name and password must all
110 ****************************************************************************/
111 static struct msrpc_use *msrpc_find(const char* pipe_name,
112 const struct user_creds *usr_creds)
115 struct user_creds null_usr;
117 copy_user_creds(&null_usr, usr_creds);
118 usr_creds = &null_usr;
120 DEBUG(10,("msrpc_find: %s %s %s\n",
122 usr_creds->ntc.user_name,
123 usr_creds->ntc.domain));
125 for (i = 0; i < num_msrpcs; i++)
127 char *msrpc_name = NULL;
128 struct msrpc_use *c = msrpcs[i];
130 if (c == NULL) continue;
132 msrpc_name = c->cli->pipe_name;
134 DEBUG(10,("msrpc_find[%d]: %s %s %s\n",
136 c->cli->usr.ntc.user_name,
137 c->cli->usr.ntc.domain));
139 if (!strequal(msrpc_name, pipe_name))
143 if (!strequal(usr_creds->ntc.user_name, c->cli->usr.ntc.user_name))
147 if (!usr_creds->reuse &&
148 !pwd_compare(&usr_creds->ntc.pwd, &c->cli->usr.ntc.pwd))
150 DEBUG(100,("password doesn't match\n"));
153 if (usr_creds->ntc.domain[0] == 0)
157 if (strequal(usr_creds->ntc.domain, c->cli->usr.ntc.domain))
166 /****************************************************************************
167 create a new client state from user credentials
168 ****************************************************************************/
169 static struct msrpc_use *msrpc_use_get(const char* pipe_name,
170 const struct user_creds *usr_creds)
172 struct msrpc_use *cli = (struct msrpc_use*)malloc(sizeof(*cli));
179 memset(cli, 0, sizeof(*cli));
181 cli->cli = msrpc_initialise(NULL);
183 if (cli->cli == NULL)
188 msrpc_init_creds(cli->cli, usr_creds);
193 /****************************************************************************
195 ****************************************************************************/
196 struct msrpc_state *msrpc_use_add(const char* pipe_name,
197 const struct user_creds *usr_creds,
200 struct msrpc_use *cli = msrpc_find(pipe_name, usr_creds);
208 /* reuse an existing connection requested, and one was not found */
209 if (usr_creds != NULL && usr_creds->reuse && !redir)
218 cli = msrpc_use_get(pipe_name, usr_creds);
219 cli->cli->redirect = redir;
221 if (!msrpc_establish_connection(cli->cli, pipe_name))
223 DEBUG(0,("msrpc_net_use_add: connection failed\n"));
229 add_msrpc_to_array(&num_msrpcs, &msrpcs, cli);
235 /****************************************************************************
236 delete a client state
237 ****************************************************************************/
238 BOOL msrpc_use_del(const char* pipe_name,
239 const struct user_creds *usr_creds,
241 BOOL *connection_closed)
245 DEBUG(10,("msrpc_net_use_del: %s. force close: %s\n",
246 pipe_name, BOOLSTR(force_close)));
248 if (connection_closed != NULL)
250 *connection_closed = False;
253 for (i = 0; i < num_msrpcs; i++)
255 char *msrpc_name = NULL;
257 if (msrpcs[i] == NULL) continue;
258 if (msrpcs[i]->cli == NULL) continue;
260 msrpc_name = msrpcs[i]->cli->pipe_name;
262 if (!strequal(msrpc_name, pipe_name)) continue;
264 if (strequal(usr_creds->ntc.user_name,
265 msrpcs[i]->cli->usr.ntc.user_name) &&
266 strequal(usr_creds->ntc.domain,
267 msrpcs[i]->cli->usr.ntc.domain))
269 /* decrement number of users */
270 msrpcs[i]->num_users--;
272 DEBUG(10,("idx: %i num_users now: %d\n",
273 i, msrpcs[i]->num_users));
275 if (force_close || msrpcs[i]->num_users == 0)
277 msrpc_use_free(msrpcs[i]);
279 if (connection_closed != NULL)
281 *connection_closed = True;
291 /****************************************************************************
292 enumerate client states
293 ****************************************************************************/
294 void msrpc_net_use_enum(uint32 *num_cons, struct use_info ***use)
301 for (i = 0; i < num_msrpcs; i++)
303 struct use_info item;
307 if (msrpcs[i] == NULL) continue;
309 item.connected = msrpcs[i]->cli != NULL ? True : False;
313 item.srv_name = msrpcs[i]->cli->pipe_name;
314 item.user_name = msrpcs[i]->cli->usr.ntc.user_name;
315 item.domain = msrpcs[i]->cli->usr.ntc.domain;
318 add_use_info_to_array(num_cons, use, &item);