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 cli_state *cli;
38 static struct cli_use **clis = NULL;
41 /****************************************************************************
42 terminate client connection
43 ****************************************************************************/
44 static void cli_use_free(struct cli_use *cli)
46 cli_ulogoff(cli->cli);
47 cli_shutdown(cli->cli);
53 /****************************************************************************
55 ****************************************************************************/
56 static void free_cli_array(uint32 num_entries, struct cli_use **entries)
58 void(*fn)(void*) = (void(*)(void*))&cli_use_free;
59 free_void_array(num_entries, (void**)entries, *fn);
62 /****************************************************************************
63 add a client state to the array
64 ****************************************************************************/
65 static struct cli_use* add_cli_to_array(uint32 *len,
66 struct cli_use ***array,
70 for (i = 0; i < num_clis; i++)
79 return (struct cli_use*)add_item_to_array(len,
80 (void***)array, (void*)cli);
84 /****************************************************************************
86 ****************************************************************************/
87 void init_cli_use(void)
93 /****************************************************************************
94 terminate client array
95 ****************************************************************************/
96 void free_cli_use(void)
98 free_cli_array(num_clis, clis);
102 /****************************************************************************
103 find client state. server name, user name, domain name and password must all
105 ****************************************************************************/
106 static struct cli_use *cli_find(const char* srv_name,
107 const struct user_credentials *usr_creds)
110 const char *sv_name = srv_name;
111 if (strnequal("\\\\", sv_name, 2))
113 sv_name = &sv_name[2];
116 DEBUG(10,("cli_find: %s %s %s\n",
118 usr_creds->user_name,
121 for (i = 0; i < num_clis; i++)
123 char *cli_name = NULL;
124 struct cli_use *c = clis[i];
126 if (c == NULL) continue;
128 cli_name = c->cli->desthost;
130 DEBUG(10,("cli_find[%d]: %s %s %s\n",
132 c->cli->usr.user_name,
133 c->cli->usr.domain));
135 if (strnequal("\\\\", cli_name, 2))
137 cli_name = &cli_name[2];
140 if (!strequal(cli_name, sv_name))
144 if (!strequal(usr_creds->user_name, c->cli->usr.user_name))
148 if (!pwd_compare(&usr_creds->pwd, &c->cli->usr.pwd))
152 if (usr_creds->domain[0] == 0)
156 if (strequal(usr_creds->domain, c->cli->usr.domain))
165 /****************************************************************************
166 create a new client state from user credentials
167 ****************************************************************************/
168 static struct cli_use *cli_use_get(const char* srv_name,
169 const struct user_credentials *usr_creds)
171 struct cli_use *cli = (struct cli_use*)malloc(sizeof(*cli));
178 memset(cli, 0, sizeof(*cli));
180 cli->cli = cli_initialise(NULL);
182 if (cli->cli == NULL)
187 cli_init_creds(cli->cli, usr_creds);
192 /****************************************************************************
194 ****************************************************************************/
195 struct cli_state *cli_net_use_addlist(char* servers,
196 const struct user_credentials *usr_creds)
198 struct cli_use *cli = cli_find(servers, usr_creds);
210 cli = cli_use_get(servers, usr_creds);
217 if (!cli_connect_serverlist(cli->cli, servers))
219 DEBUG(0,("cli_net_use_addlist: connection failed\n"));
224 cli->cli->ntlmssp_cli_flgs = 0x0;
226 add_cli_to_array(&num_clis, &clis, cli);
232 /****************************************************************************
234 ****************************************************************************/
235 struct cli_state *cli_net_use_add(const char* srv_name,
236 const struct user_credentials *usr_creds)
238 struct nmb_name calling;
239 struct nmb_name called;
240 struct in_addr *dest_ip = NULL;
244 struct cli_use *cli = cli_find(srv_name, usr_creds);
256 cli = cli_use_get(srv_name, usr_creds);
258 if (resolve_srv_name(srv_name, dest_host, &ip))
268 make_nmb_name(&called , dns_to_netbios_name(dest_host ), 32, scope);
269 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0, scope);
275 if (!cli_establish_connection(cli->cli,
281 DEBUG(0,("cli_net_use_add: connection failed\n"));
286 cli->cli->ntlmssp_cli_flgs = 0x0;
288 add_cli_to_array(&num_clis, &clis, cli);
294 /****************************************************************************
295 delete a client state
296 ****************************************************************************/
297 BOOL cli_net_use_del(const char* srv_name,
298 const struct user_credentials *usr_creds,
300 BOOL *connection_closed)
303 const char *sv_name = srv_name;
305 DEBUG(10,("cli_net_use_del: %s. force close: %s\n",
306 srv_name, BOOLSTR(force_close)));
308 if (strnequal("\\\\", sv_name, 2))
310 sv_name = &sv_name[2];
313 *connection_closed = False;
315 for (i = 0; i < num_clis; i++)
317 char *cli_name = NULL;
319 if (clis[i] == NULL) continue;
320 if (clis[i]->cli == NULL) continue;
322 cli_name = clis[i]->cli->desthost;
323 if (strnequal("\\\\", cli_name, 2))
325 cli_name = &cli_name[2];
328 if (!strequal(cli_name, sv_name)) continue;
330 if (strequal(usr_creds->user_name,
331 clis[i]->cli->usr.user_name) &&
332 strequal(usr_creds->domain,
333 clis[i]->cli->usr.domain))
335 /* decrement number of users */
336 clis[i]->num_users--;
338 DEBUG(10,("idx: %i num_users now: %d\n",
339 i, clis[i]->num_users));
341 if (force_close || clis[i]->num_users == 0)
343 cli_use_free(clis[i]);
345 *connection_closed = True;
354 /****************************************************************************
355 enumerate client states
356 ****************************************************************************/
357 void cli_net_use_enum(uint32 *num_cons, struct use_info ***use)
364 for (i = 0; i < num_clis; i++)
366 struct use_info item;
370 if (clis[i] == NULL) continue;
372 item.connected = clis[i]->cli != NULL ? True : False;
376 item.srv_name = clis[i]->cli->desthost;
377 item.user_name = clis[i]->cli->usr.user_name;
378 item.domain = clis[i]->cli->usr.domain;
381 add_use_to_array(num_cons, use, &item);