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 for (i = 0; i < num_clis; i++)
118 uchar ntpw[16], clintpw[16];
119 char *cli_name = NULL;
121 if (clis[i] == NULL) continue;
123 cli_name = clis[i]->cli->desthost;
124 if (strnequal("\\\\", cli_name, 2))
126 cli_name = &cli_name[2];
129 if (!strequal(cli_name, sv_name)) continue;
131 pwd_get_lm_nt_16(&usr_creds->pwd, NULL, ntpw);
132 pwd_get_lm_nt_16(&clis[i]->cli->usr.pwd, NULL, clintpw);
134 if (strequal(usr_creds->user_name, clis[i]->cli->usr.user_name) &&
135 strequal(usr_creds->domain, clis[i]->cli->usr.domain) &&
136 memcmp(ntpw, clintpw, 16) == 0)
145 /****************************************************************************
146 create a new client state from user credentials
147 ****************************************************************************/
148 static struct cli_use *cli_use_get(const char* srv_name,
149 const struct user_credentials *usr_creds)
151 struct cli_use *cli = (struct cli_use*)malloc(sizeof(*cli));
158 memset(cli, 0, sizeof(*cli));
160 cli->cli = cli_initialise(NULL);
162 if (cli->cli == NULL)
167 cli->cli->capabilities |= CAP_NT_SMBS | CAP_STATUS32;
168 cli_init_creds(cli->cli, usr_creds);
170 cli->cli->use_ntlmv2 = lp_client_ntlmv2();
175 /****************************************************************************
177 ****************************************************************************/
178 struct cli_state *cli_net_use_addlist(char* servers,
179 const struct user_credentials *usr_creds)
181 struct cli_use *cli = cli_find(servers, usr_creds);
193 cli = cli_use_get(servers, usr_creds);
200 if (!cli_connect_serverlist(cli->cli, servers))
202 DEBUG(0,("cli_net_use_addlist: connection failed\n"));
207 cli->cli->ntlmssp_cli_flgs = 0x0;
209 add_cli_to_array(&num_clis, &clis, cli);
215 /****************************************************************************
217 ****************************************************************************/
218 struct cli_state *cli_net_use_add(const char* srv_name,
219 const struct user_credentials *usr_creds)
221 struct nmb_name calling;
222 struct nmb_name called;
223 struct in_addr *dest_ip = NULL;
227 struct cli_use *cli = cli_find(srv_name, usr_creds);
239 cli = cli_use_get(srv_name, usr_creds);
241 if (resolve_srv_name(srv_name, dest_host, &ip))
251 make_nmb_name(&called , dns_to_netbios_name(dest_host ), 32, scope);
252 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0, scope);
258 if (!cli_establish_connection(cli->cli,
264 DEBUG(0,("cli_net_use_add: connection failed\n"));
269 cli->cli->ntlmssp_cli_flgs = 0x0;
271 add_cli_to_array(&num_clis, &clis, cli);
277 /****************************************************************************
278 delete a client state
279 ****************************************************************************/
280 BOOL cli_net_use_del(const char* srv_name,
281 const struct user_credentials *usr_creds,
283 BOOL *connection_closed)
286 const char *sv_name = srv_name;
288 DEBUG(10,("cli_net_use_del: %s. force close: %s\n",
289 srv_name, BOOLSTR(force_close)));
292 if (strnequal("\\\\", sv_name, 2))
294 sv_name = &sv_name[2];
297 *connection_closed = False;
299 for (i = 0; i < num_clis; i++)
301 char *cli_name = NULL;
303 if (clis[i] == NULL) continue;
304 if (clis[i]->cli == NULL) continue;
306 cli_name = clis[i]->cli->desthost;
307 if (strnequal("\\\\", cli_name, 2))
309 cli_name = &cli_name[2];
312 if (!strequal(cli_name, sv_name)) continue;
314 if (strequal(usr_creds->user_name,
315 clis[i]->cli->usr.user_name) &&
316 strequal(usr_creds->domain,
317 clis[i]->cli->usr.domain))
319 /* decrement number of users */
320 clis[i]->num_users--;
322 DEBUG(10,("idx: %i num_users now: %d\n",
323 i, clis[i]->num_users));
326 if (force_close || clis[i]->num_users == 0)
328 cli_use_free(clis[i]);
330 *connection_closed = True;