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;
29 extern pstring global_myname;
33 struct msrpc_state *cli;
37 static struct msrpc_use **msrpcs = NULL;
38 uint32 num_msrpcs = 0;
40 /****************************************************************************
41 terminate client connection
42 ****************************************************************************/
43 static void msrpc_use_free(struct msrpc_use *cli)
47 if (cli->cli->initialised)
49 msrpc_shutdown(cli->cli);
57 /****************************************************************************
59 ****************************************************************************/
60 static void free_msrpc_array(uint32 num_entries, struct msrpc_use **entries)
62 void(*fn)(void*) = (void(*)(void*))&msrpc_use_free;
63 free_void_array(num_entries, (void**)entries, *fn);
66 /****************************************************************************
67 add a client state to the array
68 ****************************************************************************/
69 static struct msrpc_use* add_msrpc_to_array(uint32 *len,
70 struct msrpc_use ***array,
71 struct msrpc_use *cli)
74 for (i = 0; i < num_msrpcs; i++)
76 if (msrpcs[i] == NULL)
83 return (struct msrpc_use*)add_item_to_array(len,
84 (void***)array, (void*)cli);
88 /****************************************************************************
90 ****************************************************************************/
91 void init_msrpc_use(void)
97 /****************************************************************************
98 terminate client array
99 ****************************************************************************/
100 void free_msrpc_use(void)
102 free_msrpc_array(num_msrpcs, msrpcs);
106 /****************************************************************************
107 find client state. server name, user name, domain name and password must all
109 ****************************************************************************/
110 static struct msrpc_use *msrpc_find(const char* pipe_name,
111 const struct user_creds *usr_creds)
114 struct user_creds null_usr;
116 if (usr_creds == NULL)
118 copy_user_creds(&null_usr, usr_creds);
119 usr_creds = &null_usr;
122 DEBUG(10,("msrpc_find: %s %s %s\n",
124 usr_creds != NULL ? usr_creds->ntc.user_name : "null",
125 usr_creds != NULL ? usr_creds->ntc.domain : "null"));
127 for (i = 0; i < num_msrpcs; i++)
129 char *msrpc_name = NULL;
130 struct msrpc_use *c = msrpcs[i];
132 if (c == NULL) continue;
134 msrpc_name = c->cli->pipe_name;
136 DEBUG(10,("msrpc_find[%d]: %s %s %s\n",
138 c->cli->usr.ntc.user_name,
139 c->cli->usr.ntc.domain));
141 if (!strequal(msrpc_name, pipe_name))
145 if (!strequal(usr_creds->ntc.user_name, c->cli->usr.ntc.user_name))
149 if (!usr_creds->reuse &&
150 !pwd_compare(&usr_creds->ntc.pwd, &c->cli->usr.ntc.pwd))
152 DEBUG(100,("password doesn't match\n"));
155 if (usr_creds->ntc.domain[0] == 0)
159 if (strequal(usr_creds->ntc.domain, c->cli->usr.ntc.domain))
168 /****************************************************************************
169 create a new client state from user credentials
170 ****************************************************************************/
171 static struct msrpc_use *msrpc_use_get(const char* pipe_name,
173 const struct user_creds *usr_creds)
175 struct msrpc_use *cli = (struct msrpc_use*)malloc(sizeof(*cli));
182 memset(cli, 0, sizeof(*cli));
184 cli->cli = msrpc_initialise(NULL, pid);
186 if (cli->cli == NULL)
191 msrpc_init_creds(cli->cli, usr_creds);
196 /****************************************************************************
198 ****************************************************************************/
199 struct msrpc_state *msrpc_use_add(const char* pipe_name,
201 const struct user_creds *usr_creds,
204 struct msrpc_use *cli;
205 DEBUG(10,("msrpc_use_add: %s redir: %s\n", pipe_name, BOOLSTR(redir)));
207 cli = msrpc_find(pipe_name, usr_creds);
215 /* reuse an existing connection requested, and one was not found */
216 if (usr_creds != NULL && usr_creds->reuse && !redir)
218 DEBUG(0,("msrpc_use_add: reuse requested, but one not found\n"));
226 cli = msrpc_use_get(pipe_name, pid, usr_creds);
227 cli->cli->redirect = redir;
229 if (!msrpc_establish_connection(cli->cli, pipe_name))
231 DEBUG(0,("msrpc_use_add: connection failed\n"));
237 add_msrpc_to_array(&num_msrpcs, &msrpcs, cli);
243 /****************************************************************************
244 delete a client state
245 ****************************************************************************/
246 BOOL msrpc_use_del(const char* pipe_name,
247 const struct user_creds *usr_creds,
249 BOOL *connection_closed)
253 DEBUG(10,("msrpc_net_use_del: %s. force close: %s\n",
254 pipe_name, BOOLSTR(force_close)));
256 if (connection_closed != NULL)
258 *connection_closed = False;
261 for (i = 0; i < num_msrpcs; i++)
263 char *msrpc_name = NULL;
265 if (msrpcs[i] == NULL) continue;
266 if (msrpcs[i]->cli == NULL) continue;
268 msrpc_name = msrpcs[i]->cli->pipe_name;
270 if (!strequal(msrpc_name, pipe_name)) continue;
272 if (strequal(usr_creds->ntc.user_name,
273 msrpcs[i]->cli->usr.ntc.user_name) &&
274 strequal(usr_creds->ntc.domain,
275 msrpcs[i]->cli->usr.ntc.domain))
277 /* decrement number of users */
278 msrpcs[i]->num_users--;
280 DEBUG(10,("idx: %i num_users now: %d\n",
281 i, msrpcs[i]->num_users));
283 if (force_close || msrpcs[i]->num_users == 0)
285 msrpc_use_free(msrpcs[i]);
287 if (connection_closed != NULL)
289 *connection_closed = True;
299 /****************************************************************************
300 enumerate client states
301 ****************************************************************************/
302 void msrpc_net_use_enum(uint32 *num_cons, struct use_info ***use)
309 for (i = 0; i < num_msrpcs; i++)
311 struct use_info item;
315 if (msrpcs[i] == NULL) continue;
317 item.connected = msrpcs[i]->cli != NULL ? True : False;
321 item.srv_name = msrpcs[i]->cli->pipe_name;
322 item.user_name = msrpcs[i]->cli->usr.ntc.user_name;
323 item.domain = msrpcs[i]->cli->usr.ntc.domain;
326 add_use_info_to_array(num_cons, use, &item);